Commit f344c4a3 authored by Tim Soderstrom's avatar Tim Soderstrom

Other button now sets speed (hi/lo)

But there is no debounce and also the speed is non-linear.
In other words 50% speed isn't half the RPMs.
parent ed070b2a
......@@ -23,11 +23,15 @@
// Specified Motor RPM
// #define MOTOR_RPM 76
// #define MOTOR_RPM_LOW 45
// Motor Start Percentage
#define MOTOR_START_PCT 100
// Slowest Motor Will Turn
// #define MOTOR_MIN_RPM 24
#define MOTOR_MIN_PCT 50
// Note the DC motors used are linear when it comes to percentages
// e.g. 50% doesn't seem to be half of the rotations.
#define MOTOR_SPEED_LOW_PCT 25
#define MOTOR_SPEED_HIGH_PCT 100
// How long to free spin motor when changing direction
#define COAST_MOTOR_MILLIS 1000
......@@ -48,8 +52,10 @@
#define MOTOR_SPEED 0
#define MOTOR_X A2
#define MOTOR_Y A3
//#define RUN_SWITCH A1
//#define SPEED_SWITCH 1
//#define SPEED_BUTTON 1
//#define RUN_BUTTON 2
#define SPEED_BUTTON 1
#define RUN_BUTTON 2
......@@ -68,11 +74,15 @@ bool motorCoasting = false;
// Based on switch
bool motorRun = false;
// Motor Speed
byte motorSpeedPct = 100;
// Debounce
int lastButtonState = LOW;
int buttonState = LOW;
unsigned long lastDebounceTime = 0;
/*******
* Code
********/
......@@ -82,12 +92,12 @@ void setup()
pinMode(MOTOR_SPEED, OUTPUT);
pinMode(MOTOR_X, OUTPUT);
pinMode(MOTOR_Y, OUTPUT);
//pinMode(RUN_SWITCH, INPUT);
//pinMode(SPEED_SWITCH, INPUT);
pinMode(RUN_BUTTON, INPUT_PULLUP);
pinMode(SPEED_BUTTON, INPUT_PULLUP);
/* Start forward at start speed */
analogWrite(MOTOR_SPEED, toSpeed(MOTOR_START_PCT));
analogWrite(MOTOR_SPEED, toSpeed(MOTOR_SPEED_HIGH_PCT));
digitalWrite(MOTOR_X, HIGH);
digitalWrite(MOTOR_Y, LOW);
}
......@@ -99,12 +109,10 @@ void loop()
void runMotor()
{
// byte rpm = MOTOR_RPM;
//byte rpm = MOTOR_START_PCT;
byte rpm = MOTOR_START_PCT;
byte directionInterval = DEFAULT_DIRECTION_INTERVAL;
bool runMotor = true;
bool buttonHeld = false;
bool runButtonHeld = false;
bool speedButtonHeld = false;
while(true)
{
......@@ -115,7 +123,7 @@ void runMotor()
// is just being held down.
if(checkButton(RUN_BUTTON) == LOW)
{
if(!buttonHeld)
if(!runButtonHeld)
{
if(runMotor)
runMotor = false;
......@@ -127,12 +135,24 @@ void runMotor()
}
}
buttonHeld = true;
runButtonHeld = true;
}
else
buttonHeld = false;
{
runButtonHeld = false;
}
// Read speed button with no bounce (fix me later)
if(digitalRead(SPEED_BUTTON) == LOW)
{
if(motorSpeedPct == MOTOR_SPEED_LOW_PCT)
motorSpeedPct = MOTOR_SPEED_HIGH_PCT;
else
motorSpeedPct = MOTOR_SPEED_LOW_PCT;
}
if(runMotor)
controlMotor(rpm, directionInterval);
controlMotor(motorSpeedPct, directionInterval);
else
controlMotor(0, 0);
}
......@@ -222,8 +242,9 @@ void controlMotor(byte speed, byte directionInterval)
int toSpeed(byte value)
{
// Based on RPM values
// return map(value, 0, MOTOR_RPM, 0, 255);
// Changing to percent over raw RPMs
// Based on percentages
return map(value, 0, 100, 0, 255);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment