int LED1 = 7;
int LED2 = 6;
int LED3 = 5;
int LEDpeaton = 4;
#define PIN_TRIG 3
#define PIN_ECHO 2
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LEDpeaton, OUTPUT);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// Start a new measurement:
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
Serial.print("Distance in inches: ");
Serial.println(duration / 148);
if ((duration / 58)< 300)
{
digitalWrite(LED3, HIGH);
delay(5000); // Wait for 1000 millisecond(s)
digitalWrite(LED3, LOW);
digitalWrite(LEDpeaton, LOW);
}
else
{
digitalWrite(LED1, HIGH);
digitalWrite(LEDpeaton, HIGH);
}
delay(1000);
}