I am trying to simulate a traffic signal with a provision of a pedestrian button, that when pressed, stops the traffic for a while. Here are the conditions I'm trying to meet,
One green LED (on pin 10) lit for 3 seconds (the first LED to light up). - Then one orange LED (on pin 11) lit for 1 second - Then finally one red LED (on pin 12) lit for 3 seconds.
Two-colour light (pedestrian) - The pedestrian light is constantly red (red LED on pin 9) and only turns green (LED on pin 8) once the button (on pin 2) is pressed.
On button press If the button is pressed while the car traffic light is green, the orange light is lit for 1 second, then the car lights turn red for 5 seconds during which the green pedestrian light is green.
After those 5 seconds, the pedestrian light turns red again and normal functioning resumes (car traffic light returns to green)
And here's my code for the same,
int status=0; //mode:state of button pin
int buttonpin=2;//pin related to pushbutton
void setup(){
pinMode(10,OUTPUT); //Green LED to pin10
pinMode(11,OUTPUT); //Orange LED to pin11
pinMode(12,OUTPUT); //Red LED to pin12
pinMode(9,OUTPUT); //Pedestrian Red on pin 9
pinMode(8,OUTPUT); //Pedestrian Green on pin 8
}
void loop(){
digitalWrite(9,HIGH);//Pedestrian Red is on
status=digitalRead(buttonpin);
if (status==HIGH){
digitalWrite(9,LOW); //pedestrian red is off
digitalWrite(8,HIGH); //Pedestrian Green is on
digitalWrite(11,HIGH);//orange lights up
delay(1000);//1second delay
digitalWrite(11,LOW);//orange shut off);
digitalWrite(12,HIGH);//red starts
delay(5000);//5 second delay
digitalWrite(12,LOW);
digitalWrite(9,HIGH);
}
else{
digitalWrite(10,HIGH);//green lights up
delay(3000);//3 second delay
digitalWrite(10,LOW);//green shuts off
digitalWrite(11,HIGH);//orange lights up
delay(1000);//1second delay
digitalWrite(11,LOW);//orange shut off);
digitalWrite(12,HIGH);//red starts
delay(3000);
digitalWrite(12,LOW);//red shuts off
}
}
The problem is, the pedestrian button only works when it is already pressed at the time of Traffic Green starting(because of the if loop right there in code), but once Traffic green has already been green for say 1.5 seconds, it won't work as the if loop condition check has been already passed. How can I mend my code to make the "On button Press" sequence explained above work at any time of the Green LED HIGH?
Attached is the circuit if the need be.
Edited code is:
#include <TimerOne.h>
int status=0; //mode:state of button pin
int buttonpin=2;//pin related to pushbutton
void setup(){
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
Timer1.initialize(100000);
Timer1.attachInterrupt( buttonCheck );
}
void loop(){
digitalWrite(9,HIGH);
status=digitalRead(buttonpin);
digitalWrite(10,HIGH);//green lights up
delay(3000);//3 second delay
digitalWrite(10,LOW);//green shuts off
digitalWrite(11,HIGH);//orange lights up
delay(1000);//1second delay
digitalWrite(11,LOW);//orange shut off);
digitalWrite(12,HIGH);//red starts
delay(3000);
digitalWrite(12,LOW);//red shuts off
}
void buttonCheck(){
if (status==HIGH){
digitalWrite(9,LOW);
digitalWrite(8,HIGH);
digitalWrite(11,HIGH);//orange lights up
delay(1000);//1second delay
digitalWrite(11,LOW);//orange shut off);
digitalWrite(12,HIGH);//red starts
delay(5000);//5 second delay
digitalWrite(12,LOW);
digitalWrite(9,HIGH);
return;
}
}
Will this work?
delay()
calls (using delay isn't a good idea in almost all situations). Instead use non-blocking code with themillis()
function. Refer to theBlinkWithoutDelay
example of the Arduino IDE for this. Also it will be good to learn, how to code a finite state machine (sounds more complex than it is). There are plenty tutorials on the web for this. – chrisl Jun 25 '18 at 19:58