-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ino
78 lines (70 loc) · 1.72 KB
/
code.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
#include <Servo.h>
// DEFINE SERVO
Servo servohori;
const int servohPin = 7;
const int servohLimitHigh = 160;
const int servohLimitLow = 60;
// SET PINs
const int ldrtoprPin = A1; // fotoresistenza destra
const int ldrbotrPin = A0; // fotoresistenza sinistra
const int VRX_PIN = A5; // joystick X axis
const int buttonPin = 8;
const int ledPin = 13;
// SET VARIABLES
int servoh = servohLimitLow;
int buttonState = 0;
int oldButtonState = 0;
int ledState = 0;
// SETUP
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
servohori.attach(servohPin);
servohori.write(servoh);
delay(500);
}
// LOOP
void loop() {
// READ
int left = analogRead(ldrtoprPin);
int right = analogRead(ldrbotrPin);
// Check button state
buttonState = digitalRead(buttonPin);
if (buttonState != oldButtonState && buttonState == 1) {
ledState = 1 - ledState;
digitalWrite(ledPin, ledState);
delay(50);
}
oldButtonState = buttonState;
if (ledState == 1) { // Automatic control
if (left < right) {
servoh++;
if (servoh > servohLimitHigh) {
servoh = servohLimitHigh;
}
} else if (right < left) {
servoh--;
if (servoh < servohLimitLow) {
servoh = servohLimitLow;
}
}
} else if (ledState == 0) { // Manual control
int vrxValue = analogRead(VRX_PIN);
if (506 < vrxValue && vrxValue < 519) {
// Do nothing
} else if (vrxValue < 505) {
servoh--;
if (servoh < servohLimitLow) {
servoh = servohLimitLow;
}
} else if (vrxValue > 520) {
servoh++;
if (servoh > servohLimitHigh) {
servoh = servohLimitHigh;
}
}
}
servohori.write(servoh);
delay(10);
}