Purpose is to drive 2 servo's in sweep modus but at differing speeds, same min and max angle settings.
No matter what value is given to the intervals (speed settings) for the two servo's, they keep sweeping synchronous. But the one set at the slower speed does not make the full left and right angles, so although its speed is lower, they both go back and forth at the same time.
What could be wrong?
Edit: I think solved: I used the same incr variable for both servo's. The bool incr must be split up in a bool incr1 and bool incr2.
Edit 2: no not solved yet.
/* Sweep
Sweep without delay and with assymetric back/forth speed
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo servo1; // create servo object to control a servo
Servo servo2;
unsigned long previousMillis1a = 0;
unsigned long previousMillis1b = 0;
unsigned long previousMillis2a = 0;
unsigned long previousMillis2b = 0;
unsigned long currentMillis;
int interVal1a = 20;
int interVal1b = 20;
int interVal2a = 20;
int interVal2b = 20;
int minPos = 20;
int maxPos = 160;
int pos; // variable to store the servo position
bool incr1 = true; // increasing angle or decreasing angle servo 1
bool incr2 = true; // increasing angle or decreasing angle servo 2
void setup() {
pos = minPos; // reset servo to minimal angle
servo1.attach(9); // attaches the servo on pin 9 to the servo object
servo1.write(pos); // tell servo to go to position in variable 'pos'
servo2.attach(10); // attaches the servo on pin 10 to the servo object
servo2.write(pos);
}
void loop() {
currentMillis = millis();
servo_1();
servo_2();
servo_3(); // not yet used
}
void servo_1() {
if (incr1 == true && currentMillis - previousMillis1a >= interVal1a) {
pos = pos + 1;
servo1.write(pos); // tell servo to go to position in variable 'pos'
previousMillis1a = currentMillis;
if (pos == maxPos) {
incr1 = false;
};
}
if (incr1 == false && currentMillis - previousMillis1b >= interVal1b) {
pos = pos - 1;
servo1.write(pos); // tell servo to go to position in variable 'pos'
previousMillis1b = currentMillis;
if (pos == minPos) {
incr1 = true;
};
}
}
void servo_2() {
if (incr2 == true && currentMillis - previousMillis2a >= interVal2a) {
pos = pos + 1;
servo2.write(pos); // tell servo to go to position in variable 'pos'
previousMillis2a = currentMillis;
if (pos == maxPos) {
incr2 = false;
};
}
if (incr2 == false && currentMillis - previousMillis2b >= interVal2b) {
pos = pos - 1;
servo2.write(pos); // tell servo to go to position in variable 'pos'
previousMillis2b = currentMillis;
if (pos == minPos) {
incr2 = true;
};
}
}
void servo_3() {
// not yet used
}