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!