multiple servo pushbutton stop/start

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];

Servo servo[howmanyservos];

void setup()
{
for (int i=0;i<howmanyservos;i++)
{
servoposition*=500; *
_ servodirection*=20-i; _
_ servo.attach(53-i,uSmin,uSmax);
servo.writeMicroseconds(500);
delay(25);
}
}
void loop()
{
for (int i=0;i<howmanyservos;i++)
{
servoposition+=servodirection;
if (servoposition>uSmax || servoposition<uSmin) servodirection=-servodirection;
servo.writeMicroseconds(servoposition);
}
delay(25);
}*_

You need to make a few changes.

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.

Have a look at the code in several things at a time and in planning and implementing a program.

…R

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

so kind of like this?

#include <Servo.h>

// define global variables
#define howmanyservos 48
int uSmin=500;
int uSmax=2400;
int servoposition[howmanyservos];
int servodirection[howmanyservos];
int button = 10;
int press = 0;

// define servos
Servo servo[howmanyservos];
boolean toggle = true;

void setup()
{
pinMode(button, INPUT);
for (int i=0;i<howmanyservos;i++)
{
servoposition*=500; *
_ servodirection*=20-i; _
_ servo.attach(3-i,uSmin,uSmax);
servo.writeMicroseconds(1500);
delay(25);
}
}
void loop()
{
press = digitalRead(button);
if (press == LOW)
{
if(toggle)
{
for (int i=0;i<howmanyservos;i++)
{
servoposition+=servodirection;
if (servoposition>uSmax || servoposition<uSmin) servodirection=-servodirection;
servo.writeMicroseconds(servoposition);
}
delay(25);
}
else
{
for (int i=0;i<howmanyservos;i++)
{
servoposition=500;
servo.writeMicroseconds(servoposition);*_

* toggle = !toggle;*
* }*
* }*
* }*
}

CODE TAGS
Yes, I don't see any obvious errors, so try it and see! :grin:

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 :slight_smile: smiley, then your code:

will
{
look
}
like this

You are not referring to your arrays properly. This code

void setup()
{
    pinMode(button, INPUT);
    for (int i=0;i<howmanyservos;i++)                                                           
    { 
        servoposition=500;                                                                     
        servodirection=20-i;                                                                     
        servo.attach(3-i,uSmin,uSmax);                                                         
        servo.writeMicroseconds(1500);                                                         
        delay(25);                                                                                 
    }
}

should be

void setup()
{
    pinMode(button, INPUT);
    for (int i=0;i<howmanyservos;i++)                                                           
    { 
        servoposition[i] = 500;                                                                     
        servodirection[i] = 20-i;                                                                     
        servo[i].attach(3 - i, uSmin ,uSmax);                                                         
        servo[i].writeMicroseconds(1500);                                                         
        delay(25);                                                                                 
    }
}

and there will need to be equivalent changes throughout the program

I don’t think the calculation 3 - i will give you the correct pin references - perhaps you need i + 3

And start testing with the variable howmanyservos set at 2

While the compiler does not care, a little white-space makes code much more readable.

…R