const int Trigger = 6;
const int Echo = 7;
const int LEDpin1 = 4;
const int LEDpin2 = 5;
const int Parameter = 400;
float Time;
void setup()
{
Serial.begin(9600);
pinMode(Trigger, OUTPUT);
pinMode(Echo, INPUT);
pinMode(LEDpin1,OUTPUT);
pinMode(LEDpin2,OUTPUT);
}
void loop()
{
digitalWrite(Trigger, HIGH);
delayMicroseconds(10);
digitalWrite(Trigger, LOW);
Time = pulseIn(Echo, HIGH);
Serial.print("Distance: ");
Serial.println(Time);
delay(10);
if (Time < Parameter)
digitalWrite(LEDpin1, HIGH);
else
digitalWrite(LEDpin1, LOW);
if (Time < Parameter)
digitalWrite(LEDpin2, HIGH);
else
digitalWrite(LEDpin2, LOW);
//**When I include -delay(5000)- right here, it also delays the Ultrasonic sensor to only detect every 5 seconds**
}
Asked
Active
Viewed 55 times
1
Coder9390
- 472
- 4
- 21
Leonardo Morales
- 11
- 1
-
3The answer is using non-blocking code involving the `millis()` function in a pattern like the `BlinkWithoutDelay` example shows. There are many tutorials about that on the web. When you learn that coding principle you will be able to solve your problem – chrisl Nov 20 '21 at 18:56
-
2Please format code as code (you can edit your post) so it's easier to read. – Dave Newton Nov 20 '21 at 20:10