Two servo's, two buttons, just more than all single examples and independent

Hi all,

For automating a railway switch with servo’s I was looking for a code that I can extend to multiple servo’s. The problem that I found was that all examples are with one servo, and can’t be copied to multible because of the loop type that is used.

What I’m looking for is x amount of servo’s controlled, each by one pull up push button. The code that I created from several expamples just seems not to be fully independent.

So I want 4 servo’s on my nano with servo shield, and 4 buttons.

Servo1 in standard position 1a going to position 1b by continue pull up signal ( high by relais).
Servo2 in standard position 2a going to position 2b by continue pull up signal ( high by relais). etc. etc.

Prefferable to adjust the speed as well, each should have its own start and end position and working fully indpendent possible controlled at the same time.

The code I have below isn’t working independent, one button only works when the other is pressed too :S sometimes not working etc.

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo 
Servo myservo2;   // a maximum of eight servo objects can be created 
 
 int pos1 = 0;
 int pos2 = 0; // variable to store the servo position 
 int button1 = 2;  
 int button2 = 3;
 
 void setup() 
{ 
  myservo1.attach(7);  // attaches the servo on pin 7 to the servo object
 pinMode(pos1, OUTPUT);
 pinMode(button1, INPUT); 
 digitalWrite (button1, LOW);
 
  myservo2.attach(6);  // attaches the servo on pin 6 to the servo object
 pinMode(pos2, OUTPUT);
 pinMode(button2, INPUT); 
 digitalWrite (button2, LOW);
} 
 
 void loop() 
{ 
     
  
  if (digitalRead(button2) == HIGH) 
  
  for(pos2 = 90; pos2>=90; pos2-=90)     // goes from 90 degrees to 0 degrees 
  {                                
     myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
       delay(2);                          // waits 50ms for the servo to reach the position 
 
  } 
  if (digitalRead(button1) == HIGH) 
  
  for(pos1 = 90; pos1>=90; pos1-=90)     // goes from 90 degrees to 0 degrees 
  {                                
     myservo1.write(pos1);              // tell servo to go to position in variable 'pos' 
                             // waits 50ms for the servo to reach the position 
    delay(2); 

   if (digitalRead(button2) == LOW)

  for(pos2 = 0; pos2 < 90; pos2 += 90)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of  degree 
    myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
            delay(2);                // waits 1s for the servo to reach the position 
  } 
    if (digitalRead(button1) == LOW)

  for(pos1 = 0; pos1 < 90; pos1 += 90)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of  degree 
    myservo1.write(pos1);              // tell servo to go to position in variable 'pos' 
         delay(2);                   // waits 1s for the servo to reach the position 



  }}}

anyone can see how to improve the code? the code is not even close to what i’m looking for…

Also the code below didn’t work out wel to copy for a second

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
    
  }}

I don’t really know anything about servos, but using your own sketch as my guide I think the following should get you a bit closer

#include <Servo.h>

 int servoCount=2;  //how many servos we have
 int positions[2];  //vars to store positions
Servo myservos[2];  // create servo objects to control a servo 
int buttons[2] ={2,3};
int sPins[2]={6,7};

 void setup() 
{ 
int n;
for(n=0;n<servoCount;n++)
  {myservos[n].attach(sPins[n]);//attaches each servo to it's pin
   positions[n]=0;
   myservos[n].write(0); //set servo at 0 to start; 
   pinMode(buttons[n], OUTPUT);   
   digitalWrite(buttons[n],LOW);  
  }
} 
 
 void loop() 
{ 
int n;
  for (n=0;n<servoCount;n++)
   {if(digitalRead(buttons[n]==HIGH))
      {positions[n]++;
       if (positions[n]>=90)
          positions[n]=0;
       myservos[n].write(positions[n]);
      }  
   }
}

thanks KenF, looks cool with N amount and stuff, I like that, however I’m not getting it working for this moment,

I already got myself some working code now, but I do like your simple programming with N amount, if you could help me out to get it working that would be awesome!

