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
}
}