Ok so I have an Arduino Nano clone which may be the problem right there but what I am trying to do is use the Arduino Nano to control two servos with one button. What I want to happen is when I press the button both servos will move at the same time from the original position to the desired position and stay there for a couple of seconds and then move back. Pretty simple right. I have the code that sweeps the servo to the desired position and back but when I put in the delay to hold the servo at the desired position let's say for a second both servos don't move at the same time. When I press the button only one servo moves at a time and when I press the button again the other servo moves by itself. I would greatly appreciate it if you could help me out with this problem. I modified this code from somewhere I found online. Here is my code:
#include <Servo.h>
Servo servo1; // create servo object to control a servo
Servo servo2;
int pos1;
int pos2;
int button = 2;
constint maxDeg1 = 50; // limits the maximum range of the servo's movement
constint minDeg1 = 20; // limits the minimum range of the servo's movement
constint maxDeg2 = 50; // limits the maximum range of the servo's movement
constint minDeg2 = 20; // limits the minimum range of the servo's movement
constint movement1 = 25; // distance to move servo
constint movement2 = 25; // distance to move servo
void setup()
{
servo1.attach(3); // attaches the servo motor's signal cable location, stored in the variable outputPin, to the servo object
servo2.attach(4);
pinMode(pos1, OUTPUT);
pinMode(pos2, OUTPUT);
pinMode(button, INPUT);
digitalWrite (button, LOW);
}
void loop()
{
// The following routine handles what happens if the first set of push buttons are pressed
{
if(digitalRead(button) == LOW){
if(pos1 < maxDeg1)
pos1 += movement1;
servo1.write(pos1);
}
if(digitalRead(button) == HIGH){
if(pos1 > minDeg1)
pos1 -= movement1;
servo1.write(pos1);
servo1.writeMicroseconds(0);
delay(1000);
}
}
{
if(digitalRead(button) == LOW){
if(pos2 < maxDeg2)
pos2 += movement2;
servo2.write(pos2);
}
if(digitalRead(button) == HIGH){
if(pos2 > minDeg2)
pos2 -= movement2;
servo2.write(pos2);
servo2.writeMicroseconds(0);
delay(1000);
}
}
}
It's not absolutely clear what you are trying to do. The way I understand it is:
1. You have 2 servos activated by a single button
2. Press the button and both servos move to maximum
3. Press the button, there is a delay, then both servos move to zero.
If you want both servos to move the same via a single button then you only need two if statements - one for button HIGH and the other for button LOW. And you alos don't need separate variables for pos and maxDeg.
The fact that your intent is not crystal, combined with just copying/modifying someone else's program, is a good enough reason for writing you own pseudocode where you describe in simple but clear steps what is supposed to happen at each stage.