Code only running when the serial monitor is open

I'm trying to use the MPU6050 gyroscope to control the position of some servos.

To do that I took the example MPU6050 sketch, and used the numbers it printed (for the gyroscope's position) to calculate the servo positions.

The example sketch requires you to open the serial monitor and enter something to start the gyroscope, so I tried to get rid of that by putting // before the code that I thought was responsible.

I'm trying to get the whole thing to run off an external battery pack, but it doesn't always work. Sometimes if I plug in the battery it will start off OK, but then all the servos will freeze up after a minute or so. Everything will start working again if I plug it into my PC and open the serial monitor.

Could someone help me figure out how to get it to work without being connected to a PC?

(I've attached the code).

rocket_vane_stabilisation__experiment_.ino (18.5 KB)

Im not great at code but if it was my project i would try running from a power supply rather than a battery pack to see if you have a code problem or a lack of power from a battery pack compared to the usb power supply. The fact it starts of working then stops seems to point towards a power problem.

Do you mean like a 9V power supply coming from the mains? I don't think I have one with me right now :frowning:

But I do have a big LiPo battery in my project connected to an ESC/BEC thing that can output 5.5V. Would it be possible to use 5.5V to power the Arduino by plugging it straight into the 5V pin?

try to power the servos from the 5.5 supply and supply arduino from battery

    while (!Serial); // wait for Leonardo enumeration, others continue immediately

Do you have a Leonardo? If not, you don't need that code.

If you do, what do you think that statement is doing? If you said "waiting for me to open the Serial Monitor app", you'd be right. If you said anything else, you'd be wrong.

If you don't want the sketch to wait for you to open the Serial Monitor app, why did you put that code in?

Are you powering the servos adequately? This is a common problem - your servos may need an amp
each (or more if high torque).

PaulS:

    while (!Serial); // wait for Leonardo enumeration, others continue immediately

Do you have a Leonardo? If not, you don't need that code.

If you do, what do you think that statement is doing? If you said "waiting for me to open the Serial Monitor app", you'd be right. If you said anything else, you'd be wrong.

If you don't want the sketch to wait for you to open the Serial Monitor app, why did you put that code in?

I didn't write the code myself, I simply took an example sketch and tried modifying it.
I'm using an Uno but I tried removing that code just in case, and it didn't seem to help.

I'll have a go at getting a better power supply to see if that's the problem.

On a uno you can use a power supply up to 12vdc via the power jack on the board. Now according to other people on the forum you should avoid using a power supply that's over 5vdv and less than 7.2vdc.

I normally use a 12vdc power brick and im sure if you look around the home you can find one that you can borrow for a test. (plugs straight into the power jack on the uno do not connect it to the pins)

while (!Serial); // wait for

... wait until there is serial. If there is no serial, that means wait for ever :wink:

put a && time < 3 (seconds) in it. Then after 3 seconds without serial the code continues.

gpop1:
On a uno you can use a power supply up to 12vdc via the power jack on the board. Now according to other people on the forum you should avoid using a power supply that's over 5vdv and less than 7.2vdc.

Those "other people on the forum" are WAY off. If you are using the power jack, the MINIMUM voltage to get 5V out of the regulator is 6.8V. The maximum voltage to get maximum current (1000 mA) out of the regulator is 7.66V. Higher voltages will overheat the regulator if you draw 1000 mA. The ideal range is between 6.8V and 7.66V but for current draws under 1000 mA you can use higher voltages. Recommended maximum is 12V.

Tobey83:

while (!Serial); // wait for

... wait until there is serial. If there is no serial, that means wait for ever :wink:

OP said he has an Uno. The man page for the serial library makes it clear that if(Serial) is always true for non-Leonardo style boards, and by extension the same would be the case for while(). I just tested it: stick the line while (!Serial); in setup() of blink and it will still go into loop() and blink with the serial monitor closed. To be doubly sure I even ran my Uno off a power supply into the jack without the usb cable even plugged in.

Edit: and just because I'm paranoid, I tested on a Micro as well, Micro being a small Leonardo. Blink doesn't blink with that line while (!Serial); in setup(), until the monitor is opened.

So for me it works as expected: Serial is always true on Uno, and true on a Micro only when the monitor is opened.

johnwasser:
Those "other people on the forum" are WAY off. If you are using the power jack, the MINIMUM voltage to get 5V out of the regulator is 6.8V. The maximum voltage to get maximum current (1000 mA) out of the regulator is 7.66V. Higher voltages will overheat the regulator if you draw 1000 mA. The ideal range is between 6.8V and 7.66V but for current draws under 1000 mA you can use higher voltages. Recommended maximum is 12V.

Thanks for the correction john. I cut and pasted what you wrote into my arduino folder just in case I need it in the future.

Had a go at powering the servos with a LiPo and the Arduino/gyroscope seperately with the battery pack, and it didn't seem to fix anything.

What I did notice is that after I open the serial monitor to get the code running, I can disconnect the Arduino from my PC and have everything continue to run fine for a bit.

But if I shake the gyroscope around alot, the servos go a bit crazy and start jittering about. So I tried doing the same thing with the serial monitor open and found that I'd get random numbers coming in from the gyroscope if it gets shaken up too much.

I also found that sometimes it would just suddenly stop sending data when I moved it slowly. When I tried restarting it the initialisation would fail. Could there be a loose connection somewhere causing the problem?

Screenshots:

Edit:
Connections to the MPU6050 - http://imgur.com/LZjVjr0
Might try something else to connect it.