Hi,
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.
Solution:
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()
{
myservo.attach(servoPin);
}
void loop()
{
int servoValue = 0;
float temperature = 0;
temperature = ((analogRead(temperaturePin) * .004882814) - .5) * 100;
readings[intCounter] = temperature;
myservo.write(50);
delay(300);
myservo.write(60);
delay(300);
intCounter = intCounter + 1;
if(intCounter > readingsCount)
{
intCounter = 0;
}
delay(100);
}