Hi guys, I'm doing a project in which I use the serial monitor to tell the servo what degree i want it at. It works fines except that it doesn't register the first digit that I write (for example, if I want it to go to 180 degrees, i have to write 1180 and if i want it to go to 90 degrees i need to write 990). Anyone have any idea why this is happening. This is my code, bellow, there's a bunch of stuff that has nothing to do with servos so I'm sorry about that
#include <Servo.h>
Servo servo1;
long num1;
void setup() {
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
//Setup for Servo 1
servo1.attach(10);
Serial.begin(9600);
}
void loop()
{
while(Serial.available())
{
char In=Serial.read();
if(In=='w' || In=='W') // Forward @ full speed
{
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(9, LOW); //Disengage the Brake for Channel A
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(3, 255); //Spins the motor on Channel A at full speed
analogWrite(11, 255); //Spins the motor on Channel B at full speed
}
else if(In=='s' || In=='S') // Stop
{
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(9, HIGH); //Engages the Brake for Channel A
digitalWrite(8, HIGH); //Engages the Brake for Channel B
analogWrite(3, 0); //Spins the motor on Channel A at zero
analogWrite(11, 0); //Spins the motor on Channel B at zero
}
else if(In=='a' || In=='A') // Slow Left
{
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(9, LOW); //Disengages the Brake for Channel A
digitalWrite(8, LOW); //Disengages the Brake for Channel B
analogWrite(3, 0); //Spins the motor on Channel A at zero
analogWrite(11, 90); //Spins the motor on Channel B at 90
}
else if(In=='q' || In=='Q') // Fast Left
{
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(13, HIGH); //Establishes backwards direction of Channel B
digitalWrite(9, LOW); //Disengages the Brake for Channel A
digitalWrite(8, LOW); //Disengages the Brake for Channel B
analogWrite(3, 90); //Spins the motor on Channel A at 90
analogWrite(11, 90); //Spins the motor on Channel B at zero
}
else if(In=='d' || In=='D') // Slow Right
{
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(9, LOW); //Disengages the Brake for Channel A
digitalWrite(8, LOW); //Disengages the Brake for Channel B
analogWrite(3, 90); //Spins the motor on Channel A at 90
analogWrite(11, 0); //Spins the motor on Channel B at zero
}
else if(In=='e' || In=='E') // Fast Right
{
digitalWrite(12, HIGH); //Establishes backwards direction of Channel A
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(9, LOW); //Disengages the Brake for Channel A
digitalWrite(8, LOW); //Disengages the Brake for Channel B
analogWrite(3, 90); //Spins the motor on Channel A at 90
analogWrite(11, 90); //Spins the motor on Channel B at 90
}
else if(In=='z' || In=='Z') // Backwards
{
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(9, LOW); //Disengages the Brake for Channel A
digitalWrite(8, LOW); //Disengages the Brake for Channel B
analogWrite(3, 255); //Spins the motor on Channel A at fullspeed
analogWrite(11, 255); //Spins the motor on Channel B at fullspeed
}
num1= Serial.parseInt();
Serial.print(num1);
servo1.write(num1);
}
}