Go Down

Topic: Traffic light circuit help? (Read 630 times) previous topic - next topic

aj88

Hello all,
I made a double traffic light circuit that I learned how to make (I am a beginner) using the Blink example. The coding works perfectly. However, I now want to incorporate a button into the circuit that will turn both lights to yellow, then red. Here is the code:
Code: [Select]
int button =2;
int red =9;
int yellow =10;
int green =11;
int red2 =5;
int yellow2 =6;
int green2 =7;
int buttonState =0;
void setup() {
 pinMode(red, OUTPUT);
 pinMode(yellow, OUTPUT);
 pinMode(green, OUTPUT);
 pinMode(red2, OUTPUT);
 pinMode(yellow2, OUTPUT);
 pinMode(green2, OUTPUT);
 pinMode(button, INPUT);
}
void loop() {
 buttonState = digitalRead(button);
 if (buttonState == HIGH) {
   digitalWrite(yellow, HIGH);
   digitalWrite(yellow2, HIGH);
   digitalWrite(green, LOW);
   digitalWrite(green2, LOW);
   digitalWrite(red, LOW);
   digitalWrite(red2, LOW);
   delay(1000);
   digitalWrite(yellow, LOW);
   digitalWrite(yellow2, LOW);
   digitalWrite(red, HIGH);
   digitalWrite(red2, HIGH);
   delay(3000);
 }
 else {
 digitalWrite(red, HIGH);
 digitalWrite(yellow,LOW);
 digitalWrite(green, LOW);
 digitalWrite(red2, LOW);
 digitalWrite(yellow2, LOW);
 digitalWrite(green2, HIGH);
 delay(5000);
 digitalWrite(yellow2, HIGH);
 digitalWrite(green2,LOW);
 delay(1000);
 digitalWrite(red, LOW);
 digitalWrite(green, HIGH);
 digitalWrite(green2, LOW);
 digitalWrite(yellow2, LOW);
 digitalWrite(red2, HIGH);
 delay(3000);
 digitalWrite(green,LOW);
 digitalWrite(yellow,HIGH);
 delay(1000);
 }
}

I use the 5v pin to supply power across my button switch and a 10k pull down resistor. However, when I push the button, the lights don't turn yellow then red as I wanted. Nothing happens except for the 'else' part continually loops. Is there a way to make it so when I push the button, the circuit will immediately go to the 'if' part instead of continually looping the 'else'?

Erdin

Could you use a wire and connect pin 2 to 5V and to GND ?
I can't see what the problem is with the code, so I want to be sure that pin 2 is high and low.

Xnor

Erdin, I think it's that aj88 wants to be able to interrupt the delays as to get an immediate output.
To actually do what you are wanting to do you need to look into threading. That is the only way I know how to get rid of the Delay problem.
If it's just that it's always looping to Else
I think something like this might help.
Code: [Select]
char oldVal='2';
int button =2;
int red =9;
int yellow =10;
int green =11;
int red2 =5;
int yellow2 =6;
int green2 =7;
int buttonState =0;
void setup() {
 pinMode(red, OUTPUT);
 pinMode(yellow, OUTPUT);
 pinMode(green, OUTPUT);
 pinMode(red2, OUTPUT);
 pinMode(yellow2, OUTPUT);
 pinMode(green2, OUTPUT);
 pinMode(button, INPUT);
}
 void lightHIGH()
 {
   digitalWrite(yellow, HIGH);
   digitalWrite(yellow2, HIGH);
   digitalWrite(green, LOW);
   digitalWrite(green2, LOW);
   digitalWrite(red, LOW);
   digitalWrite(red2, LOW);
   delay(1000);
   digitalWrite(yellow, LOW);
   digitalWrite(yellow2, LOW);
   digitalWrite(red, HIGH);
   digitalWrite(red2, HIGH);
   delay(3000);
 }
 void lightLOW()
 {
 digitalWrite(red, HIGH);
 digitalWrite(yellow,LOW);
 digitalWrite(green, LOW);
 digitalWrite(red2, LOW);
 digitalWrite(yellow2, LOW);
 digitalWrite(green2, HIGH);
 delay(5000);
 digitalWrite(yellow2, HIGH);
 digitalWrite(green2,LOW);
 delay(1000);
 digitalWrite(red, LOW);
 digitalWrite(green, HIGH);
 digitalWrite(green2, LOW);
 digitalWrite(yellow2, LOW);
 digitalWrite(red2, HIGH);
 delay(3000);
 digitalWrite(green,LOW);
 digitalWrite(yellow,HIGH);
 delay(1000);
 }
