const int Trigger = 32;
const int Echo = 34;
const int Buzzer = 14;
const int LEDR = 13;
const int LEDY = 25;
const int LEDG = 26;
#define SOUND_SPEED 0.034
long duration;
float distanceCm;
void setup()
{
pinMode (LEDR ,OUTPUT) ;
pinMode (LEDY ,OUTPUT) ;
pinMode (LEDG ,OUTPUT) ;
pinMode (Trigger ,OUTPUT);
pinMode (Echo , INPUT);
pinMode (Buzzer ,OUTPUT);
Serial.begin(115200);
}
void loop()
{
digitalWrite(Trigger, LOW);
delayMicroseconds(2);
digitalWrite(Trigger, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger, LOW);
duration = pulseIn(Echo, HIGH);
distanceCm = duration*SOUND_SPEED/2;
Serial.print("PEDESTRIAN CROSSSING !!! ");
//Serial.print(distanceCm);
//Serial.println("cm");
if (distanceCm < 5)
{
digitalWrite(Buzzer, HIGH);
delay(50);
digitalWrite(Buzzer, LOW);
digitalWrite(LEDG, HIGH);
delay(50);
digitalWrite(LEDG, LOW);
}
else if (distanceCm < 10)
{
digitalWrite(Buzzer, HIGH);
delay(100);
digitalWrite(Buzzer, LOW);
digitalWrite(LEDG, HIGH);
delay(100);
digitalWrite(LEDG, LOW);
}
else if (distanceCm < 15)
{
digitalWrite(Buzzer, HIGH);
delay(200);
digitalWrite(Buzzer, LOW);
digitalWrite(LEDG, HIGH);
delay(200);
digitalWrite(LEDG, LOW);
}
else if (distanceCm < 20)
{
digitalWrite(Buzzer, HIGH);
delay(500);
digitalWrite(Buzzer, LOW);
digitalWrite(LEDG, HIGH);
delay(500);
digitalWrite(LEDG, LOW);
}
delay(100);
}