I have a simple sketch (my first) for very basic object avoidance. If I leave the USB Cable connected it runs as it should.
If I disconnect the USB cable and run on 6V battery power only then it skips steps.
I am using an Arduino Uno r3 with a Cytron L298P Motor Shield and an HC-SR04 Sensor. I have attached the .ino file.
When USB Attached:
Moves forward till distance < 10" backs up turns right then turns left compares distance then moves in the direction with the greater distance. Exactly as the sketch reads.
On Batteries Only:
Moves forward till < 10" backs up turns right then moves forward if distance is > 10" Then repeats the same right turn only sequence at distance < 10". Does not turn left and compare distance. However I did notice that the appropriate motor channel indicator LEDs flash very briefly instead of actually turning to the left when it should.
Batteries (5 cell rechargeable AA) check out good at 6+ volts. Running 2 new 130 size 5 volt motors.
Any suggestions would be appreciated.
Thanks
BasicObstacleAvoidanceSpider.ino (4.21 KB)
Hi,
What you describe sounds like your microcontroller board is experiencing a brownout.
This can happen if you are using the same power source (the 5xAA batteries) for both the Arduino and the motors and the power source is not powerful enough for peak usage. During those peaks, the voltage drops too low and the microcontroller resets.
This would explain why it suddenly starts going forward again (the default entry point of your finite state machine (FSM)) instead of turning the opposite direction to check the distance.
Looking at your code quickly, there seems to be no pause between turning right and turning left (rDistance scan() ), which may cause a current spike due to the back EMF from trying to reverse both motors right away.
Try out the exact same code, but add a delay after the scan for the right side. Also, you may want to place another small delay after the scan for the left side, too, just to be on the safe side.
As a side note, you may want to change the delay for when (lDistance < rDistance), since when you first see an obstacle, you turn right for 2 s, then left for 4 s. But, if the left distance is smaller, you then turn right 2 s, which would place your robot approximately at the starting angle. Maybe change it to 4 s too?
Let us know if this helps!
Sincerely,
Thanks for the prompt reply and advice.
I had already figured out the power supply issue while trying to sleep last night, and added a separate 9V power supply before going to work this morning. I ran a quick check and it seemed to fix the problem.
I will check out the timing after I get home this evening.
Thanks again I will let you know how everything works.
Best Regards
Art Smith
Well it turns out timing is everything. Adding a 1/2 second stop after each change of direction smoothed everything out tremendously.
I posted pics and the finished sketch over in the projects thread.
Thanks for all the help. Hopefully one day I will be able to return the favor.