One of the most common mistakes in Arduino robotics projects is not bad code — it is bad power management.
Students often try to power everything (motors, sensors, LEDs) directly from the Arduino's 5V pin. Motors, like muscles in the body, draw high energy. If you try to pull that energy from the brain (the Arduino), blood to the brain drops and it faints (resets). Sudden robot freezes and sensors going wild are about 90% caused by this.
So what is the tip?
Take motor power from an external battery. But there is a critical detail: the Common Ground (GND) Rule. Even if you use different power sources (one for Arduino, one for motors), you must connect the negative (-) terminals (GND) of the batteries together. If you do not join the grounds, the circuit is incomplete and your robot's parts cannot speak the same language.
Remember: perfect software can only run on stable, correctly powered hardware.