I made some code that's supposed to add 1 to a variable, and a have 3 lights that are supposed to light up when the variable reaches a certain value.
(When the variable=0 the red LED lights up, when the variable=1 the yellow LED lights up etc.
when the variable's value goes above 2 its resets to 0 again)
When I upload the code to my arduino board the yellow light lights up and when I press the button nothing happens.
Ive been trying to find a solution for a while and I haven't been able to fix my code at all.
const int pinRed = 12;
const int pinYellow = 11;
const int pinGreen = 10;
const int button = 9;
int lastButtonState = 0;
int buttonState = 0;
int lightState = 0;
void setup() {
pinMode(pinRed, OUTPUT);
pinMode(pinYellow, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
buttonState = digitalRead(button);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
lightState++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(lightState);
} else {
Serial.println("off");
}
delay(50);
}
lastButtonState = buttonState;
if (lightState = 0) {
digitalWrite(pinRed, HIGH);
digitalWrite(pinYellow, LOW);
digitalWrite(pinGreen, LOW);
} else if (lightState = 1) {
digitalWrite(pinRed, LOW);
digitalWrite(pinYellow, HIGH);
digitalWrite(pinGreen, LOW);
} else if (lightState = 2) {
digitalWrite(pinRed, LOW);
digitalWrite(pinYellow, LOW);
digitalWrite(pinGreen, HIGH);
}
}
