Servo problem


I have problem with servo. In the example below servo should move around forever, but after few rounds it stops. I had this problem in a larger piece of code, but I have stripped everyting unnecessary away from the source.

I am running Arduino UNO delivered in SparkFun Inventor’s Kit with Windows environment.

What should happen: Servo moves around forever (in limit of integer counter)
What happens: Servo stops moving after 9 loops.

I did some investigation with Serial.write and it says intCounter gets random negative value when it should change to zero (see if statement in source).

Did I miss something or is this a bug in Arduino?

Edit: Solved.


const int readingsCount = 9;
float readings[10] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};

floats need to initialize first…

#include <Servo.h>

int servoPin = 3;
int temperaturePin = 1;

const int readingsCount = 9;
float readings[readingsCount];

Servo myservo;

int intCounter = 0;

void setup()

void loop()
  int servoValue = 0;
  float temperature = 0;
  temperature = ((analogRead(temperaturePin) * .004882814) - .5) * 100;
  readings[intCounter] = temperature;

  intCounter = intCounter + 1;

  if(intCounter > readingsCount)
    intCounter = 0;

Take a careful look at the size of your array, and what you index it with. Big clue:

Servo stops moving after 9 loops

This isn't a servo problem, so maybe change the name of the thread.

Initialising the array didn't fix the problem - it was already initialised to zero - but changing the size of the array did the trick.