switch case doesnt work - it goes to one case only and ignore case 0:

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

}

What’s connected to the IR pin? (And How)

——
Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks... Tom.. :slight_smile: