Setup() runs multiple times arbitrarily . . .

I am running a relatively basic program that runs some sensors, and based on their input, moves stepper motors.

From what I understand (correct me if I’m wrong), the code will run through setup once, and then proceed into void loop() ‘forever’.

What happens with our code is that it will run through the loop, but once in a while, at arbitrary points, it will go back to setup and run those processes again, and then continue into loop, as if the program has been restarted. Our code does use the serial monitor (baud rate of 9600), and uses a few Serial.print statements, our arduino and sensors are powered via usb, while the motor is powered by an external power source.

Has anyone encountered this problem, or does anyone know of a fix? If you need the code let me know. I’m hoping this is just a general rule about coding that I am overlooking and can apply to my program.

Thanks!

-Ajay

Can you post up the code to look at, then someone may be able to help.

Mowcius

Sounds like you're getting resets.

Does it? Really? I hadn't noticed! ;D

Mowcius

Woohoo! Fixed it, but not sure why the fix worked . . . .

When we removed our serial connection from the code, this problem went away. Does anyone know why this might be the case?

I would post the code, but it is for a class project, and we don't want the TA's thinking we got our code from the internet. I may post it when the due date passes

You could be running out of RAM memory, which can cause mysterious crash/reset when (for example) return addresses on the stack overlap with static data.

Removing serial code would remove a 128-byte chunk of buffer memory that might move you back from the edge…

Possibilities:

  • out of memory (removing Serial reduces the memory use with 128B)
  • pointer error

If you stop & restart the serial connection, the arduino will reset to the beginning again.

If you stop & restart the serial connection, the arduino will reset to the beginning again.

Yes, I had just thought of that as well. Were you connecting and disconnecting from the arduino serial, or was the computer/other device doing it?

Mowcius