Arduino-Self Balancing robot with HC-SRO4

Hello, I am currently making a self balancing robot and I so far had manage to make the Robot to balance it self pretty well, I am using arudino mega, MPU6050, L298N, my issue currently is that when I try to implement the portion of the HC-SR04 sensor, even if just reading the values and the Balance portion of the program will either fail, stop, or go crazy and not respond again, at the button of the attach file I have include my file and/how I try to do the implentation of the HC-SR04 (as we were shown in class).

If someone can guide me to the process of how to implement this to my code so I can make the robot avoid obstacle will be greatly appreciated.

Thank you.

tesdt.ino (7.98 KB)

Does the library use delay()? Does it send a ping and wait until the ping comes back? That will interfere with balancing because the balancing calculation must run hundreds of times per second with no delays.

Try to find an ultrasonic library which uses interrupts instead of waiting.

1 Like