-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservo_leds.ino
85 lines (67 loc) · 1.57 KB
/
servo_leds.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
#include <Servo.h>
Servo myservo;
const int servo_pin = 6; // digital pin
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
const int left_led = 12;
const int right_led = 11;
const int up_led = 10;
const int down_led = 9;
const int THRESHOLD = 200;
const int MAX_VALUE = 1023;
void on(int pin) {
digitalWrite(pin, HIGH);
}
void off(int pin) {
digitalWrite(pin, LOW);
}
void switch_state(int pin, int value_read) {
if (value_read < THRESHOLD) {
on(pin);
} else {
off(pin);
}
}
void blink_all() {
on(left_led);
on(right_led);
on(up_led);
on(down_led);
delay(1000);
off(left_led);
off(right_led);
off(up_led);
off(down_led);
}
void setup() {
myservo.attach(servo_pin);
pinMode(left_led, OUTPUT);
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH); // ??
Serial.begin(115200);
}
void loop() {
int SW_read = digitalRead(SW_pin);
int X_read = analogRead(X_pin);
int Y_read = analogRead(Y_pin);
// Serial.print(SW_read);
// Serial.print("\n");
// Serial.print(X_read);
// Serial.print("\n");
// Serial.print(Y_read);
// Serial.print("\n");
if (SW_read == 0) {
blink_all();
} else {
switch_state(left_led, X_read);
switch_state(right_led, MAX_VALUE - X_read);
switch_state(up_led, Y_read);
switch_state(down_led, MAX_VALUE - Y_read);
}
float pos = 180 * (float(Y_read) / float(MAX_VALUE));
Serial.print(pos);
Serial.print("\n");
myservo.write(int(pos));
delay(10);
}