// Define pin numbers
const int trigPin = 12;
const int echoPin = 14;
const int redLED = 2;
const int yellowLED = 13;
const int greenLED = 4;
// Define variables for the duration and the distance
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
Serial.print("Distance: ");
Serial.println(distance);
if (distance > 10 && distance < 50) {
// Car is detected
if (digitalRead(redLED) == HIGH) {
// Transition from red to green through yellow
digitalWrite(redLED, LOW); // Turn Red LED off
digitalWrite(yellowLED, HIGH); // Turn Yellow LED on
delay(3000); // Yellow light duration
digitalWrite(yellowLED, LOW); // Turn Yellow LED off
}
digitalWrite(greenLED, HIGH); // Turn Green LED on
} else {
// No car is detected
digitalWrite(greenLED, LOW); // Turn Green LED off
digitalWrite(yellowLED, LOW); // Ensure Yellow LED is off
digitalWrite(redLED, HIGH); // Turn Red LED on
}
delay(1000); // Delay a second for better visualization
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2