Pages: [1]   Go Down
Author Topic: Controlling multiple servos with different pushbuttons  (Read 709 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I am trying to have 3 different servos each connected to a separate pushbutton move whenever the pushbutton they are assigned to is pressed. Along with that a LED will light up. I have managed to make it work fine with one pushbutton and servo but am running into issues when trying to add more. The code I have tried with two pushbuttons and servos kind of works but the servos do not move independently of each other, no matter which button is pressed both servos move.
Code:
#include <Servo.h>

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

const int buttonPin = 2;      // the pin that the pushbutton is attached to
const int ledPin    = 5;      // the pin that the LED is attached to
const int buttonPin2 = 3;     // the pin that the second pushbutton is attached to

int buttonState     = 0;      // current state of the button
int lastButtonState = 0;      // previous state of the button
int ledState        = 0;      // remember current led state
int pos             = 0;      // variable to store the servo positions

void setup()
{
  myservo.attach(8);          // attaches the servo on pin 8 to the servo object
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input
  pinMode(ledPin, OUTPUT);    // initialize the button pin as a output
 
  myservo2.attach(9);          // attaches the servo on pin 9 to the servo object
  pinMode(buttonPin2, INPUT);  // initialize the button pin as a input
  pinMode(ledPin, OUTPUT);    // initialize the button pin as a output
}

void loop()
{
  buttonState = digitalRead(buttonPin); // read the pushbutton input pin
    if (buttonState != lastButtonState) // compare buttonState to previous state
    {
      if (buttonState == 1)
      {
        if(ledState == 1)
          {
            ledState = 0;
            for(pos = 0; pos <= 90; pos += 5)    // goes from 0 degrees to 90 degrees
            {                                    // in steps of 5 degrees
            myservo.write(pos);                  // tell servo to go to position 'pos'
            delay(15);        // waits 15ms for the servo to reach the position
            }
          }
        else 
          { 
            ledState = 1;
            for(pos = 90; pos >= 1; pos -= 5)    // goes from 90 degrees to 0 degrees
            {                               
            myservo.write(pos);                  // tell servo to go to position 'pos'
            delay(15);                           // waits 15ms for the servo to reach the position
            }
          }
      }
     
    lastButtonState = buttonState; // remember the current state of the button
    }
  // turns LED on if the ledState =1 or off if ledState = 0
  digitalWrite(ledPin, ledState);
  myservo.write(pos);          // goes from 0 degrees to 90 degrees

  delay(20);
 
 
   buttonState = digitalRead(buttonPin2); // read the pushbutton input pin
    if (buttonState != lastButtonState) // compare buttonState to previous state
    {
      if (buttonState == 1)
      {
        if(ledState == 1)
          {
            ledState = 0;
            for(pos = 0; pos <= 90; pos += 5)    // goes from 0 degrees to 90 degrees
            {                                    // in steps of 5 degrees
            myservo2.write(pos);                  // tell servo to go to position 'pos'
            delay(15);        // waits 15ms for the servo to reach the position
            }
          }
        else 
          { 
            ledState = 1;
            for(pos = 90; pos >= 1; pos -= 5)    // goes from 90 degrees to 0 degrees
            {                               
            myservo2.write(pos);                  // tell servo to go to position 'pos'
            delay(15);                           // waits 15ms for the servo to reach the position
            }
          }
      }
     
    lastButtonState = buttonState; // remember the current state of the button
    }
  // turns LED on if the ledState =1 or off if ledState = 0
  digitalWrite(ledPin, ledState);
  myservo2.write(pos);          // goes from 0 degrees to 90 degrees

  delay(20);
}

Any suggestions?
Thanks!
Logged

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello and welcome,

What if you create new variable lastButtonState2 ? Since you use lastButtonState for both buttons it might make troubles... smiley
« Last Edit: November 19, 2012, 02:59:10 am by guix » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why is the variable that defines the direction to move the servo called ledState?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because it is being led astray?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: