Help with Multiple Servo Activation

Hello…I need help…and I don’t think it is a difficult issue, but I just can’t seem to get it yet.

I am trying to push a button that makes a servo move 90 degrees for 10 seconds, then turn back. I have this working fine. The problem happens when I try to do this for more than one servo.

I can make it work for two servos, but not while the first one is still counting off its 10 second period.

Here is my code…CAN SOMEONE GUIDE ME TO A SOLUTION?

/*
SweepTwoServos
By Philip van Allen <philvanallen.com> for the VarSpeedServo.h library (October 2013)
This example code is in the public domain
*/

#include <VarSpeedServo.h>

VarSpeedServo myservo_1; // create servo object to control a servo
VarSpeedServo myservo_2; //a maximum of eight servo objects can be created

const int servoPin1 = 9; // the digital pin used for the first servo
const int servoPin2 = 10; // the digital pin used for the second servo

unsigned long startTime_1 = 0; // will store last time servo was opened
unsigned long startTime_2 = 0; // will store last time servo was opened
int valveOpenPeriod_1 = 10000; // sets the period the servo should be opened - 10 seconds
int valveOpenPeriod_2 = 5000; // sets the period the servo should be opened - 5 seconds

void setup() {
myservo_1.attach(servoPin1); // attaches the servo on pin 9 to the servo object
myservo_1.write(90,30,false); // set the intial position of the servo, move slowly, run in background
myservo_2.attach(servoPin2); // attaches the servo on pin 10 to the servo object
myservo_2.write(90,30,false); // set the intial position of the servo, move slowly, run in background
}

void loop()
{
unsigned long currentMillis = millis(); // start counting

if(digitalRead(3) == HIGH) // if inflation button is pushed
{
startTime_1 = currentMillis; // reset start time to current time
while(millis() - startTime_1 < valveOpenPeriod_1)
{
myservo_1.write(180,30,false); // tell servo to go to position 180 to open valve
delay(10); // waits 10ms for the servo to reach the position
}
myservo_1.write(90,30,false); // tell servo to go to back to original position 90 to close valve
delay(10); // waits 10ms for the servo to reach the position
}

/* THE PROBLEM
The two blocks of code, one above and one below, work independently but not simultaneously.
How do I get them to work simultaneously. In other words, I need each servo to move 90 degrees
for a period of time, then move back to its original position. I need this to work for
each servo whenever I press the putton.
*/

if(digitalRead(5) == HIGH) // if inflation button is pushed
{
startTime_2 = currentMillis; // reset start time to current time
while(millis() - startTime_2 < valveOpenPeriod_2)
{
myservo_2.write(180,127,true); // tell servo to go to position 180 to open valve
delay(10); // waits 10ms for the servo to reach the position
}
myservo_2.write(90,127,true); // tell servo to go to position 90 to close valve
delay(10); // waits 10ms for the servo to reach the position
}
}

You really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :slight_smile:

I am trying to push a button that makes a servo move 90 degrees for 10 seconds, then turn back.

Moving a servo 90 degrees for 10 seconds doesn't make sense. Does that mean that the servo should move 90 degrees in 10 seconds? Or does it mean that the servo should slam 90 degrees and then stay there for 10 seconds? Or, does it mean that 10 seconds from the time the servo starts moving slowly, for an turn of 90 degrees, the servo should start moving 90 degrees in the opposite direction?

Once we know what you want to do, we can help you write the code to do that.