this is my first program and stuck with switch, all the time print 0 and go to case 0 only: (my system has one sensor, 2 buttons and 2 continuous servo motors) but I need to get the switch fixed first and the buttons to be recognized. this is for my daughter costume. thanks
#include <Servo.h>;
const int buttonPin = 5;
const int buttonPin2 = 6;
const int servoPin = 2;
const int servoPin2 = 3;
const int IR = 8;
int incomingByte = 0;
int buttonState;
int buttonState2;
Servo servo;
Servo servo2;
void setup()
{
Serial.begin(9600);
servo.attach (servoPin);
servo2.attach (servoPin2);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(7, OUTPUT);
}
void loop()
{
int detect = digitalRead(IR);
int buttonVal;
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if(buttonState == LOW && detect == LOW)
{
buttonVal = 0;
Serial.println(buttonVal);
}
else if(buttonState2 == LOW && detect == LOW)
{
buttonVal = 1;
Serial.println(buttonVal);
}
else {
Serial.println("NO button pressed");
}
switch (buttonVal) {
case 0:
Serial.println("motor 1 ON");
servo.write (10);
delay(1600);
servo.write(90);
break;
case 1:
Serial.println("led on");
break;
} //END SWITCH
}