Please Help!
Code: Select all
const byte servopin = 10;
int currentPosition = 600;
int commandPosition = 600;
int Period = (20*1000);
byte rotateSpeed = 1;
int increment = 2;
void setup(){ //void setup start
Serial.begin(115200);
pinMode(servopin,OUTPUT);
}
void rotate(){
digitalWrite(servopin,HIGH);
delayMicroseconds(currentPosition);
digitalWrite(servopin,LOW);
delayMicroseconds(Period - currentPosition);
}
void loop(){
int data = Serial.read();
if (Serial.available() > 0) { //if serial available start
int data = Serial.read();
if (data =='a'){
commandPosition = 600;
}
if (data =='s'){
commandPosition = 1050;
}
if (data =='d'){
commandPosition = 1500;
}
if (data =='f'){
commandPosition = 1950;
}
if (data =='g'){
commandPosition = 2400;
}
}// if serial.available end
/******************************************
Set of command to assign value from
commandPosition to currentPosition
******************************************/
if (commandPosition > currentPosition){
currentPosition = (currentPosition + increment);
rotate();
}
if (commandPosition < currentPosition){
currentPosition = (currentPosition - increment);
rotate();
}
if (commandPosition == currentPosition){
currentPosition = commandPosition;
digitalWrite(servopin,LOW); // stop sending pulses
} // as soon as the desired position is
// reached
// Rotation speed
delay(rotateSpeed);