-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial-listener.ino
51 lines (46 loc) · 1.17 KB
/
serial-listener.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
// Based on Sweep by BARRAGAN <http://barraganstudio.com>
#include <Servo.h>
Servo x_servo;
Servo y_servo;
int max_angle = 140;
int min_angle = 0;
int increment = 5;
int position;
byte byteRead;
char servo = 'n';
void setup() {
x_servo.attach(13);
y_servo.attach(12);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
byteRead = Serial.read();
if (byteRead > 47 && byteRead < 58) {
// only because I have no idea how to decode ascii...
position = (position * 10) + byteRead - 48;
} else {
if (byteRead == 120) {
// move on x-axis
Serial.print('x');
Serial.println(position);
if (position > max_angle || position < min_angle) {
Serial.println("I can't let you do that...");
} else {
x_servo.write(position);
}
position = 0;
} else if (byteRead == 121) {
// move on y-axis
Serial.print('y');
Serial.println(position);
if (position > max_angle || position < min_angle) {
Serial.println("I can't let you do that...");
} else {
y_servo.write(position);
}
position = 0;
}
}
}
}