void loop() {
 buttonState = digitalRead(button);
 
 if (buttonState == HIGH) {
     if(oldVal!='1')
lightHIGH();
oldVal='1';
 }
 else if (buttonState == LOW) {
   if(oldVal!='0')
 lightLOW();
 oldVal='0';
 }
}


...I don't know enough about threading on Arduino's yet but a good resource seems to be located at: http://playground.arduino.cc/Code/TimedAction#Example

aj88

I have 5v at one side of the button, pin 2 and the 10k resistor to ground is on the other side.



johncc


Could you use a wire and connect pin 2 to 5V and to GND ?
I can't see what the problem is with the code, so I want to be sure that pin 2 is high and low.


Poor phrasing, that!

aj88


Erdin, I think it's that aj88 wants to be able to interrupt the delays as to get an immediate output.
To actually do what you are wanting to do you need to look into threading. That is the only way I know how to get rid of the Delay problem.
If it's just that it's always looping to Else
I think something like this might help.
Code: [Select]
char oldVal='2';
int button =2;
int red =9;
int yellow =10;
int green =11;
int red2 =5;
int yellow2 =6;
int green2 =7;
int buttonState =0;
void setup() {
 pinMode(red, OUTPUT);
 pinMode(yellow, OUTPUT);
 pinMode(green, OUTPUT);
 pinMode(red2, OUTPUT);
 pinMode(yellow2, OUTPUT);
 pinMode(green2, OUTPUT);
 pinMode(button, INPUT);
}
 void lightHIGH()
 {
   digitalWrite(yellow, HIGH);
   digitalWrite(yellow2, HIGH);
   digitalWrite(green, LOW);
   digitalWrite(green2, LOW);
   digitalWrite(red, LOW);
   digitalWrite(red2, LOW);
   delay(1000);
   digitalWrite(yellow, LOW);
   digitalWrite(yellow2, LOW);
   digitalWrite(red, HIGH);
   digitalWrite(red2, HIGH);
   delay(3000);
 }
 void lightLOW()
 {
 digitalWrite(red, HIGH);
 digitalWrite(yellow,LOW);
 digitalWrite(green, LOW);
 digitalWrite(red2, LOW);
 digitalWrite(yellow2, LOW);
 digitalWrite(green2, HIGH);
 delay(5000);
 digitalWrite(yellow2, HIGH);
 digitalWrite(green2,LOW);
 delay(1000);
 digitalWrite(red, LOW);
 digitalWrite(green, HIGH);
 digitalWrite(green2, LOW);
 digitalWrite(yellow2, LOW);
 digitalWrite(red2, HIGH);
 delay(3000);
 digitalWrite(green,LOW);
 digitalWrite(yellow,HIGH);
 delay(1000);
 }
void loop() {
 buttonState = digitalRead(button);
 
 if (buttonState == HIGH) {
     if(oldVal!='1')
lightHIGH();
oldVal='1';
 }
 else if (buttonState == LOW) {
   if(oldVal!='0')
 lightLOW();
 oldVal='0';
 }
}


...I don't know enough about threading on Arduino's yet but a good resource seems to be located at: http://playground.arduino.cc/Code/TimedAction#Example
That is what I want, but when I tried your code, it stopped at the end of lightLow. Thank you for your help, I will look into that!

Xnor

Another thing that would work as pointed out by one of the moderators, Nick Gammon, in another post: http://www.gammon.com.au/blink

Go Up