Arduino Nano Clone Control Servo Sweep Delay Problem

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include  <Servo.h>

Servo servo1;  // create servo object to control a servo 
Servo servo2; 

int pos1;    
int pos2; 
int button = 2; 

const int maxDeg1 = 50; // limits the maximum range of the servo's movement 
const int minDeg1 = 20;   // limits the minimum range of the servo's movement 

const int maxDeg2 = 50; // limits the maximum range of the servo's movement 
const int minDeg2 = 20;   // limits the minimum range of the servo's movement

const int movement1 = 25; // distance to move servo
const int 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); 
      }
}                   
}
closed account (48T7M4Gy)
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.

Your right kemort I got it to work thank you very much.
Topic archived. No new replies allowed.