The objective of this project consists to make a traffic signal simulador with push button for pedestrian, using an arduino on the SimulIDE platform.
The push button works reducing the time that the green light is on. To turn the action of press the push button responsive, the arduino was codified in a way that the delay time stipulated to wait the green light turns off was fractionated inside a loop, besides a break condition was gave.
Here, I let the complete code used to make this simulation.
/* OUTPUT PINS */
int pinRed = 2;
int pinYellow = 3;
int pinGreen = 4;
/* TIME CONDITIONS */
bool escape = false;
int counter = 0;
/* INPUT PIN */
int pinButton = 0;
/* FUNCTION TO LOAD THE INITIAL CONDITIONS */
void setup() {
pinMode(pinRed, OUTPUT);
pinMode(pinYellow, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(pinButton, INPUT);
}
/* MAIN FUNCTION */
void loop() {
digitalWrite(pinRed, LOW);
for(int i=0; i<1000; i++){
digitalWrite(pinGreen, HIGH);
delay(10);
if(digitalRead(pinButton) == HIGH && i<800){
escape = true;
}
if(escape){
counter++;
if(counter > 200){
counter = 0;
escape = false;
break;
}
}
}
digitalWrite(pinGreen, LOW);
digitalWrite(pinYellow, HIGH);
delay(2000);
digitalWrite(pinYellow, LOW);
digitalWrite(pinRed, HIGH);
delay(5000);
}
------------------------------------------------------------------------------------------------
To sumarise the code, we have two conditions:
- First: Noone came, so no one push the button.
In that case the green light appears for 10 seconds until the yellow.
- Second: Someone came and push the button.
In that case, if the signal is green and we have less than 2 seconds until the signal changes, it changes the pedestrian will wait only 2 seconds for it.
The fractionation of the delay time of the green light was the solution thought to capt the pedestrian action and make the condition to it.
The circuit created on the SimulIDE can see above:
Comentários
Postar um comentário