Servo Rotation Problem
hi everyone,
i'm totally newbie in programming , other electronics.
i'm trying program arduino (based on daniel's servo code) have react photoresistor in manner.
the logic code this:
1) photoresistor captures light intensity , transform them analog signal
2) status mark set servo status 0
3) if analog signal greater 64 , status mark 0, turn servo 180 degree
4) once servo reaches 180 degree, stop. status mark set 1
5) step 1
6) if analog signal less 64 , status mark 1, rotate servo 0 degree
7) servo reaches 0 degree, servo stop. status mark set 0
step 1)
as guys can see, logic loop circle if statements in it.
here code:
_______________________
int servopin = 9; // r/c servo connected digital pin 13
int myangle; // angle of servo (roughly in degrees) 0 - 180
int pulsewidth; // function variable
int statuss = 0;
int analogvaluea= 0;
int analogvalueb= 0;
int analogvaluec= 0;
int photocella = 0;
int photocellb = 1;
int photocellc = 2;
int ledpin = 13;
int time;
long randnumber;
void setup()
{
beginserial(9600);
pinmode(1, input); // set pin a1 input
pinmode(0, input); // set pin a1 input
pinmode(2, input); // set pin a1 input
pinmode(9, output); // sets pin d9 output
pinmode(13, output); // sets pin d13 output
}
void servopulse(int servopin, int myangle) // servo function
{ // function determining our pulsewidth servo
pulsewidth = (myangle * 6) + 320; // determines our delay below (for standard pot)
digitalwrite(servopin, high); // set servo high
delaymicroseconds(pulsewidth); // wait small amount (determined pulsewidth)
digitalwrite(servopin, low); // set servo low
delay(20); // refresh cycle of typical servos (20 ms)
}
void loop()
{
time = millis();
randnumber = random(1,4); // return random number 1 - 3
serial.print(randnumber);
serial.print(" ");
digitalwrite(ledpin, high); // sets led on
analogvaluea = analogread (photocella);
analogvalueb = analogread (photocellb);
analogvaluec = analogread (photocellc);
serial.print(analogvaluea, dec); //prints analog value in serial message
serial.print(" ");
serial.print(analogvalueb, dec); //prints analog value in serial message
serial.print(" ");
serial.print(analogvaluec, dec); //prints analog value in serial message
serial.print(" ");
serial.print(randnumber);
serial.print(" ");
serial.print(statuss);
serial.print(" ");
if ((analogvaluea >= 64) , (statuss == 0)){
for (myangle=0; myangle<=180; myangle++) { // cycle through every angle (rotate servo 180 slowly)
servopulse(servopin, myangle);
statuss = 1;
//digitalwrite(servopin, low); // set servo low
//delay(20); // refresh cycle of typical servos (20 ms)
}
}
if ((analogvaluea <= 64) , (statuss == 1)){
for (myangle=180; myangle>=0; myangle--) { // cycle through every angle (rotate servo 180 slowly)
servopulse(servopin, myangle);
statuss = 0;
// digitalwrite(servopin, low); // set servo low
// delay(20); // refresh cycle of typical servos (20 ms)
}
}
delay(1000);
}
________________________________
the photoresistors , random value works fine. the problem i'm having once servo motor rotates 180 degrees , setting status mark 1, instead of rotating 0 degree , set status mark 0, servo motor tries continue going forward (180degree +).
the reason servo tries go forward because servo jerking 180 degrees side , not jerking backward @ all.
can please have @ code , give me suggestion on how fix problem?
thank you
i'm totally newbie in programming , other electronics.
i'm trying program arduino (based on daniel's servo code) have react photoresistor in manner.
the logic code this:
1) photoresistor captures light intensity , transform them analog signal
2) status mark set servo status 0
3) if analog signal greater 64 , status mark 0, turn servo 180 degree
4) once servo reaches 180 degree, stop. status mark set 1
5) step 1
6) if analog signal less 64 , status mark 1, rotate servo 0 degree
7) servo reaches 0 degree, servo stop. status mark set 0
step 1)as guys can see, logic loop circle if statements in it.
here code:
_______________________
int servopin = 9; // r/c servo connected digital pin 13
int myangle; // angle of servo (roughly in degrees) 0 - 180
int pulsewidth; // function variable
int statuss = 0;
int analogvaluea= 0;
int analogvalueb= 0;
int analogvaluec= 0;
int photocella = 0;
int photocellb = 1;
int photocellc = 2;
int ledpin = 13;
int time;
long randnumber;
void setup()
{
beginserial(9600);
pinmode(1, input); // set pin a1 input
pinmode(0, input); // set pin a1 input
pinmode(2, input); // set pin a1 input
pinmode(9, output); // sets pin d9 output
pinmode(13, output); // sets pin d13 output
}
void servopulse(int servopin, int myangle) // servo function
{ // function determining our pulsewidth servo
pulsewidth = (myangle * 6) + 320; // determines our delay below (for standard pot)
digitalwrite(servopin, high); // set servo high
delaymicroseconds(pulsewidth); // wait small amount (determined pulsewidth)
digitalwrite(servopin, low); // set servo low
delay(20); // refresh cycle of typical servos (20 ms)
}
void loop()
{
time = millis();
randnumber = random(1,4); // return random number 1 - 3
serial.print(randnumber);
serial.print(" ");
digitalwrite(ledpin, high); // sets led on
analogvaluea = analogread (photocella);
analogvalueb = analogread (photocellb);
analogvaluec = analogread (photocellc);
serial.print(analogvaluea, dec); //prints analog value in serial message
serial.print(" ");
serial.print(analogvalueb, dec); //prints analog value in serial message
serial.print(" ");
serial.print(analogvaluec, dec); //prints analog value in serial message
serial.print(" ");
serial.print(randnumber);
serial.print(" ");
serial.print(statuss);
serial.print(" ");
if ((analogvaluea >= 64) , (statuss == 0)){
for (myangle=0; myangle<=180; myangle++) { // cycle through every angle (rotate servo 180 slowly)
servopulse(servopin, myangle);
statuss = 1;
//digitalwrite(servopin, low); // set servo low
//delay(20); // refresh cycle of typical servos (20 ms)
}
}
if ((analogvaluea <= 64) , (statuss == 1)){
for (myangle=180; myangle>=0; myangle--) { // cycle through every angle (rotate servo 180 slowly)
servopulse(servopin, myangle);
statuss = 0;
// digitalwrite(servopin, low); // set servo low
// delay(20); // refresh cycle of typical servos (20 ms)
}
}
delay(1000);
}
________________________________
the photoresistors , random value works fine. the problem i'm having once servo motor rotates 180 degrees , setting status mark 1, instead of rotating 0 degree , set status mark 0, servo motor tries continue going forward (180degree +).
the reason servo tries go forward because servo jerking 180 degrees side , not jerking backward @ all.
can please have @ code , give me suggestion on how fix problem?
thank you
does have suggestion?
my problem has gotten bad worst, i've implemented daniel's simple servo code arduino , still doesn't work.
for reason, servo not go 0 degree , re-perform 180 degree turn. note did not change on daniel's code, missing in coding part?
my problem has gotten bad worst, i've implemented daniel's simple servo code arduino , still doesn't work.
for reason, servo not go 0 degree , re-perform 180 degree turn. note did not change on daniel's code, missing in coding part?
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Servo Rotation Problem
arduino
Comments
Post a Comment