I am trying to make a button work as a switch. The code works to turn the lights "on" but the code doesn't want to turn them off.
My code works like so:
- If button is pressed and the lights are off, turn on the lights.
- If button is pressed and the lights are on, turn off the lights.
But number 2 doesn't work.
int buttonStatus = 0;
int check = 1;
int Status = 0;
void setup() {
pinMode(5,OUTPUT);
pinMode(7,OUTPUT);
pinMode(9,OUTPUT);
pinMode(11,OUTPUT);
pinMode(13,OUTPUT);
pinMode(2,INPUT);
}
void loop() {
if (check = 1) {
buttonStatus = digitalRead(2);
if (buttonStatus == HIGH && Status == 0) {
Status = 1;
buttonStatus = 0;
} else if (buttonStatus == HIGH && Status == 1) {
Status = 0;
buttonStatus = 0;
}
}
if (Status == 1) {
digitalWrite(5,HIGH);
delay(50);
digitalWrite(5,LOW);
digitalWrite(7,HIGH);
delay(50);
digitalWrite(7,LOW);
digitalWrite(9,HIGH);
delay(50);
digitalWrite(9,LOW);
digitalWrite(11,HIGH);
delay(100);
digitalWrite(11,LOW);
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
} else {
digitalWrite(5,LOW);
digitalWrite(7,LOW);
digitalWrite(9,LOW);
digitalWrite(11,LOW);
digitalWrite(13,LOW);
}
}
Ok, your description and your code tell two different things. I'm try to interprete them, but if I'm wrong just tell me and I'll try to correct the answer.
This code lets you use a pushbutton to turn on and off a light on pin 5. One press will turn it on, the other will turn it off. You have to connect the button with one end to pin 2 and the other to ground (since we are using a pull-up resistor).
I also added a small debounce delay to cope with the bounces of the mechanical switch (50ms)
When you press the button the led on pin 5 will turn on, when you press it again it will turn off.
This is the behavior you asked. Your code, on the other side, lights up a sequence of LEDs when you push the button. In this case, if you want to start the cycle with a press and then stop it with another press, you have to use a sort of simple state machine, like the one in the code. I also added a small debounce to the button, which needs again to be connected between 2 and ground.
This works in this way: you press the button and the board will start cycling through the LEDS. 5, 7, 9, 11, 13, 5, 7, 9, 11, 13, ... Until you press again the button. When you do this, it stops, then at the next press restarts from 5.
If you want that after the 13 it stops, change the line 105 from
stateMachineStatus = STATE_LED5ON;
tostateMachineStatus = STATE_LEDSOFF;
.One note: in your code the delay is too low (and it is the same that I put here): 50 ms between one led and the other cannot be noticed. If you want to actually see them in sequence, put values of at least 250 in the TIME_LEDxON defines.
DISCLAIMER: I haven't tested these codes since I don't have arduino ide installed at present. If there are some bugs, simply tell me and I'll fix them.