system
December 25, 2014, 10:09pm
1
Hi I am trying to control a servo using the for function. But my variable starts automatically at 1169 not at 1500 as I asked my program.
here's my code thank you!
#include <Servo.h>
Servo servo;
void setup()
{
servo.attach(9);
Serial.begin(9600);}
void loop()
{
servo.write(1500);
delay(2000);
for(int x; x>=544; x-=25){
servo.write(x);//
delay(200);
Serial.print(" x= ");
Serial.print(x);
}
Serial.print('\n');
delay(3000);
}
Here, my servo starts at 1169.I tried to find out why. but I am unable to fix my problem.
for(int x; x>=544; x-=25){
servo.write(x);//
Thank you for your help!
You have not given x a value when declaring it as part of the for loop. The initial value of local variables when not given an initial value is undefined.
system
December 25, 2014, 10:28pm
3
at the begining? if I assign a value for my x at the begining, it will only make the for once.
system
December 25, 2014, 10:32pm
4
oh it's fine thx
#include <Servo.h>
Servo servo;
void setup()
{
servo.attach(9);
Serial.begin(9600); //imprimer les données recues - la fréquence
}
void loop()
{
servo.write(1500);
delay(2000);
for(int x=1500; x>=544; x-=25){
servo.write(x);//120
delay(200);
Serial.print(" x= "); //imprimer les données sous...
Serial.print(x);
}
Serial.print('\n');
delay(3000);
}
zoomkat
December 26, 2014, 12:12am
5
Note that if you are using microsecond values, you need to use the below write format.
servo.writeMicroseconds(1500);