Hi,
I'm trying to design a pedestrian crossing using a state machine. The crossing is suppose to be able to detect a button press no matter what the colour of the traffic lights are. i.e. if someone just missed the crossing and pressed the button while the traffic lights were red for cars, the system should remember that and stop traffic in 30s time.
Traffic light sequence is green - orange - red - red &orange - green.
I've managed to create a system that doesn't use the delay() function for timing but i'm not sure how to take it further. It currently has all the same limitations as a similar system that uses the delay() function due to the way I've programmed it.
As is obvious from the post this is my first ever time coding an arduino. Also I have looked at some of the examples (including blinking led) but I think i'm just not getting how to use a state machine. Code is below.
//Declare LEDs
//declare button
int btnPedestrian = 13;
int btnPedestrian2 = 7;
int red = 2;
int redstate = LOW;
unsigned long previoustime1 = 0; // will store last time LED was updated
int orange = 4;
int orangestate = LOW;
int green = 6;
int greenstate = LOW;
unsigned long orangetimer = 0;
unsigned long redtimer = 0;
unsigned long redorangetimer = 0;
unsigned long test = 0;
int counter =0;
void setup (){
//put set up code here:
pinMode(green, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(red, OUTPUT);
//Turn all LEDs off
digitalWrite(green, LOW);
digitalWrite(orange, LOW);
digitalWrite(red, LOW);
//Set button to input
pinMode(btnPedestrian, INPUT);
pinMode(btnPedestrian2, INPUT);
//Serial Monitor
Serial.begin(9600);
}
void loop () {
unsigned long currenttime = millis();
//Sets light to green if button hasn't been pressed and traffic light isn't orange or red
if(orangestate == LOW && redstate == LOW && digitalRead(btnPedestrian)==LOW){
greenstate = HIGH;
digitalWrite(green, greenstate);
}
//If light is green and someone presses button then light will change to orange
else if (greenstate == HIGH && redstate == LOW && orangestate == LOW &&digitalRead(btnPedestrian)==HIGH){
greenstate = LOW;
orangestate = HIGH;
digitalWrite(orange, orangestate);
digitalWrite(green, greenstate);
previoustime1=currenttime;
}
//if light is orange but no red or green then wait 2s then turn red on and orange off
if (currenttime - previoustime1 >= 2000 && orangestate == HIGH && redstate == LOW && greenstate == LOW){
orangestate = LOW;
redstate = HIGH;
test = currenttime - previoustime1;
Serial.print(test);
previoustime1 = currenttime;
Serial.print(previoustime1);
digitalWrite(red, redstate);
digitalWrite(orange, orangestate);
}
//if light is red but no orange or green then wait 5s then turn orange on
if (currenttime - previoustime1 >=5000 && redstate == HIGH && greenstate == LOW && orangestate == LOW){
orangestate = HIGH;
previoustime1 = currenttime;
Serial.print(previoustime1);
digitalWrite(orange, orangestate);
}
//if light is orange and red for 2s then turn green on and turn red and orange off
if (currenttime - previoustime1 >= 2000 && orangestate == HIGH && redstate == HIGH && greenstate == LOW){
redstate = LOW;
orangestate = LOW;
greenstate = HIGH;
previoustime1 = currenttime;
Serial.print(previoustime1);
digitalWrite(orange, orangestate);
digitalWrite(red, redstate);
digitalWrite(green, greenstate);
}
}