Obstacle Avoidance

So I made a robot it works and I already checked the HCSR04 sensor is not broken. I just want it to stop when it sees something but when I tried something like this it doesn’t work.

if (distance < triggerDistance)
digitalWrite(Motor1Pin2, LOW);
digitalWrite(Motor1Pin1, LOW);
digitalWrite(Motor2Pin1, LOW);
digitalWrite(Motor2Pin2, LOW);

WHY? Please help I called 911 but they hung up on me :’(

Please help I called 911 but they hung up on me

Perfectly understandable. What does that code not do that you want? Or, what does it do that you don't want?

Hey, It doesn't do anything. When I upload it the robot just moves and you could wave your hand in front of it but it doesn't stop it continues moving. Thank You

You need to use serial to put some debug prints in your code.

You could for a start:

  • Print out the value of distance so you can see if it’s < triggerDistance
  • Print out a “Hi” inside that if(){} so you can see if the logic’s actually taking you there.

You could also show all your code not just that snippet, since the problem could be anywhere… Also a circuit schematic…


How to use this forum