S2 Servos Moving one after another

Hi,

I guess this is my first question and help needing post.

I just want to have the ability to use 2 or more servos and geting them moving with a push button one to 180 degrees for example and then the other one to the same 180 degrees and finaly having them reset to 0.

I Already have coded something but i'm not geting there.

BTW i'm a large rookie on Arduino.

#include <Servo.h> 

 // Set digital pin numbers:
 const int servoPin = 8;  // The number of the Servo pin
 const int buttonPin = 6;  // The number of the Pushbutton pin


 int buttonState = 0;  // Variable for reading the pushbutton status
 int directionState = 0;  // Variable for reading direction of the servo


 Servo myservo;  // Create servo object to control a servo 
 Servo myservo2;

 int pos = 0;  // Variable to store the servo position 


void setup() {
   myservo.attach(8);  // attaches the servo on pin 8 to the servo object 
   myservo2.attach(9);
   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
 }

void loop(){
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);

   if (directionState == 0){
     //The button is pushed
     if (buttonState == HIGH) {
       directionState = 1;// The direction for the servo is clockwise

       // goes from 0 degrees to 180 degrees in steps of 1 degree
       for(pos = 15; pos < 175; pos=pos+1)
       {
         myservo.write(pos);  // tell servo to go to position in variable ‘pos’ 
         myservo2.write(170);
         delay(15);  // waits 15ms for the servo to reach the position 
       }
     }

   } else if (directionState == 1) {
     // The button is pushed
     if (buttonState == HIGH) {
       directionState = 0;  // The direction for the servo is anti-clockwise 

       // goes from 180 degrees to 0 degrees in steps of 1 degree 
       for(pos = 175; pos>=15; pos=pos-1)
       {
         myservo.write(pos);  // tell servo to go to position in variable ‘pos’ 
         myservo2.write(15);
         delay(15);  // waits 15ms for the servo to reach the position 

         
       }
     }
   }
 }

What happens when you run the program ?
You probably need to detect when the button becomes pressed rather than is pressed. See the StateChangeDetection example in the IDE.

They move at the same time.
I do want that the program runs all the way to the end with just one push of the button.

I think you should swap the order of

