Controlling servo via serial monitor

I am wanting to be able to control a servo motor via the serial monitor. I have tried this simple sketch in the online simulator 123d circuits and it works fine. However I cannot get it to work in the real world. I am using an Arduino Uno and have tried to program it from the Arduino IDE on a Mac and a Raspberry Pi. Any ideas on why it doesn’t work? Thanks.

-John

//begin code

#include <Servo.h>

Servo myServo;

void setup()
{
Serial.begin(9600);
myServo.attach(4);
myServo.write(0);
}
void loop()
{
if(Serial.available() > 0)
{
int data = Serial.read();

if(data == ‘a’)
{
for(int i = 0; i< 180; i++)
{
myServo.write(i);
delay(25);
}
}
}
}

I tried again and got it to work. Don't know why it didn't work the other times I tried.

make sure your servo power supply is robust.

You may be interested in Serial Input Basics

…R