I am trying to program a loop for my arduino uno with modkit. I created the code below, but it seems to be doing all the commands at once (fwd/rev). I would like the motor to go fwd, stop when the button is pressed (pause for xx seconds), then go in reverse. I would like it to then repeat this for xx loops. Does anyone know how to get around this? I guess my ultimate question is how, do I make step 1, step 2, etc not everything all at once?
forever{
- motorOn(MOTOR1,FWD);
- motorSpeed(MOTOR1,100);
- if(buttonPressed(BUTTON1)){
-
-
- }
- motorOn(MOTOR1,REV);
- motorSpeed(MOTOR1,100);
}
thanks,
Jeff
Right, we forgot to add another condition for changing between the reverse direction and the forward direction. Did you want this to be done when the button is pressed again? If so, something like this should work:
forever{
* motorOn(MOTOR1,FWD);
* motorSpeed(MOTOR1,100);
* while (!buttonPressed(BUTTON1)){
* * // Do nothing until button pressed
* }
* motorOff(MOTOR1);
* delay(2000);
* motorOn(MOTOR1,REV);
* motorSpeed(MOTOR1,100);
* while (!buttonPressed(BUTTON1)){
* * // Do nothing until button pressed
* }
* motorOff(MOTOR1);
* delay(2000);
}
If you want this to loop only a specific amount of times, you would want to use the “repeat X” structure instead of “forever”. If this code segment is in the Arduino’s main “loop()” function, you will need to add an empty
forever{
* // Do nothing anymore
}
block at the end to stop the program from restarting all over.
Hope this helps,
Hi Jeff,
I’m not very familiar with ModKit, but here’s what I would try to get the behaviour you are discussing:
forever{
- motorOn(MOTOR1,FWD);
- motorSpeed(MOTOR1,100);
- while (!buttonPressed(BUTTON1)){
-
- // Do nothing until button pressed
- }
- motorOff(MOTOR1);
- delay(2000);
- motorOn(MOTOR1,REV);
- motorSpeed(MOTOR1,100);
}
Hope this helps,
Thanks for your help Jeffrey. Unfortunately, I am still getting the same thing as before. The motor goes forward, delays when the button is pressed, then continues forward again. Should I be inserting a “break” command somewhere so it proceeds to the reverse command?
-Jeff