Sketch stops working if USB is unplugged while motors are not running.

Hey there,

I have an Arduino Uno connected to s shield which control two LEGO NXT motors. I have sketch with cpp libraries that you can find below. (I have messed around the code trying to fix the problem. Sorry for messy parts/duplicates).

I plug the usb in and then plug in the batteries. Then I upload my code which takes input from Radio to control motors. What it is suppoed to is to get simple packets in form '1\n' or '5\n' that correspond to commands. It works perfectly fine when I send commands through radio and Arduino is both plugged in to USB and batteries. However, it stops executing commands over radio if I unplug from USB while motors are not running. If I unplug while motors are running, it keeps working without a problem.

So far I have checked the power all around the board which seems to be good. I have sent radio commands from a different computer to which Arduino is not connected to and it works that way as well but stops working if USB is unplugged. Tried different Arduino Uno, shield and a motor controller. It didn't make a change.

I suspect the problem is I mess up with the serials somewhere in the code. However, I am a beginner so I wasn't good at debugging it.

In the code Comms is where I get and process the radio commands.

ArduinoComms.ino is the entry point.

SDPArduino is a section of the file which is provided by my university for motor control. However, this file has been in use for years and seems to work fine with everything. I just added the part which is concerned with serials.

NxtControl is the file which is the interface for motor control.

Thank you for your help.

Comms.cpp (1.4 KB)

ArduinoComms.ino (390 Bytes)

NxtControl.cpp (3.07 KB)

SDPArduino.cpp (605 Bytes)

how are you powering the Arduino, if not thru the USB?

gcjr:
how are you powering the Arduino, if not thru the USB?

A battery pack. While on battery power I checked the power over each component. They are at correct levels.

Garlex:
A battery pack.

Are the motors powered by the 5V pin on the arduino? Which battery pack is used?

Whenever you switch on a motor, it draws a lot of current until it reaches the desired speed whereafter the current draw settles. If the initial current is not available the motors will not spin up. If the battery pack needs voltage regulation (eg. 9V to 5V), the available current may be much lower than required due to the limitations of the linear voltage regulator on the arduino.

Danois90:
Are the motors powered by the 5V pin on the arduino? Which battery pack is used?

Whenever you switch on a motor, it draws a lot of current until it reaches the desired speed whereafter the current draw settles. If the initial current is not available the motors will not spin up. If the battery pack needs voltage regulation (eg. 9V to 5V), the available current may be much lower than required due to the limitations of the linear voltage regulator on the arduino.

Motors are powered by the shield on top and batteries are connected to the shield instead of Arduino. According to the specs I have from university the shield handles the power regulation.

I have tested if it battery power problem by making motors run in all directions in a simple Sketch. I uploaded it to the Arduino. Plugged it off from USB and turned it back on again with only on battery power. It works perfectly fine that way. Motors reach 100% power without a problem.