Offline
Newbie
Karma: 0
Posts: 18
|
 |
« on: February 02, 2013, 08:20:53 pm » |
Hello everyone, 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: https://sites.google.com/site/tekesprojects/ // 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); } }
|
|
|
|
« Last Edit: February 02, 2013, 08:23:02 pm by teke115 »
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #1 on: February 02, 2013, 08:31:13 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #2 on: February 02, 2013, 08:35:51 pm » |
Thank you. I am new to this so I do not really understand what you mean. Again, Thank you 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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35478
Seattle, WA USA
|
 |
« Reply #3 on: February 02, 2013, 09:34:14 pm » |
#define LED 13 // the pin for the red LED #define LED2 12 // Yellow LED #define LED3 11 // Green LED Nothing, two, three... Is that how you count? #define BUTTON 7// Pushbutton to turn red #define BUTTON2 6 // Button to turn green Nothing, two... If you are going to number some related variables, number ALL of them. void setup() { pinMode(LED, OUTPUT); // Outputs and Inputs pinMode(BUTTON, INPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); pinMode(BUTTON2, INPUT); } No internal pullup resistors are being used. So, you have external pullup or pulldown resistors, right? How are the switches wired? digitalWrite(LED, LOW); digitalWrite(LED2, HIGH); // Go Yellow digitalWrite(LED3, LOW); Would yellowLEDPin, redLEDPin, and greenLEDPin make more sense?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #4 on: February 02, 2013, 09:49:36 pm » |
#define LED 13 // the pin for the red LED #define LED2 12 // Yellow LED #define LED3 11 // Green LED Nothing, two, three... Is that how you count? #define BUTTON 7// Pushbutton to turn red #define BUTTON2 6 // Button to turn green Nothing, two... If you are going to number some related variables, number ALL of them. void setup() { pinMode(LED, OUTPUT); // Outputs and Inputs pinMode(BUTTON, INPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); pinMode(BUTTON2, INPUT); } No internal pullup resistors are being used. So, you have external pullup or pulldown resistors, right? How are the switches wired? digitalWrite(LED, LOW); digitalWrite(LED2, HIGH); // Go Yellow digitalWrite(LED3, LOW); Would yellowLEDPin, redLEDPin, and greenLEDPin make more sense? Yes That makes a lot of sense. Thank you for all of your help. I just don't know why it refuses to follow the patterns correctly.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35478
Seattle, WA USA
|
 |
« Reply #5 on: February 02, 2013, 10:03:52 pm » |
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).
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 18
|
 |
« Reply #6 on: February 02, 2013, 11:18:37 pm » |
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). Thank You a TON!!!! I will try tomorrow morning.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 3
Posts: 207
|
 |
« Reply #7 on: February 03, 2013, 08:06:13 am » |
I would hope that those leds do not illuminate since there are no current limiting resistors being used. - Scotty
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 836
|
 |
« Reply #8 on: February 03, 2013, 08:10:55 am » |
Nothing, two, three... Is that how you count? Lots of people count that way.... lethal weapon lethal weapon 2 lethal weapon 3 rocky rocky 2 rocky 3
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #9 on: February 03, 2013, 08:17:48 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
|