How to control Servo via Serial??!!

Can someone tell me how to control a servo with serial inputs from the keyboard
Like :
If i click
1(On the keyboard) then the servo should go 30 degrees to the left.
2(On the keyboard) then the servo should go 30 degrees to the right.

So that if I enter "111" and click "Enter" , then
the servo goes 90 degrees to the left

So that if I enter "11" and click "Enter" , then
the servo goes 60 degrees to the left

So that if I enter "1111" and click "Enter" , then
the servo goes 120 degrees to the left

and so on...

And the servo should not rotate back when it has gone to maximum of 180 degrees or minimum of 0 degrees, until and unless I rotate it by clicking either "1" or "2".

This is for making a turret in my RC car with a LED.I place the LED on the Servo and rotate the servo and flash the LED once to shoot.

Can someone tell me how to control a servo with serial inputs from the keyboard Like :

You will need to write an application for your computer to send your keyboard commands via the serial port to the arduino, and load code on your arduino to decode the commands into servo commands.

Could you tell me how to make a servo go to a pre set position:(Or give me the code to this small bit listed below)

If I press "1" on the keyboard
Then the servo goes to 90 degrees which is the middle of 0-180 degrees.

Below is some simple code that uses the serial monitor to control a servo. You are on your own to write an application for your pc to read keyboard presses.

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(1);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n;
    char carray[6]; //converting string to number
    readString.toCharArray(carray, sizeof(carray));
    n = atoi(carray); 
    myservo.writeMicroseconds(n); // for microseconds
    //myservo.write(n); //for degees 0-180
    readString="";
  } 
}

Is there an easier method because this is too complicated for me?Or maybe is there a tutorial or something that will tell me about these stuff.

Could you give me the most basic code of :
How to rotate the servo to 45 degrees from 0, then delay 1 second and rotate back to 0 degrees?
Please try and give me that simple code as then later on I will derive the code I need from that basic code....

Could you give me the most basic code of :
How to rotate the servo to 45 degrees from 0, then delay 1 second and rotate back to 0 degrees?

#include <Servo.h>
const int servoPin = 2;
Servo myServo;

void setup ()
{
  myServo.write (0);
  myServo.attach (servoPin);  
  delay (1000);  // give the servo time to reach 0 "degrees".
}

void loop ()
{
  myServo.write (45);  // send the servo to 45 "degrees"
  delay (1000);           // wait a second 
  myServo.write (0);   // send it back to zero
  for (;;);   // do nothing for the rest of time.
 

  
}

Thanks a lot AWOL I used that basic code and made my program and the LED turret works perfectly.
The part of the code I was looking for is "myservo.write(45)"
Thanks again

I have another small question??

Can we create an integer like this :

int i = (myServo.read);

and if this is wrong could you please give me the right code for that one step above.

I just need to create something that can read my servo's position so that when I press a key on the keyboard and increment of 20 degrees is added to the present servo location.

int i = (myServo.read);

No, you can't create an integer value from a function pointer like that.

Instead, you must call the function:int i = myServo.read();

It should be noted that the servo read method only returns the last value written, which your sketch probably already knows.

Is this code OK

//Include Servo Library
#include <Servo.h>

//Define Pins
int servoPin = 9;

//Create Servo Object
Servo VishalServo;

void setup()
{

//Attaches the Servo to our object
VishalServo.attach(servoPin);
Serial.begin(9600);
VishalServo.write (90);
delay(1000);
}

void loop()
{
while (Serial.available() == 0);
int val = Serial.read() - '0';
int i = VishalServo.read();

if (val == 7)
{
Serial.println("Turning left");
analogWrite(int i+20);
}
}

because when I run the program this error comes up:

servo_test.cpp: In function 'void loop()':
servo_test:32: error: expected primary-expression before 'int'

And could you tell me how to stop the servo from going back when it reaches 180 or 0 degrees.

If any of the above code is wrong could you give me the right code for my gun turret.
Thanks

analogWrite(int i+20);

What is the "int" doing there?
Come to that, what are you using an analogWrite (with too few arguments) for?

because when I run the program this error comes up

You haven't "run" the program, because it hasn't compiled.

Ya I have removed int but how do I tell the servo not to rotate back when it goes to 180 or 0 degrees.
Thanx

Well, you don't use analogWrite, that's a certainty.
How did you move it to 90 "degrees"?

VishalServo.write (90); with that

So I cannot use DigitalWrite and I cannot use Analog write then how do I tell it to increment 20 degrees when I press 7 on the keyboard

Because I had this
analogWrite(VishalServo, i+20)

I don't understand why you think that analogWrite is at all suitable.
It operates at ten times the frequency required by a servo.

Ok I changed it now can you skim through my modified program and tel me if it will work.
Thanks

//Include Servo Library
#include <Servo.h>

//Define Pins
int servoPin = 9;

//Create Servo Object
Servo VishalServo;

void setup()
{

//Attaches the Servo to our object
VishalServo.attach(servoPin);
Serial.begin(9600);
VishalServo.writeMicroseconds(1500);
delay(1000);
}

void loop()
{
while (Serial.available() == 0);
int val = Serial.read() - '0';
int i = VishalServo.read();

if (val == 7)
{
Serial.println("Turning left");
VishalServo.write(i+20);
}
}

It looks like it should, but why don't you try it?

VishalServo.writeMicroseconds(1500);

Why have you swapped methods here?
What you have written is exactly equivalent to VishalServo.write(90);