Arduino Uno Lagging

Hey guys,

What my code is supposed to do is that when the value of the ultrasonic sensor drops below 50 CM, Both Piezos should go off.

But, What's happening is that the Arduino just lags out of existence when the value is <50. Like the Serial monitor record everything very fast but when the value is <50, it takes like 1 reading every 2 seconds. The TX Blink light on my Arduino also does the same. When value is >50, Its always on, but when its <50, It blinks once every 2 seconds or so. How do I fix this?

Code:

#define RechoPin 9 
#define RtrigPin 8

long duration; 
int distance; 
int RPiezo (6);
int LPiezo (5);

void setup() {
  pinMode(RtrigPin, OUTPUT); 
  pinMode(RechoPin, INPUT); 
  Serial.begin(9600); 
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); 
  Serial.println("with Arduino UNO R3");
}
void loop() {
 
  digitalWrite(RtrigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(RtrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(RtrigPin, LOW);
   
  duration = pulseIn(RechoPin, HIGH);
  
  distance = duration * 0.034 / 2; 
 
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  if
    (distance < 50) {
    digitalWrite(RPiezo, HIGH);
    digitalWrite(LPiezo, HIGH);
    delay (750);
    digitalWrite(RPiezo, LOW);
    digitalWrite(LPiezo, LOW);
    delay(750);
    
  }
}

I triple checked all of the connections and they were all right, and I'm pretty sure my Arduino UNO is not a clone.

Please help.

Delay is 1.5 seconds as you have 2 * delay(750); in your code.

Two delays of 750ms would do that.

Oh yeah, I forgot to mention, The piezos do not turn on when value is below 50.

Replace your piezos with LEDs and see if they go on-off.

Ok, now

Don't be mad at me but
I kind of...

Don't have LEDs :disappointed_relieved:

I know, I know

I have all these sensors but not an LED. So, I tried it with the In-Built LED.

No, it did not work. The serial monitor just kept showing zero with 1 recording every like 2 seconds. connections were all secure and right.

Code:

#define RechoPin 9 
#define RtrigPin 8

long duration; 
int distance; 

void setup() {
  pinMode(LED_BUILTIN, OUTPUT); 
  Serial.begin(9600); 
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); 
  Serial.println("with Arduino UNO R3");
}
void loop() {
 
  digitalWrite(RtrigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(RtrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(RtrigPin, LOW);
   
  duration = pulseIn(RechoPin, HIGH);
  
  distance = duration * 0.034 / 2; 
 
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  if
    (distance < 50) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay (750);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(750);
    
  }
}

Have you proven that the piezos will operate with 5 volts applied? Piezos usually need AC in order to operate. Like in your smoke alarm, they are part of an oscillator circuit.

digitalWrite(LED_BUILTIN, HIGH);
delay (750);
digitalWrite(LED_BUILTIN, HIGH);

HIGH and HIGH?
Try HIGH and LOW.

There goes my last 3 braincells.

I understood nothing. No offense but, can you explain a bit more in umm...

Layman's terms?

Those are layman's terms. Connect one of your piezos to 5 volts + and -, and see if it works!

They do work.

Ok, I fixed that, but now the LED is always blinking no matter what distance the obstacle is and the serial monitor still is 0

Hi,
it's just printing 0 because you haven't set pinModes().

  pinMode(RtrigPin, OUTPUT); // Sets the RtrigPin as an Output
  pinMode(RechoPin, INPUT); // Sets the RechoPin as an Input

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.