Servo and loop() method mystery

Hello all,
This is my code inside loop() method. int i=0 is declared outside loop method

if (i==0)
{
for (; i<=90; i++)
{
delay(50);
myServo.write(i);
}
}

My expectation was that servo rotates slowly by 90 degrees and stays there forever. However once in some 20 seconds or so, the led L on Arduino blinks and the servo moves quickly back to 0 degrees and rotates slowly upto 90. Looks like loop() method is resetting the counter or is my understanding wrong? Please help

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your complete code.
It will be formatted in a scrolling window that makes it easier to read.

Also, what model Arduino are you using and what version IDE?

Thanks... Tom... :slight_smile:

I'm using Arduino Uno
WIndows 7,
Arduino IDE: 1.7.9
I'm powering my board through my laptop's USB

#include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9);
  myServo.write(0);

}
int i=0;
void loop() {

  for (; i<=90; i++)
  {
    delay(50);
    myServo.write(i);
  }
}

Put in a serial print to see the value of i before and after the for loop.
Like Delta_G, I suspect the arduino is resetting for some reason.

Thank you for the suggestions.
I just printed the value before and after for loops. It indeed is getting reset:
Before:91 After:91
Before:0 After:91 //Resets here
Before:91 After:91
Before:91 After:91
Before:91 After:91

The same issue happened with a simple RGB color mix code I wrote. Delta_G as you say, could it be an issue with the IDE version I'm using?

My servo is drawing power from my USB powered UNO board. I'm using the servo provided in the starter kit. The circuit is set up as illustrated in the Starter Kit.
Another interesting thing I observed was when I had opened the Serial port monitor from my IDE, the Servo was at constant 90 angle. The moment I closed it, I saw servo resetting.

So what do you suggest?

My servo is drawing power from my USB powered UNO board. I'm using the servo provided in the starter kit. The circuit is set up as illustrated in the Starter Kit.

Strange. Why would Arduino kit not mention it then?
Moreover there must something else resetting my Arduino. Because even my LED color mix circuit behaved in a similar manner even though I had written while(true) block.

longshot:
You are writing 91 a bunch of times to the servo code, something in the library might be getting overwhelmed.

After the for loop put in something like:

if (i == 91) delay(50);

See if it takes longer to crash.

madhugubby:
Strange. Why would Arduino kit not mention it then?
...

Because they forgot to vet the examples in that book through this forum. :wink:

madhugubby:
Strange. Why would Arduino kit not mention it then?
Moreover there must something else resetting my Arduino. Because even my LED color mix circuit behaved in a similar manner even though I had written while(true) block.

Now you tell us!

Seriously, you never power motors or servos from logic 5V supply, that's bad news for the logic
chips.

Secondly the problem with the LEDs could be something else or a similar problem (pulling too much
current from 5V or insufficient decoupling).

Does the servo sketch work reliably with no servo connected or powered?

Does the LED code run without reseting with no LEDs connected?

Hi,
try a proper for loop

for (int i=0; i<=90; i++)
{
}

Tom.... :slight_smile:

His loop is perfectly proper, and simply makes it run once, since i is never reset.
After all this:

for (;;) {}

is a common C idiom.

Harbison and Steele specifically state that the initialization does not need to be there.

Hi,

KeithRB:
His loop is perfectly proper, and simply makes it run once, since i is never reset.
After all this:

for (;;) {}

is a common C idiom.

Harbison and Steele specifically state that the initialization does not need to be there.

Yes an idiom,why confuse the situation, write C++ as it is taught.

for (;;){}

is not a for loop

for (int i=0 ; i<=90; i++){}

now thats a for loop.
and thats an idiom
Tom... :slight_smile:

Thank you folks for all the suggestions and information. Looks like it is the damn Serial port that is resetting my board. When I removed all the serial port output, the code ran smooth. Also I read few blogs that said "Auto reset by serial port". There are two workaround suggested. I'll try one of them.
Thanks once agian