I am currently making a traffic light on my Arduino MEGA and needed some help with programming problems. I am using 2 buttons. I want to have it set up so when I press and release one button, it goes to yellow for one second and then red until I hit the second button to go back to green. It either lights all up at once or won't turn red. There is a sketch of my setup and then my code will also be included. Any help is greatly appreciated.
IMAGE:
// Traffic Light
#define LED 13 // the pin for the red LED
#define BUTTON 7// Pushbutton to turn red
#define LED2 12 // Yellow LED
#define LED3 11 // Green LED
#define BUTTON2 6 // Button to turn green
int val = 0;
int val2 = 0;
void setup() {
pinMode(LED, OUTPUT); // Outputs and Inputs
pinMode(BUTTON, INPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(BUTTON2, INPUT);
}
void loop(){
val = digitalRead(BUTTON);
val2 = digitalRead(BUTTON2);
if (val == HIGH) { // When pressed..
digitalWrite(LED, LOW);
digitalWrite(LED2, HIGH); // Go Yellow
digitalWrite(LED3, LOW);
delay(1000); // For 1 Sec
digitalWrite(LED, HIGH); // Then Red
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}
else {
digitalWrite(LED, LOW);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
delay(1000);
digitalWrite(LED, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}
if (val2 == HIGH){ // When other button pressed...
digitalWrite(LED, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH); // Turn Green
}
else {
digitalWrite(LED, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, HIGH);
}
}
Thank you. I am new to this so I do not really understand what you mean. Again, Thank you
ash901226:
urm maybe it would help if you debounce your switchs?
and maybe you could use a flag base programming
look into state machine could help too
I just don't know why it refuses to follow the patterns correctly.
I don't know what it actually does, and I can't follow the pattern, because the variable names don't make sense to me.
You could be having hardware issues. Floating pins, perhaps.
I'd ditch the external pulldown resistors, and greatly simplify the wiring. One leg of each switch goes to ground. The other leg goes to a digital pin. Turn on the internal pullup resistors by using digitalWrite(pinNumber, HIGH); after setting the pin to INPUT. There goes any possibility of floating pins being a problem. (LOW will mean pressed, if you do this).
Next, I'd consider the fact that the switches could be bouncing. A delay(10) after reading the switch will take care of that.
Finally, I'd consider that maybe you don't want to act when the pin IS low. Rather, you want to act when the pin just BECOMES low. In order to do that, you need to know the state of the switch this time (easy, you just read it) AND last time (easy, too. At the end of loop, save the current state. Next time, it is the previous state). (Meaningful names are important.) Compare the current state against the previous state, for each pin. If they are the same, do nothing. Otherwise, the switch was pressed and is now released or the switch was released and is now pressed. The current state tells you which. Act only when the current state is not the same as the previous state AND the current state is pressed (LOW, with internal pullup resistors).
I just don't know why it refuses to follow the patterns correctly.
I don't know what it actually does, and I can't follow the pattern, because the variable names don't make sense to me.
You could be having hardware issues. Floating pins, perhaps.
I'd ditch the external pulldown resistors, and greatly simplify the wiring. One leg of each switch goes to ground. The other leg goes to a digital pin. Turn on the internal pullup resistors by using digitalWrite(pinNumber, HIGH); after setting the pin to INPUT. There goes any possibility of floating pins being a problem. (LOW will mean pressed, if you do this).
Next, I'd consider the fact that the switches could be bouncing. A delay(10) after reading the switch will take care of that.
Finally, I'd consider that maybe you don't want to act when the pin IS low. Rather, you want to act when the pin just BECOMES low. In order to do that, you need to know the state of the switch this time (easy, you just read it) AND last time (easy, too. At the end of loop, save the current state. Next time, it is the previous state). (Meaningful names are important.) Compare the current state against the previous state, for each pin. If they are the same, do nothing. Otherwise, the switch was pressed and is now released or the switch was released and is now pressed. The current state tells you which. Act only when the current state is not the same as the previous state AND the current state is pressed (LOW, with internal pullup resistors).
it is mostly base of preference, but as PaulS point out its better to start counting from 0/1 up to what ever number you have to give the variable name. but usually for me, if i have to use the same variable name nowadays i would utilize array to keep all the info