Go to 3rd and velocity and stay until switch is turned OFF
Also, IF switch is ever turned OFF start loop from beginning
What is happening:
First portion of IF working
However, code in NOT restarting if switch is tuned off while at first 2 settings.
/*
TekTips1 - program to highlight Arduino wiring to a Teknic ClearPath integrated servo motor
Author is Abe Amirana, 7/10/2017
Program uses a "MCVC" (Motion Control Velocity Control) ClearPath servo motor
Operating Mode is the "Ramp Up/Down to Selected Velocity" (uses 2 input bits to control 4 velocities)
- View operational mode video for further information on this specific Operational Mode:
https://www.teknic.com/watch-video/#Opmode5
TekTips Video: Wiring a ClearPath motor to an Arduino micro controller
www.youtube.com/blahblahblah
*/
//Wire colors refer to a standard Teknic ClearPath controller cable
const int Enable = 6; // ClearPath ~enable input; +enable = BLU wire; -enable = ORN wire
const int InputA = 8; // ClearPath Input A; +InputA = WHT wire; -InputA is BRN wire
const int InputB = 9; // ClearPath Input B; +InputB = BLK wire; -InputB = YEL wire
const int HLFB = 4; // ClearPath HLFB Output; +HLFB = GRN wire; -HLFB = RED wire
const int myLED = 13; // Arduino on-board LED
const int switchPin = 2;
int switchState = 0;
int MoveTime = 10000; // desired time for ramp and constant velocity
// ============================================================================================
// put your setup code here, to run once:
void setup()
{
pinMode(Enable, OUTPUT);
pinMode(InputA, OUTPUT);
pinMode(InputB, OUTPUT);
//pinMode(HLFB, INPUT_PULLUP);
pinMode(HLFB, INPUT);
pinMode(myLED, OUTPUT);
pinMode(switchPin, INPUT);
// start off by ensuring thet the motor is disabled before proceeding
digitalWrite(Enable, LOW);
delay(10);
// set the two outputs to their initial states
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(10);
// toggle ~enable, read HLFB & set on-board LEDs - this clears any safety shutdowns & activates motor's power stage
digitalWrite(Enable, HIGH);
delay(20);
if (digitalRead(HLFB) == LOW)
{
digitalWrite(myLED, HIGH);
delay(1000);
}
digitalWrite(Enable, LOW);
delay(20);
if (digitalRead(HLFB) == HIGH)
{
digitalWrite(myLED, LOW);
delay(1500);
}
}
// end of setup code
// ============================================================================================
// ============================================================================================
// put your main code here:
void loop()
{
//command to check switch state and electrical state
switchState = digitalRead(switchPin);
// command to enable the motor; then wait 1 sec
digitalWrite(Enable, HIGH);
delay(500);
//Begin cycling through velocities IF switch is ON
if (switchState == HIGH)
{
// command to the 1st velocity setting
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
delay(MoveTime);
// command to the 2nd velocity setting
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
delay(MoveTime);
do //go to 3rd velocity and stay until switch is turned off
{
digitalWrite(InputA, HIGH);
digitalWrite(InputB, HIGH);
switchState = digitalRead(switchPin);
}
while (switchState == HIGH);
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(250);
}
}
// end of main code:
// end of main code, will stay at final velocity until switch turned off:
// ============================================================================================
ok, first time trying to use millis(). I am really close I think. Right now A and B are NOT toggling as it switches velocity.
//Wire colors refer to a standard Teknic ClearPath controller cable
const int Enable = 6; // ClearPath ~enable input; +enable = BLU wire; -enable = ORN wire
const int InputA = 8; // ClearPath Input A; +InputA = WHT wire; -InputA is BRN wire
const int InputB = 9; // ClearPath Input B; +InputB = BLK wire; -InputB = YEL wire
const int HLFB = 4; // ClearPath HLFB Output; +HLFB = GRN wire; -HLFB = RED wire
const int myLED = 13; // Arduino on-board LED
const int switchPin = 2;
unsigned int startTime = 0;
unsigned int runTime = 0;
int MoveTime = 5000; // desired time for ramp and constant velocity
// ============================================================================================
// put your setup code here, to run once:
void setup()
{
pinMode(Enable, OUTPUT);
pinMode(InputA, OUTPUT);
pinMode(InputB, OUTPUT);
//pinMode(HLFB, INPUT_PULLUP);
pinMode(HLFB, INPUT);
pinMode(myLED, OUTPUT);
pinMode(switchPin, INPUT);
// start off by ensuring thet the motor is disabled before proceeding
digitalWrite(Enable, LOW);
delay(10);
// set the two outputs to their initial states
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(10);
// toggle ~enable, read HLFB & set on-board LEDs - this clears any safety shutdowns & activates motor's power stage
digitalWrite(Enable, HIGH);
delay(20);
if (digitalRead(HLFB) == LOW)
{
digitalWrite(myLED, HIGH);
delay(1000);
}
digitalWrite(Enable, LOW);
delay(20);
if (digitalRead(HLFB) == HIGH)
{
digitalWrite(myLED, LOW);
delay(1500);
}
}
// end of setup code
// ============================================================================================
// ============================================================================================
// put your main code here:
void loop()
{
digitalWrite(Enable,HIGH);
//Begin cycling through velocities IF switch is ON
if (digitalRead(2) == HIGH && startTime == 0)
{
startTime = millis(); //set startTime = to when switch is flipped
runTime = millis();//start counting from time switch is flipped
// command to the 1st velocity setting
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
if (digitalRead(2) == HIGH && startTime > MoveTime)
{
// command to the 2nd velocity setting IF switch == HIGH and startTime > MoveTime
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
}
do
{
//go to 3rd velocity IF switch == HIGH and > 12 seconds after switch is turned ON, stay until switch is OFF
digitalWrite(InputA, HIGH);
digitalWrite(InputB, HIGH);
}
while (digitalRead(2) == HIGH && runTime > 12000);
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
startTime = 0; //reset startTime
runTime = 0; //reset runTime
}
}
// end of main code
if (digitalRead(2) == HIGH && startTime == 0)
{
startTime = millis(); //set startTime = to when switch is flipped
runTime = millis();//start counting from time switch is flipped
// command to the 1st velocity setting
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
if (digitalRead(2) == HIGH && startTime > MoveTime)
{
// command to the 2nd velocity setting IF switch == HIGH and startTime > MoveTime
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
}
On any pass through loop(), when the switch pin is HIGH, what are reasonable values for startTime and runTime, when this code completes?
do
{
//go to 3rd velocity IF switch == HIGH and > 12 seconds after switch is turned ON, stay until switch is OFF
digitalWrite(InputA, HIGH);
digitalWrite(InputB, HIGH);
}
while (digitalRead(2) == HIGH && runTime > 12000);
Once the pin is set HIGH, it does not need to be set HIGH again. So, all this code does is set the pins HIGH, and wait until the switch goes LOW, assuming that runTime is greater than 12000.
If runTime is less than 12000, the pins will be set HIGH and the loop will end immediately.
In either case, what the code actually does hardly seems reasonable.
do/while is a useful construct about once or twice a millenium. I can think of very few cases in my 40 year career as a programmer that I used do/while. while is far more common, and usually the correct construct to use.
Put comments in your code that explain what the code is supposed to do. This makes it easier to do two things.
Check that the code actually does what the comment says.
Check that the comments explain a complete, logical story.
I need motor to start turning ONLY when switch is flipped
Motor to go 1st velocity (A = HIGH, B = LOW) and run for 10 seconds
Motor to go to 2nd velocity (A = LOW, B = HIGH) run for 10 seconds
After motor has gone through both speeds, go to 3rd velocity (A = HIGH, B =HIGH) and stay until switch is turned off
If at any point the switch the is turned OFF. Need loop to start from beginning when turned back ON
Thank you everyone for the help thus far.
I tried to clean up my comments.
const int Enable = 6; // ClearPath ~enable input; +enable = BLU wire; -enable = ORN wire
const int InputA = 8; // ClearPath Input A; +InputA = WHT wire; -InputA is BRN wire
const int InputB = 9; // ClearPath Input B; +InputB = BLK wire; -InputB = YEL wire
const int HLFB = 4; // ClearPath HLFB Output; +HLFB = GRN wire; -HLFB = RED wire
const int myLED = 13; // Arduino on-board LED
const int switchPin = 2;
unsigned int startTime = 0;
unsigned int runTime = 0;
int MoveTime = 5000; // desired time for ramp and constant velocity
// ============================================================================================
// put your setup code here, to run once:
void setup()
{
pinMode(Enable, OUTPUT);
pinMode(InputA, OUTPUT);
pinMode(InputB, OUTPUT);
//pinMode(HLFB, INPUT_PULLUP);
pinMode(HLFB, INPUT);
pinMode(myLED, OUTPUT);
pinMode(switchPin, INPUT);
// start off by ensuring thet the motor is disabled before proceeding
digitalWrite(Enable, LOW);
delay(10);
// set the two outputs to their initial states
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(10);
// toggle ~enable, read HLFB & set on-board LEDs - this clears any safety shutdowns & activates motor's power stage
digitalWrite(Enable, HIGH);
delay(20);
if (digitalRead(HLFB) == LOW)
{
digitalWrite(myLED, HIGH);
delay(1000);
}
digitalWrite(Enable, LOW);
delay(20);
if (digitalRead(HLFB) == HIGH)
{
digitalWrite(myLED, LOW);
delay(1500);
}
}
// end of setup code
// ============================================================================================
// ============================================================================================
// put your main code here:
void loop()
{
digitalWrite(Enable,HIGH);
//Begin cycling through velocities IF switch is ON
if (digitalRead(2) == HIGH && startTime == 0)
{
startTime = millis(); //set startTime = millis() time when switch is flipped
runTime = millis();//set runTime = millis() counting from time switch is flipped
// command to the 1st velocity setting
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
if (digitalRead(2) == HIGH && startTime > 10000)
{
// command to the 2nd velocity setting if switch is ON and already ran at first velocity for 10 seconds
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
}
while (digitalRead(2) == HIGH && runTime > 20000)
{
//go to 3rd velocity IF switch is ON and already run at velocity 2 for 10 seconds and stay at 3rd velocity until switch is turned OFF
digitalWrite(InputA, HIGH);
digitalWrite(InputB, HIGH);
}
// IF switch is turned OFF, turn OFF both inputs and reset timers to allow code to reset if turned back on
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
startTime = 0; //reset startTime
runTime = 0; //reset runTime
}
}
// end of main code
Until you better understand what you are, stop using compound ifs. Used ONLY nested ifs.
Apparently, the trigger for the action you want to perform is the switch becoming pressed. So, write the code that way.
int currState = LOW;
int prevState = LOW;
void loop()
{
currState = digitalRead(2);
if(currState != prevState)
{
// Some change to the switch state happened -
// from pressed to released or from released to pressed.
if(currState == HIGH)
{
// The change is to pressed
runTheMotor();
}
}
}
Notice that this part of the code is not the least bit concerned about how to make the motor move, how fast, or for how long.
Get this part tested and working, with nothing but a stub for the function.
void runTheMotor()
{
}
When you KNOW that the switch is wired correctly (why you are not using the internal pullup resistor and it's simpler wiring is a mystery), THEN figure out exactly what "run the motor" means. When you know what it means, it will be nearly trivial to write the code to make it happen.
Paul S, thank you for the feedback, but I am really struggling. I think I am trying to change too much at once I think.
In my original code, I verified that:
Loop would not start until switch was ON
Motor would cycle through first 2 speeds spending some defined amount of time at each speed
Motor would go to 3rd speed and stay at this speed as long as switch remained ON
(so switch is working)
However, if switch was turned OFF at 1st 2 speeds, it would still run until it got to 3rd speed then shut off. I want the motor to stop anytime the switch is turned OFF and restart at speed 1 when turned back ON.
I now understand that the delay() function was preventing the code from realizing the switch was turned OFF. I tried to replace delay() with millis() with no joy.
I tried to replace delay() with millis() with no joy.
And that code is?
Using a clock instead of a timer is not a matter of calling a different function. It is a completely different mindset.
In any case, I'd still like to see you confine the "run the motor" code to a function. That function CAN check the switch and return any time the switch is turned off (after stopping the motor, of course). The function can run to completion if the switch remains on for the whole time.
Putting the "run the motor" code in a separate function means that you can comment that function, and we can look at just that function to see what you are doing wrong.
You'll see, then, that runAtSpeedOne(), runAtSpeedTwo(), and runAtSpeedThree() are useful methods to call. They can be called conditionally (on the switch still being on), returning, after shutting the motor off, if necessary, if the switch is turned off while they are executing.
I never try to write complex code like yours in one function. I create stubs, and loads of comments, to help me organize the code, and break it down into pieces my feeble mind can comprehend at a glance.
Here is my original code. I will try and re-write per your recommendation in the meantime. Thanks so much for your assistance.
//Wire colors refer to a standard Teknic ClearPath controller cable
const int Enable = 6; // ClearPath ~enable input; +enable = BLU wire; -enable = ORN wire
const int InputA = 8; // ClearPath Input A; +InputA = WHT wire; -InputA is BRN wire
const int InputB = 9; // ClearPath Input B; +InputB = BLK wire; -InputB = YEL wire
const int HLFB = 4; // ClearPath HLFB Output; +HLFB = GRN wire; -HLFB = RED wire
const int myLED = 13; // Arduino on-board LED
const int switchPin = 2;
int switchState = 0;
int MoveTime = 10000; // desired time for ramp and constant velocity
// ============================================================================================
// put your setup code here, to run once:
void setup()
{
pinMode(Enable, OUTPUT);
pinMode(InputA, OUTPUT);
pinMode(InputB, OUTPUT);
//pinMode(HLFB, INPUT_PULLUP);
pinMode(HLFB, INPUT);
pinMode(myLED, OUTPUT);
pinMode(switchPin, INPUT);
// start off by ensuring thet the motor is disabled before proceeding
digitalWrite(Enable, LOW);
delay(10);
// set the two outputs to their initial states
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(10);
// toggle ~enable, read HLFB & set on-board LEDs - this clears any safety shutdowns & activates motor's power stage
digitalWrite(Enable, HIGH);
delay(20);
if (digitalRead(HLFB) == LOW)
{
digitalWrite(myLED, HIGH);
delay(1000);
}
digitalWrite(Enable, LOW);
delay(20);
if (digitalRead(HLFB) == HIGH)
{
digitalWrite(myLED, LOW);
delay(1500);
}
}
// end of setup code
// ============================================================================================
// ============================================================================================
// put your main code here:
void loop()
{
//command to check switch state and electrical state
switchState = digitalRead(switchPin);
// command to enable the motor; then wait 1 sec
digitalWrite(Enable, HIGH);
delay(500);
//Begin cycling through velocities IF switch is ON
if (switchState == HIGH)
{
// command to the 1st velocity setting
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
delay(MoveTime);
// command to the 2nd velocity setting
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
delay(MoveTime);
do //go to 3rd velocity and stay until switch is turned off
{
digitalWrite(InputA, HIGH);
digitalWrite(InputB, HIGH);
switchState = digitalRead(switchPin);
}
while (switchState == HIGH);
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(250);
}
}
// end of main code:
// end of main code, will stay at final velocity until switch turned off:
// ============================================================================================
ok, revisited millis() usage and now it WORKS as desired. Thank you everyone for your time and expertise.
/*
TekTips1 - program to highlight Arduino wiring to a Teknic ClearPath integrated servo motor
Author is Abe Amirana, 7/10/2017
Program uses a "MCVC" (Motion Control Velocity Control) ClearPath servo motor
Operating Mode is the "Ramp Up/Down to Selected Velocity" (uses 2 input bits to control 4 velocities)
- View operational mode video for further information on this specific Operational Mode:
https://www.teknic.com/watch-video/#Opmode5
TekTips Video: Wiring a ClearPath motor to an Arduino micro controller
www.youtube.com/blahblahblah
*/
const int Enable = 6; // ClearPath ~enable input; +enable = BLU wire; -enable = ORN wire
const int InputA = 8; // ClearPath Input A; +InputA = WHT wire; -InputA is BRN wire
const int InputB = 9; // ClearPath Input B; +InputB = BLK wire; -InputB = YEL wire
const int HLFB = 4; // ClearPath HLFB Output; +HLFB = GRN wire; -HLFB = RED wire
const int myLED = 13; // Arduino on-board LED
const int switchPin = 2;
unsigned int startTime = 0;
unsigned int runTime = 0;
int MoveTime = 5000; // desired time for ramp and constant velocity
// ============================================================================================
// put your setup code here, to run once:
void setup()
{
pinMode(Enable, OUTPUT);
pinMode(InputA, OUTPUT);
pinMode(InputB, OUTPUT);
//pinMode(HLFB, INPUT_PULLUP);
pinMode(HLFB, INPUT);
pinMode(myLED, OUTPUT);
pinMode(switchPin, INPUT);
// start off by ensuring thet the motor is disabled before proceeding
digitalWrite(Enable, LOW);
delay(10);
// set the two outputs to their initial states
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(10);
// toggle ~enable, read HLFB & set on-board LEDs - this clears any safety shutdowns & activates motor's power stage
digitalWrite(Enable, HIGH);
delay(20);
if (digitalRead(HLFB) == LOW)
{
digitalWrite(myLED, HIGH);
delay(1000);
}
digitalWrite(Enable, LOW);
delay(20);
if (digitalRead(HLFB) == HIGH)
{
digitalWrite(myLED, LOW);
delay(1500);
}
}
// end of setup code
// ============================================================================================
// ============================================================================================
// put your main code here:
void loop()
{
digitalWrite(Enable,HIGH);
//Begin cycling through velocities IF switch is ON
if (digitalRead(2) == HIGH && startTime == 0)
{
startTime = millis(); //set startTime = millis() time when switch is flipped
runTime = millis();//set runTime = millis() counting from time switch is flipped
// command to the 1st velocity setting
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
while (digitalRead(2) == HIGH && millis() - startTime < 10000)
{
}
while (digitalRead(2) == HIGH && millis() - startTime < 20000)
{
// command to the 2nd velocity setting if switch is ON and already ran at first velocity for 10 seconds
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
}
while (digitalRead(2) == HIGH)
{
//go to 3rd velocity IF switch is ON and already run at velocity 2 for 10 seconds and stay at 3rd velocity until switch is turned OFF
digitalWrite(InputA, HIGH);
digitalWrite(InputB, HIGH);
}
}
// IF switch is turned OFF, turn OFF both inputs and reset timers to allow code to reset if turned back on
if (digitalRead(2) == LOW)
{
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
startTime = 0; //reset startTime
runTime = 0; //reset runTime
}
}
// end of main code
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
while (digitalRead(2) == HIGH && millis() - startTime < 10000)
{
}
This is correct. There is nothing to do in the body of the loop except wait.
while (digitalRead(2) == HIGH && millis() - startTime < 20000)
{
// command to the 2nd velocity setting if switch is ON and already ran at first velocity for 10 seconds
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
}
This is wrong. There is no reason to keep banging on the pins. The do not need to be reminded millions of times per minute to stay at the prescribed state.
Wrote functions to clean up code.
Cleaned up WHILE loops to constantly bang on pins
Wrote println functions to display what speed motor is at
Current status:
Motor waits till switch is ON to begin rotating
If switch is turned OFF motor will start from speed1 when turned back on
Problem
If switch is turned OFF, say at speed1, code prints speed1, speed2, speed3
AND after some amount of time if switch is turned ON it goes directly to speed3 rather than cycle through
It's like startTime is not being reset to millis()
const int Enable = 6; // ClearPath ~enable input; +enable = BLU wire; -enable = ORN wire
const int InputA = 8; // ClearPath Input A; +InputA = WHT wire; -InputA is BRN wire
const int InputB = 9; // ClearPath Input B; +InputB = BLK wire; -InputB = YEL wire
const int HLFB = 4; // ClearPath HLFB Output; +HLFB = GRN wire; -HLFB = RED wire
const int myLED = 13; // Arduino on-board LED
const int switchPin = 2;
int switchState = 0;
unsigned int startTime = 0;
//set up speed setting functions
void speed1() //turn A ON and B OFF, print speed
{
Serial.print("Speed1");
Serial.println();
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
}
void speed2() //turn A OFF and B ON, print speed
{
Serial.print("Speed2");
Serial.println();
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
}
void speed3() //turn A and B ON, print speed
{
Serial.print("Speed3");
Serial.println();
digitalWrite(InputA, HIGH);
digitalWrite(InputB, HIGH);
}
void speed0()//turn A and B OFF, print speed
{
Serial.print("Speed0");
Serial.println();
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
}
// ============================================================================================
// put your setup code here, to run once:
void setup()
{
pinMode(Enable, OUTPUT);
pinMode(InputA, OUTPUT);
pinMode(InputB, OUTPUT);
//pinMode(HLFB, INPUT_PULLUP);
pinMode(HLFB, INPUT);
pinMode(myLED, OUTPUT);
pinMode(switchPin, INPUT);
// start off by ensuring thet the motor is disabled before proceeding
digitalWrite(Enable, LOW);
delay(10);
// set the two outputs to their initial states
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(10);
// toggle ~enable, read HLFB & set on-board LEDs - this clears any safety shutdowns & activates motor's power stage
digitalWrite(Enable, HIGH);
delay(20);
if (digitalRead(HLFB) == LOW)
{
digitalWrite(myLED, HIGH);
delay(1000);
}
digitalWrite(Enable, LOW);
delay(20);
if (digitalRead(HLFB) == HIGH)
{
digitalWrite(myLED, LOW);
delay(1500);
}
Serial.begin(9600);
}
// end of setup code
// ============================================================================================
// ============================================================================================
// put your main code here:
void loop()
{
//turn on enable signal
digitalWrite(Enable,HIGH);
//read switch pin
digitalRead(2);
//while switch is ON start cycling through speeds
while (digitalRead(2) == HIGH)
{
startTime = millis(); //track time from when switch is ON
//go to 1st speed
speed1();
//run at speed1 for 1 sec then switch to speed2
while (digitalRead(2) == HIGH && millis() - startTime < 1000)
{
}
//go to 2nd speed
speed2();
//run at speed2 for 1 sec then run at speed3
while (digitalRead(2) == HIGH && millis() - startTime < 2000)
{
}
//go to 3rd speed
speed3();
//stay at speed3 until switch is turned OFF
while (digitalRead(2) == HIGH && millis() - startTime >= 2000)
{
}
}
//while switch is OFF, set startTime = millis() and disable both outputs (speed0)
startTime = millis(); //reset startTime
speed0();
while (digitalRead(2) == LOW)
{
}
}
// end of main code
//while switch is ON start cycling through speeds
while (digitalRead(2) == HIGH)
{
While? Why should the process repeat over and over? It seems to me that you would want to start the process when the switch becomes pressed.
//stay at speed3 until switch is turned OFF
while (digitalRead(2) == HIGH && millis() - startTime >= 2000)
{
}
So, why is time a factor here at all?
//while switch is OFF, set startTime = millis() and disable both outputs (speed0)
startTime = millis(); //reset startTime
speed0();
It seems strange to store the end time in a variable called startTime.
I think you need to look at the state change detection example, and learn to write code that runs when a switch changes state, and quit overusing the while statement.
Thanks again for the feedback. I have cleaned up the code a bit.
Reviewed change state. My problem is NOT getting the code to recognize if/when the switch is ON.
Perhaps I need to be more clear in what is desired and what is occurring. I really think there is some anomaly here.
DESIRED OPERATION:
With switch ON: motor begins cycling through speeds. Starting @ speed1
Go to speed1 and stay for 1 sec (short time is for troubleshooting, will be set to 5 min on final)
Go to speed2 and stay for 1 sec (short time is for troubleshooting, will be set to 5 min on final)
Go to speed3 and stay until switch turned OFF
With switch OFF: both inputs are toggled LOW and motor does NOT spin.
PROBLEM DESCRIPTION:
Initially motor responds as desired.
Turns OFF when switch is OFF
Starts at speed1 when turned ON
After approximately 70 seconds
Motor starts directly at speed3 and will do so every time the switch is operated until board is RESET
/*
DESIRED OPERATION:
With switch ON: motor begins cycling through speeds.
- Go to speed1 and stay for 1 sec (short time is for troubleshooting, will be set to 5 min on final)
- Go to speed2 and stay for 1 sec (short time is for troubleshooting, will be set to 5 min on final)
- Go to speed3 and stay until switch turned OFF
With switch OFF: both inputs are toggled LOW and motor does NOT spin.
PROBLEM DESCRIPTION:
- Initially motor responds as desired.
- Turns OFF when switch is OFF
- Starts at speed1 when turned ON
- After approximately 70 seconds
- Motor starts directly at speed3 and will do so every time the switch is operated until board is RESET
*/
const int Enable = 6; // ClearPath ~enable input; +enable = BLU wire; -enable = ORN wire
const int InputA = 8; // ClearPath Input A; +InputA = WHT wire; -InputA is BRN wire
const int InputB = 9; // ClearPath Input B; +InputB = BLK wire; -InputB = YEL wire
const int HLFB = 4; // ClearPath HLFB Output; +HLFB = GRN wire; -HLFB = RED wire
const int myLED = 13; // Arduino on-board LED
const int switchPin = 2;
unsigned int time = 0;//variable to track time since switch is turned ON
bool switchState = false;
//set up speed setting functions
void speed1() //turn A ON and B OFF and print to serial monitor
{
Serial.print("Speed1");
Serial.println();
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
}
void speed2() //turn A OFF and B ON and print to serial monitor
{
Serial.print("Speed2");
Serial.println();
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
}
void speed3() //turn A and B ON and print to serial monitor
{
Serial.print("Speed3");
Serial.println();
digitalWrite(InputA, HIGH);
digitalWrite(InputB, HIGH);
}
void off()//turn A and B OFF and print to serial monitor
{
Serial.print("OFF");
Serial.println();
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
}
// ============================================================================================
// put your setup code here, to run once:
void setup()
{
pinMode(Enable, OUTPUT);
pinMode(InputA, OUTPUT);
pinMode(InputB, OUTPUT);
//pinMode(HLFB, INPUT_PULLUP);
pinMode(HLFB, INPUT);
pinMode(myLED, OUTPUT);
pinMode(switchPin, INPUT);
// start off by ensuring thet the motor is disabled before proceeding
digitalWrite(Enable, LOW);
delay(10);
// set the two outputs to their initial states
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(10);
// toggle ~enable, read HLFB & set on-board LEDs - this clears any safety shutdowns & activates motor's power stage
digitalWrite(Enable, HIGH);
delay(20);
if (digitalRead(HLFB) == LOW)
{
digitalWrite(myLED, HIGH);
delay(1000);
}
digitalWrite(Enable, LOW);
delay(20);
if (digitalRead(HLFB) == HIGH)
{
digitalWrite(myLED, LOW);
delay(1500);
}
Serial.begin(9600);//enable comms to serial monitor
}
// end of setup code
// ============================================================================================
// ============================================================================================
// put your main code here:
void loop(){
digitalWrite(Enable,HIGH);//turn ON enable signal
if (digitalRead(2)==HIGH)//read switch pin and begin cycling through speeds if ON
{
time=millis();//start tracking time from when switch is ON
speed1(); //go to speed1
while (digitalRead(2)==HIGH && millis()-time<1000); //as long as switch is ON stay at speed 1 for 1 second
speed2();//go to speed2
while (digitalRead(2)==HIGH && millis()-time<2000);//as long as switch is ON stay at speed 2 for 1 second
speed3();//go to speed3
while (digitalRead(2)==HIGH);//stay at speed3 as long as switch is ON
}
off(); //disable both inputs as long as switch is OFF
while (digitalRead(2) == LOW);
}
// end of main code
OMG! That's it! I can't believe that has been it this entire time.
Thank you Paul. You are the man!
Correct code below
/*
With switch ON: motor begins cycling through speeds.
- Go to speed1 and stay for 1 sec (short time is for troubleshooting, will be set to 5 min on final)
- Go to speed2 and stay for 1 sec (short time is for troubleshooting, will be set to 5 min on final)
- Go to speed3 and stay until switch turned OFF
With switch OFF: both inputs are toggled LOW and motor does NOT spin.
*/
const int Enable = 6; // ClearPath ~enable input; +enable = BLU wire; -enable = ORN wire
const int InputA = 8; // ClearPath Input A; +InputA = WHT wire; -InputA is BRN wire
const int InputB = 9; // ClearPath Input B; +InputB = BLK wire; -InputB = YEL wire
const int HLFB = 4; // ClearPath HLFB Output; +HLFB = GRN wire; -HLFB = RED wire
const int myLED = 13; // Arduino on-board LED
unsigned long time = 0;//variable to track time since switch is turned ON
//set up speed setting functions
void speed1() //turn A ON and B OFF and print to serial monitor
{
Serial.print("Speed1");
Serial.println();
digitalWrite(InputA, HIGH);
digitalWrite(InputB, LOW);
}
void speed2() //turn A OFF and B ON and print to serial monitor
{
Serial.print("Speed2");
Serial.println();
digitalWrite(InputA, LOW);
digitalWrite(InputB, HIGH);
}
void speed3() //turn A and B ON and print to serial monitor
{
Serial.print("Speed3");
Serial.println();
digitalWrite(InputA, HIGH);
digitalWrite(InputB, HIGH);
}
void off()//turn A and B OFF and print to serial monitor
{
Serial.print("OFF");
Serial.println();
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
}
// ============================================================================================
// put your setup code here, to run once:
void setup()
{
pinMode(Enable, OUTPUT);
pinMode(InputA, OUTPUT);
pinMode(InputB, OUTPUT);
//pinMode(HLFB, INPUT_PULLUP);
pinMode(HLFB, INPUT);
pinMode(myLED, OUTPUT);
// start off by ensuring thet the motor is disabled before proceeding
digitalWrite(Enable, LOW);
delay(10);
// set the two outputs to their initial states
digitalWrite(InputA, LOW);
digitalWrite(InputB, LOW);
delay(10);
// toggle ~enable, read HLFB & set on-board LEDs - this clears any safety shutdowns & activates motor's power stage
digitalWrite(Enable, HIGH);
delay(20);
if (digitalRead(HLFB) == LOW)
{
digitalWrite(myLED, HIGH);
delay(1000);
}
digitalWrite(Enable, LOW);
delay(20);
if (digitalRead(HLFB) == HIGH)
{
digitalWrite(myLED, LOW);
delay(1500);
}
Serial.begin(9600);//enable comms to serial monitor
}
// end of setup code
// ============================================================================================
// ============================================================================================
// put your main code here:
void loop(){
digitalWrite(Enable,HIGH);//turn ON enable signal
if (digitalRead(2)==HIGH)//read switch pin and begin cycling through speeds if ON
{
time=millis();//start tracking time from when switch is ON
speed1(); //go to speed1
while (digitalRead(2)==HIGH && millis()-time<1000); //as long as switch is ON stay at speed 1 for 1 second
speed2();//go to speed2
while (digitalRead(2)==HIGH && millis()-time<2000);//as long as switch is ON stay at speed 2 for 1 second
speed3();//go to speed3
while (digitalRead(2)==HIGH);//stay at speed3 as long as switch is ON
}
off(); //disable both inputs as long as switch is OFF
while (digitalRead(2) == LOW);
}
// end of main code