I very strongly suspect the code you posted is not the simplest code that would demonstrate your problems. I bet you could remove all of the sensor code and just have a single line that tries to drive the motors.
The behavior you are describing is consistent with incorrectly paralleled outputs. You are driving the motor driver outputs to two different voltages while they are connected, which is repeatedly shorting out the battery and and causing the voltage to drop to a level that is below what the Baby Orangutan needs to run. You might be lucky in this regard, as using a better power supply in this case could easily burn out the motor drivers.
In general, you are trying to do something reasonably advanced by paralleling your batteries and motor drivers, and to pull it off you need to have a much better understanding of the details than you appear to. As an easy example, there are several problems with your program that make it clear you don’t have a firm understanding of what you’re doing:
int rmotorsAA = (3,5);
int rmotorsBB = (6,11);
What are you trying to do here? The compiler is just reducing this to:
int rmotorsAA = 5;
int rmotorsBB = 11;
Also, analogWrite() take an unsigned 8-bit value as its value argument. Have you looked at the command reference for it? Supplying it with a negative number doesn’t make any sense. What kind of output are you expecting from something like analogWrite(pin, -240)?
analogWrite(rmotorsBB, counter);
analogWrite(rmotorsAA, -counter);
Aside from these relatively minor mistakes, you are making a huge assumption that the different motor driver outputs are doing the same things at the same times. Everything you are doing relies upon this very wrong assumption.
You have four motors and four motor drive outputs. Instead of paralleling everything in sight, I think you should just connect each motor to its own motor driver channel while using a more appropriate power supply and control them that way. If you haven’t broken anything, everything should work fine that way.
- Ben