-
Notifications
You must be signed in to change notification settings - Fork 4
/
cam_servo.ino
116 lines (84 loc) · 3.52 KB
/
cam_servo.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <Servo.h>
// Title: Auto Pan-Tilt Servo/Cam Control
// Subject: This Sketch receives X,Y coordinates from srial then
// moves the camera to center of those coordinates.
// Remixed: TechBitar / Hazim Bitar
// Date: Dec 2011
// Credit: I based this sketch on zagGrad's (SparkFun) code.
#define servomaxx 180 // max degree servo horizontal (x) can turn
#define servomaxy 180 // max degree servo vertical (y) can turn
#define screenmaxx 320 // max screen horizontal (x)resolution
#define screenmaxy 240 // max screen vertical (y) resolution
#define servocenterx 90 // center po#define of x servo
#define servocentery 90 // center po#define of y servo
#define servopinx 9 // digital pin for servo x
#define servopiny 10 // digital servo for pin y
#define baudrate 57600 // com port speed. Must match your C++ setting
#define distancex 1 // x servo rotation steps
#define distancey 1 // y servo rotation steps
int valx = 0; // store x data from serial port
int valy = 0; // store y data from serial port
int posx = 0;
int posy = 0;
int incx = 10; // significant increments of horizontal (x) camera movement
int incy = 10; // significant increments of vertical (y) camera movement
Servo servox;
Servo servoy;
short MSB = 0; // to build 2 byte integer from serial in byte
short LSB = 0; // to build 2 byte integer from serial in byte
int MSBLSB = 0; //to build 2 byte integer from serial in byte
void setup() {
Serial.begin(baudrate); // connect to the serial port
Serial.println("Starting Cam-servo Face tracker");
pinMode(servopinx,OUTPUT); // declare the LED's pin as output
pinMode(servopiny,OUTPUT); // declare the LED's pin as output
servoy.attach(servopiny);
servox.attach(servopinx);
// center servos
servox.write(servocenterx);
delay(200);
servoy.write(servocentery);
delay(200);
}
void loop () {
while(Serial.available() <=0); // wait for incoming serial data
if (Serial.available() >= 4) // wait for 4 bytes.
{
// get X axis 2-byte integer from serial
MSB = Serial.read();
delay(5);
LSB = Serial.read();
MSBLSB=word(MSB, LSB);
valx = MSBLSB;
delay(5);
// get Y axis 2-byte integer from serial
MSB = Serial.read();
delay(5);
LSB = Serial.read();
MSBLSB=word(MSB, LSB);
valy = MSBLSB;
delay(5);
// read last servos positions
posx = servox.read();
posy = servoy.read();
//Find out if the X component of the face is to the left of the middle of the screen.
if(valx < (screenmaxx/2 - incx)){
if( posx >= incx ) posx += distancex; //Update the pan position variable to move the servo to the left.
}
//Find out if the X component of the face is to the right of the middle of the screen.
else if(valx > screenmaxx/2 + incx){
if(posx <= servomaxx-incx) posx -=distancex; //Update the pan position variable to move the servo to the right.
}
//Find out if the Y component of the face is below the middle of the screen.
if(valy < (screenmaxy/2 - incy)){
if(posy >= 5)posy += distancey; //If it is below the middle of the screen, update the tilt position variable to lower the tilt servo.
}
//Find out if the Y component of the face is above the middle of the screen.
else if(valy > (screenmaxy/2 + incy)){
if(posy <= 175)posy -= distancey; //Update the tilt position variable to raise the tilt servo.
}
// Servos will rotate accordingly
servox.write(posx);
servoy.write(posy);
}
}