here my code now independent working.

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
 int pos1 = 0;    // variable to store the servo position 
 int button1 = 1;  // The button will be on Pin 7 
 
 Servo myservo2;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
 int pos2 = 0;    // variable to store the servo position 
 int button2 = 2;  // The button will be on Pin 7 
 
 
 void setup() 
{ 
  myservo1.attach(7);  // attaches the servo on pin 9 to the servo object
 pinMode(pos1, OUTPUT);
 pinMode(button1, INPUT); 
 digitalWrite (button1, LOW);
 
   myservo2.attach(6);  // attaches the servo on pin 9 to the servo object
 pinMode(pos2, OUTPUT);
 pinMode(button2, INPUT); 
 digitalWrite (button2, LOW);
 
} 
 
 void loop() 
{ 
  
    if (digitalRead(button1) == LOW)

  for(pos1 = 0; pos1 < 90; pos1 += 90)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of  degree 
    myservo1.write(pos1);              // tell servo to go to position in variable 'pos' 
                          // waits 1s for the servo to reach the position 
  } 
  if (digitalRead(button1) == HIGH) 
  
  for(pos1 = 90; pos1>=90; pos1-=90)     // goes from 90 degrees to 0 degrees 
  {                                
     myservo1.write(pos1);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
  }
{ 
  
    if (digitalRead(button2) == LOW)

  for(pos2 = 0; pos2 < 90; pos2 += 90)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of  degree 
    myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
                          // waits 1s for the servo to reach the position 
  } 
  if (digitalRead(button2) == HIGH) 
  
  for(pos2 = 90; pos2>=90; pos2-=90)     // goes from 90 degrees to 0 degrees 
  {                                
     myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
  delay(50);                             // waits 50ms for the servo to reach the position 
  }}




}

Looking at your setup routine I notice you have

pinMode(pos1, OUTPUT);

I believe this is a mistake. Surely pos1 is holding the position (it is, after all initialised to be 0)
i’m guessing that what you intended was pinMode(7, OUTPUT);

Looking at your for loops (within the loop function) each of them will only execute ONCE.

For example the first one

for(pos1 = 0; pos1 < 90; pos1 += 90)

Before it executes this loop it will initialise pos1 to 0;
It will then execute the loop once
It will then update pos1 (+=90) which makes pos1 now equal to 90
Then when it checks the condition (pos1 smaller than 90 it will find it’s not true
So the loop will not execute a second time.

You have the same issue with all of your for loops yet you say it is working for you?

If so, this could be simplified to

    if (digitalRead(button1) == LOW)
        myservo1.write(0);              // tell servo to go to position 0  
    if (digitalRead(button1) == HIGH) 
      myservo1.write(90);              // tell servo to go to position 90 
    delay(50);                             // waits 50ms for the servo to reach the position 
  
    if (digitalRead(button2) == LOW)
       myservo2.write(0);              // tell servo to go to position 0 

   if (digitalRead(button2) == HIGH) 
     myservo2.write(90);              // tell servo to go to position 90 
  delay(50);                             // waits 50ms for the servo to reach the position

As you can see, this makes the pos1 variable redundant.

so putting it all together with the enumerated servos would be as follows

#include <Servo.h>

int servoCount=2;
int buttons[2] = {1,2};  // The button will be on Pin 7 
int sPins[2]={7,6};     //pins used for servos
Servo myservos[2];  // create servo objects to control a servo 
 
 
 
 void setup() 
{ 
int n;
for (n=0;n<servoCount;n++)
  {myservos[n].attach(sPins[n]);  // attaches the servo on pin 9 to the servo object
   pinMode(sPins[n], OUTPUT);
   pinMode(buttons[n], INPUT); 
   digitalWrite (buttons[n], LOW);
  }
} 
 
 void loop() 
{ 
int n;
  for (n=0;n<servoCount;n++)
    {if (digitalRead(buttons[n]) == LOW)
        myservos[n].write(0);              // tell servo to go to position 0  
    if (digitalRead(buttons[n]) == HIGH) 
      myservos[n].write(90);               // tell servo to go to position 90
     }                                      
    delay(50);                             // waits 50ms for the servos to reach their position 
}