-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.ino
More file actions
96 lines (70 loc) · 1.53 KB
/
Copy pathsource.ino
File metadata and controls
96 lines (70 loc) · 1.53 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
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
#define tinker 3
#ifdef tinker
#include <Servo.h>
#define LED 2
#define AUTOMAGIC 3
#define CLOCKWISE 4
#define ANTICLOCKWISE 5
#define SERVO 11
#define LIGHT1 A0
#define LIGHT2 A1
#else
#include <ESP32_Servo.h>
#define LED 27
#define AUTOMAGIC 33
#define CLOCKWISE 15
#define ANTICLOCKWISE 32
#define SERVO 14
#define LIGHT1 A0
#define LIGHT2 A1
#endif
Servo servo;
int servoDegrees = 0;
int oldDegrees = servoDegrees;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(AUTOMAGIC, INPUT);
pinMode(CLOCKWISE, INPUT);
pinMode(ANTICLOCKWISE, INPUT);
servo.attach(SERVO);
pinMode(LIGHT1, INPUT);
pinMode(LIGHT2, INPUT);
servo.write(servoDegrees);
}
bool automagic=false;
void loop() {
if(digitalRead(AUTOMAGIC) == HIGH ){
automagic=true;
}
if(digitalRead(CLOCKWISE) == HIGH ){
automagic=false;
servoDegrees += 1;
}
if(digitalRead(ANTICLOCKWISE) == HIGH ){
automagic=false;
servoDegrees -= 1;
}
if(automagic){
digitalWrite(LED, HIGH);
servoDegrees = convertedLightSensorValue();
} else {
digitalWrite(LED, LOW);
}
servoDegrees = 0 < servoDegrees ? servoDegrees : 0;
servoDegrees = 180 > servoDegrees ? servoDegrees : 180;
if (oldDegrees != servoDegrees) {
servo.write(servoDegrees);
oldDegrees = servoDegrees;
}
delay(10);
}
int lightSensorValue() {
int x = analogRead(LIGHT1);
int y = analogRead(LIGHT2);
return (x+y)/2;
}
int convertedLightSensorValue() {
float value = lightSensorValue();
return value / 1024.0 * 180.0;
}