if (directionState == 0){
     //The button is pushed
     if (buttonState == HIGH) {

in other words, test for the buttonState first.

...R

The below discussion might be of interest. The project was to have a model fire truck ladder extend and retract.

http://forum.arduino.cc/index.php?topic=244994.0

Wil check that and let's se how it goes.

Thanks

Ok 1ª Stage Project Working!!

Thanks for al the help.

#include <Servo.h> 

// Set digital pin numbers:
const int servoPin = 8;  // The number of the Servo pin
const int buttonPin = 6;  // The number of the Pushbutton pin
const int buttonPin2 = 5;

int buttonState = 0;  // Variable for reading the pushbutton status
int buttonState2 = 0;
int directionState = 0;  // Variable for reading direction of the servo
int directionState2 = 0;
int pos = 15;  // variable to store and set the servo position 

Servo myservo;  // Create servo object to control a servo 
Servo myservo2;


void setup() {
  myservo.attach(8);  // attaches the servo on pin 8 to the servo object 
  myservo2.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo.write(pos);
  myservo2.write(pos);
  pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (directionState == 0){
    //The button is pushed
    if (buttonState == HIGH) {
      directionState = 1;// The direction for the servo is clockwise

      // goes from 0 degrees to 180 degrees in steps of 1 degree
      
      
        myservo.write(175);  // tell servo to go to position in variable ‘pos’ 
        delay(1000);  // waits 15ms for the servo to reach the position 
        myservo.write(0);
     
        myservo2.write(175);  // tell servo to go to position in variable ‘pos’ 
        delay(1000);  // waits 15ms for the servo to reach the position 
        myservo2.write(0);
    }

  } else if (directionState == 1) {
    // The button is pushed
    if (buttonState == HIGH) {
      directionState = 0;  // The direction for the servo is anti-clockwise 

      myservo.write(175);  // tell servo to go to position in variable ‘pos’ 
        delay(1000);  // waits 15ms for the servo to reach the position 
        myservo.write(0);
     
        myservo2.write(175);  // tell servo to go to position in variable ‘pos’ 
        delay(1000);  // waits 15ms for the servo to reach the position 
        myservo2.write(0);

        
      }
    }
  }

Good on you for getting it working.

A small suggestion for improvement that will not make any difference in this program but could be important for larger projects in the future would be to change all of the int variables into bytes to save memory. It is good practice to use the smallest variable type that will hold the range of values required. Not only can this save memory but in more complicated programs it can speed them up slightly and remove the need to turn interrupts off and on whilst performing manipulation on multi byte variables.

Personally I would also rename one or both of the servos to make the names consistent and meaningful. Once again the program will not run any differently but it will be easier to add to and maintain in the future.

I also hate to see comments that don't match the code

      myservo2.write(175);  // tell servo to go to position in variable 'pos'
      delay(1000);  // waits 15ms for the servo to reach the position

All the sugestion are welcome and those on the memory saver are perfect to make in future and longer programs as you mention, will make the changes.

Yes you are right i forgot to replace the comments on the code.

Hi Guys,

Here's another help needed.

How to add to this code an Wireless RF Remote & Receiver?

I cannot manage them to work, like pressing the button on the keyfob and the code runs all the way to the end, for it to work i have to be pressing the button all along in the Keyfob.

#include <Servo.h> 

 // Set digital pin numbers:
 const int servoPin = 8;  // The number of the Servo pin
 const int buttonPin = 5;  // The number of the Pushbutton pin


 int buttonState = 0;  // Variable for reading the pushbutton status
 int directionState = 0;  // Variable for reading direction of the servo
 int pos = 15;  // variable to store and set the servo position 

 Servo myservo;  // Create servo object to control a servo 
 Servo myservo2;
 Servo myservo3;
 Servo myservo4;
 Servo myservo5; 

void setup() {
   myservo.attach(7);  // attaches the servo on pin 7 to the servo object 
   myservo2.attach(8);  // attaches the servo on pin 8 to the servo object 
   myservo3.attach(9);
   myservo4.attach(10);
   myservo5.attach(11);
   myservo.write(pos);
   myservo2.write(pos);
   myservo3.write(pos);
   myservo4.write(pos);
   myservo5.write(pos);
   
   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
 }

void loop(){
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);

   if (directionState == 0){
     //The button is pushed
     if (buttonState == HIGH) {
       directionState = 1;// The direction for the servo is clockwise

       // goes from 0 degrees to 180 degrees in steps of 1 degree
       
       
         myservo.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo.write(5);
      
         myservo2.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo2.write(5);

         myservo3.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo3.write(5);

         myservo4.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo4.write(5);

         myservo5.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo5.write(5);
         
     }

   } else if (directionState == 1) {
     // The button is pushed
     if (buttonState == HIGH) {
       directionState = 0;  // The direction for the servo is anti-clockwise 

       myservo.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(1000);  // waits 15ms for the servo to reach the position 
         myservo.write(1);
      
         myservo2.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(1000);  // waits 15ms for the servo to reach the position 
         myservo2.write(1);

         myservo3.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(1000);  // waits 15ms for the servo to reach the position 
         myservo3.write(1);
      
         myservo4.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(1000);  // waits 15ms for the servo to reach the position 
         myservo4.write(1);
         
         myservo5.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(1000);  // waits 15ms for the servo to reach the position 
         myservo5.write(1);
         
       }
     }
   }

I cannot manage them to work,

Please post the code you have tried and describe what happens when you use it. What wireless hardware are you using and how is it connected to the Arduino and powered ?

The code i use just for an example

I use IC 2262/2272 4 CH Key 315MHZ Wireless Remote Control Receiver module arduino

And i power up the Receiver, ground and 5v and all the signals from the receiver to the Arduino Digital 2,3,4 and 5 pins

#include <Servo.h> 


 Servo myservo;  // Create servo object to control a servo 

void setup() {
   myservo.attach(11);
   pinMode(5, INPUT);
   pinMode(4, INPUT);
   pinMode(3, INPUT);
   pinMode(2, INPUT);// initialize the pushbutton pin as an input
 }

void loop(){

         if(digitalRead(5)) 
         myservo.write(5);  // tell servo to go to position in variable ‘pos’ 
         delay(1000);  // waits Xms for the servo to reach the position 
         if(digitalRead(4)) 
         myservo.write(60);
         delay(1000);
         if(digitalRead(3)) 
         myservo.write(100);
         delay(1000);
         if(digitalRead(2)) 
         myservo.write(175);
         delay(1000);
  
         
     }

Okay, i done it!!!

#include <Servo.h> 

 int pos = 15;  // variable to store and set the servo position 

  // Create servos object to control a servo 
 Servo myservo; 
 Servo myservo2;
 Servo myservo3;
 Servo myservo4;
 Servo myservo5; 



void setup() {
   // Attaches the servo on designated pin to the servo object 
   myservo.attach(7);  
   myservo2.attach(8);   
   myservo3.attach(9);
   myservo4.attach(10);
   myservo5.attach(11);
   
   // Forces servo into initial position
   myservo.write(pos);
   myservo2.write(pos);
   myservo3.write(pos);
   myservo4.write(pos);
   myservo5.write(pos);
   
   pinMode(5, INPUT); // Input for the Wireless Receiver
 }

void loop(){

         if (digitalRead(5)){
         
         myservo.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo.write(5);
      
         myservo2.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo2.write(5);

         myservo3.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo3.write(5);

         myservo4.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo4.write(5);

         myservo5.write(175);  // tell servo to go to position in variable ‘pos’ 
         delay(2000);  // waits Xms for the servo to reach the position 
         myservo5.write(5);
         }
         
     }

Now for the tricky part

I can i make a random out of those servos????

Should you be using a library of some kind to help decode the received signal ?
What is it that makes you think that the button presses cause pins 2, 3, 4 and 5 to change state ? Have you got any documentation ?

UKHeliBob,

I'm not using any library, just want the servos to move whem the press of Button A on the Keyfob, wich i manage to discover that it is pin 0 on the receiver, and then i connect it to pin 5 on the Arduino.

Are you saying that pin 5 on the Arduino changes state when you press button A on the transmitter ? If so, what do you want the servo to do when you press the button ? If you want the servo to move to a (pseudo)random position then use

myServo.write(random(0, 181));

I guess i didn't explained miself, what i want is for the 5 servos connected, do what they suposed to be doing, go from 0 to 175 and return to 0 but they are doing that on a sequence, like 1 then 2 then 3 then 4 then 5.

What i do whant is for them to do the 0 to 175 and to 0 but on a random order, like 3 first then the 1 then the 5 and so on.