I am using the following code (thanks to Oddbot from letsmakerobots.com) to run a 48 servos at the same time off an Arduino Mega. However, I am wanting to add a pushbutton that can both start the code running and stop it (returning all the servos back to their starting position). Any help would be greatly appreciated!
#include <Servo.h>
#define howmanyservos 48
int uSmin=500;
int uSmax=2400;
int servoposition[howmanyservos];
int servodirection[howmanyservos];
The way the code is now it runs the servos without stopping. But to be able to respond to a push button it is necessary to move the servos one step at a time and check the button between steps.
You also need some way to know whether the button press is intended to start or stop the servos - that means there needs to be a variable (perhaps called runServos) which causes the servos to move when it is true and to stop when it is false.
Perhaps use an interrupt to toggle the state of a boolean variable. In your loop, run the servos as you have in your current sketch if the variable is in one condition and write code to return them to their starting condition if the variable is in the opposite condition. - Scotty
// define global variables #define howmanyservos 48
int uSmin=500;
int uSmax=2400;
int servoposition[howmanyservos];
int servodirection[howmanyservos];
int button = 10;
int press = 0;
The use of code tags is not just for the hell of it. Do you see that your posted code changed to italics in the middle?- that’s because in your code you have the letter i in square brackets and the browser interprets that as a switch to italics, and humans no longer see the i in the brackets. Often code will end up with a 8) in the middle since that’s an 8 followed by a ). The use of code tags prevents that.
When you post code you should select it and then hit the </> icon above the smiley, then your code: