#include LiquidCrystal lcd(13, 7, 9, 8, 10, 11); const int trigPin = 12; const int echoPin = 6; long duration; int distanceCm, distanceInch; int GREEN = 4; int YELLOW = 3; int RED = 2; int DELAY_GREEN = 5000; int DELAY_YELLOW = 2000; int DELAY_RED = 5000; void setup() { lcd.begin(16,2); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(GREEN, OUTPUT); pinMode(YELLOW, OUTPUT); pinMode(RED, OUTPUT); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distanceCm= duration*0.034/2; distanceInch = duration*0.0133/2; lcd.setCursor(0,0); lcd.print("Distance: "); lcd.print(distanceCm); lcd.print(" cm"); delay(10); lcd.setCursor(0,1); lcd.print("Distance: "); lcd.print(distanceInch); lcd.print("inch"); delay(10); green_light(); delay(DELAY_GREEN); yellow_light(); delay(DELAY_YELLOW); red_light(); delay(DELAY_RED); } void green_light() { digitalWrite(GREEN, HIGH); digitalWrite(YELLOW, LOW); digitalWrite(RED, LOW); } void yellow_light() { digitalWrite(GREEN, LOW); digitalWrite(YELLOW, HIGH); digitalWrite(RED, LOW); } void red_light() { digitalWrite(GREEN, LOW); digitalWrite(YELLOW, LOW); digitalWrite(RED, HIGH); }