-
Notifications
You must be signed in to change notification settings - Fork 14
/
pan_tilt_serial.ino
60 lines (56 loc) · 1.26 KB
/
pan_tilt_serial.ino
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
54
55
56
57
58
59
#include <Servo.h>
Servo servo1;
Servo servo2;
int movedirection;
int currentX = 90;
int currentY = 90;
int moved = 0;
void setup()
{
servo2.attach(7);
servo1.attach(8);
moveto(servo1,currentX);
moveto(servo2,currentY);
delay(3000);
Serial.begin(19200);
}
void loop()
{
if(Serial.available()>0){
movedirection = Serial.read();
//Serial.print(movedirection);
switch(movedirection){ /* clock notation but *10 to move faster */
case 49:
/*1 move up*/
currentY = currentY>1 ? currentY+5 : 0;
moveto(servo2,currentY);
break;
case 50:
/*2 move right*/
currentX = currentX<179 ? currentX+5 : 180;
moveto(servo1,currentX);
break;
case 51:
/*3 move down*/
currentY = currentY<179 ? currentY-5 : 180;
moveto(servo2,currentY);
break;
case 52:
/*4 move left */
currentX = currentX>1 ? currentX-5 : 0;
moveto(servo1,currentX);
break;
default:
//Serial.print("wth");
currentX = 90;
currentY = 90;
moveto(servo1,currentX);
moveto(servo2,currentY);
}
}
/*delay(400);*/
}
void moveto(Servo whichservo,int position){
whichservo.write(position);
/*delay(15);*/
}