Go Down

Topic: Traffic Light Programming help (Read 602 times) previous topic - next topic

teke115

Feb 03, 2013, 02:20 am Last Edit: Feb 03, 2013, 02:23 am by teke115 Reason: 1
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/

Code: [Select]
// 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);
}
   
}

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

teke115

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

PaulS

Code: [Select]
#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?

Code: [Select]
#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.

Code: [Select]
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?

Code: [Select]
    digitalWrite(LED, LOW);
    digitalWrite(LED2, HIGH); // Go Yellow
    digitalWrite(LED3, LOW);

Would yellowLEDPin, redLEDPin, and greenLEDPin make more sense?

teke115


Code: [Select]
#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?

Code: [Select]
#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.

Code: [Select]
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?

Code: [Select]
    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.

PaulS

Quote
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).

teke115


Quote
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.

scottyjr

I would hope that those leds do not illuminate since there are no current limiting resistors being used. - Scotty

michinyon

Quote
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


ash901226

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

Go Up