#define echoPin 9 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 8 //attach pin D3 Arduino to pin Trig of HC-SR04
#define LED 7 //attach pin D3 Arduino to pin Trig of HC-SR04
#define OLED 6
#define YLED 5
#define BLED 4
#define GLED 3
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
digitalWrite(LED, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
digitalWrite(LED, 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)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
//Custom Code
if (distance <=100){
digitalWrite(OLED, HIGH);
}
else{
digitalWrite(OLED, LOW);
}
if (distance <=200){
digitalWrite(YLED, HIGH);
}
else{
digitalWrite(YLED, LOW);
}
if (distance <=300){
digitalWrite(BLED, HIGH);
}
else{
digitalWrite(BLED, LOW);
}
if (distance <=400){
digitalWrite(GLED, HIGH);
}
else{
digitalWrite(GLED, LOW);
}
}