include Servo include server library Servo ser create servo object to

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<Servo.h> // include server library
Servo ser; // create servo object to control a servo
int poser = 10; // initial position of server
int val; // initial value of input
void setup() {
Serial.begin(9600); // Serial comm begin at 9600bps
ser.attach(4);//
}
void loop() {
if (Serial.available()) // if serial value is available
{
val = Serial.read();// then read the serial value
if (val == 'd') //if value input is equals to d
{
Serial.println("d pressed");
poser = 0; //than position of servo motor increases by 1 ( anti clockwise)
ser.write(poser);// the servo will move according to position
delay(15);//delay for the servo to get to the position
}
if (val == 'a') //if value input is equals to a
{
Serial.println("a pressed");
poser = 120; //than position of servo motor decreases by 1 (clockwise)
ser.write(poser);// the servo will move according to position
delay(15);//delay for the servo to get to the position
}
}
}