-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracking-arduino.cc
More file actions
66 lines (57 loc) · 1.58 KB
/
tracking-arduino.cc
File metadata and controls
66 lines (57 loc) · 1.58 KB
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
/*
* The Arduino program used in the YouTube video http://www.youtube.com/watch?v=Gg6zqjDq1ho
*
* This program allow to rotate a webcam to track an object. This is
* done by using Arduino to control a servo motor. The communication
* is done using a serial connection (through USB or native).
*
* See the documentation to get the OpenCV program.
*
* Author: Frédéric Jolliton <[email protected]>
* Date: january 22, 2011
* Documentation: http://doc.tuxee.net/tracking
*/
#include "WProgram.h"
#include "Servo/Servo.h"
Servo servo;
static int target = 1500;
static int value = 0;
void setup()
{
Serial.begin(9600);
servo.attach(7);
pinMode(13, OUTPUT);
}
void loop()
{
//-------- Receive position from the PC (OpenCV) --------
// Note that we don't necessary read a whole line at once. The value
// is accumulated until it is read entirely.
while (Serial.available()) {
int c = Serial.read();
if (c >= '0' && c <= '9') {
value = 10 * value + (c - '0');
} else if (c == '\n') {
target = value;
value = 0;
} else if (c == '\r') {
// skip
} else {
// reset on any unexpected character
value = 0;
}
}
//-------- Clamp the target value --------
// Enable (briefly) on-board LED if the target was out of bound.
if (target <= 500) {
target = 500;
digitalWrite(13, HIGH);
} else if (target >= 2400) {
target = 2400;
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
//-------- Update servo position --------
servo.writeMicroseconds(target);
}