unable to repeatedly increase servo degree.

Hello,
I've write a code that listen to the serial for a number and send it to the servo(SG-90) to move.
When I send to the serial 10 its move 10 degrees, and after when i send 10 degree its not move(any number).
When I send a number, and then the negative value of the number its goes back -number.

for example:

currentAngle: 10
pos: 10
currentAngle: 20 // not moving at all
pos: 10// not moving at all
currentAngle: 30// not moving at all
pos: 10// not moving at all
currentAngle: 40// not moving at all
pos: 10// not moving at all
currentAngle: 50// not moving at all
pos: 10// not moving at all

and when I send number and -number its move:

currentAngle: 10// moving
pos: 10
currentAngle: 0 //not accepting another number than its negative.
pos: -10
currentAngle: 100 // moving
pos: 100
currentAngle: 0
pos: -100
currentAngle: 100//not accepting another number than its negative.
pos: 100

this is the code:

#include <Servo.h>

Servo servo1;  
int readInt;
boolean flag = false;
int currentAngle = 0;
void setup()
{  
   servo1.attach(8);
   servo1.write(currentAngle);
   Serial.begin(9600); 
}
void moveServo(int pos){
    Serial.print("currentAngle: ");
    Serial.println(currentAngle);
    Serial.print("pos: ");
    Serial.println(pos);
    servo1.write(pos);
}
void loop() 
{ 
  while(Serial.available()>0) //Allocating the data from Serial Monitor.
  { 
    delay(3);  
    int c = Serial.parseInt();
    readInt += c;
    flag = true;
  }
  if(flag){
    currentAngle += readInt;
    moveServo(readInt);
    readInt = 0;
    flag = false;
  }
  delay(1000);
}

Thanks!

currentAngle += readInt;
moveServo(readInt);

Shouldn't that be

 moveServo(currentangle);

UKHeliBob:

currentAngle += readInt;

moveServo(readInt);



Shouldn't that be


moveServo(currentangle);

Hello,
Thanks for your help.
I don’t think I need to send the ‘currentAngle’ variable because it’s holding the whole amount of history I have done so far, so if I have moved 70, you say I need to send 70+serial input which is not my request.

What do you want it to do if you send 10 and then 10 again?

Eshk12:
Hello,
Thanks for your help.
I don’t think I need to send the ‘currentAngle’ variable because it’s holding the whole amount of history I have done so far, so if I have moved 70, you say I need to send 70+serial input which is not my request.

Let's rephrase that:

Your code should definitely be:

 moveServo(currentangle);

for it to work. Servo's take an angle in degrees which is an absolute destination position,
not a move amount. This value is being repeatedly sent to the servo 20 times a
second, so if it were a move amount, the servo would run off the endstops immediately.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.