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.
#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!