I am trying to create in the starter kit the mood cue and I would like to use one button to turn the servo to one position, another one to turn it to another position and so on. Although it is not working... Below is my code:
#include <Servo.h>
Servo myServo;
int greenLed = 12;
int yellowLed = 10;
int redLed = 9;
int buttonPin1 = 11;
int buttonPin2 = 8;
int buttonPin3 = 7;
int buttonState1;
int buttonState2;
int buttonState3;
int comeIn = 10;
int knock = 70;
int stayOut = 140;
void setup() {
myServo.attach(6);
myServo.write(90);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
delay(1000);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
if (buttonPin1 == HIGH) {
myServo.write(comeIn);
digitalWrite(greenLed, HIGH);
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, LOW);
}
if (buttonPin2 == HIGH) {
myServo.write(knock);
digitalWrite(yellowLed, HIGH);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
}
if (buttonPin3 == HIGH) {
myServo.write(stayOut);
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
} else {
myServo.write(90);
}
delay(100);
}
Thanks a lot for your answer. Yes that was probably the problem although now when I press the buttons the corresponding LED lights up but the servo does not change it rotation, only on when I press button 3, which is the red LED. Also, how would I make it possible so when I click the button it stays at that amount of degrees instead of returning back to 90 degrees as the else statement says?
Thanks for your answer, now when I press button 1, which should let anyone "come in" the yellow LED turns on and the green one turns off... I basically want it so when you press button 2 the yellow LED to light up and set the servo to 70 degrees and then stay at 70 degrees until I push another of the two buttons... Same with the green led, come in for example. I want it to turn the yellow and red LEDs off and the green ON, while also turning the servo to 10 degrees and staying there. I am going to bed here in Sweden now so I will see your replies tomorrow. but any help is appreciated a lot as this would be a cool project
//zoomkat servo button test 12-29-2011
// Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
pinMode(button2, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
digitalWrite(5, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(170);
}
press2 = digitalRead(button2);
if (press2 == LOW)
{
servo1.write(10);
}
}