-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffx-lighting-dodger.ino
90 lines (69 loc) · 1.91 KB
/
ffx-lighting-dodger.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
86
87
88
89
90
#include <Arduino.h>
#include <Servo.h>
#include <LiquidCrystal.h>
#define photoPin A3
#define servoPin 10
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo servo;
int lightVal = 0, dodged = 0, maxLight = 0, minLight = 9999;
// this value is dependent on the photoresistor and the brightness of your TV
// you will probably need to test different values here
int threshold = 380;
// this sets the starting servo position and the distance traveled for a button press
// you may need to change these values depending on the servo motor you are using
int startDegs = 159;
int pressDegs = 15;
void updateLCD(){
String spaces1 = "";
for(int i = 0; i < 8 - String(dodged).length() - String(lightVal).length(); i++){
spaces1 = spaces1 + " ";
}
String spaces2 = "";
for(int i = 0; i < 4 - String(minLight).length(); i++){
spaces2 = spaces2 + " ";
}
lcd.clear();
lcd.home();
lcd.print("LD: ");
lcd.print(dodged);
lcd.print(spaces1 + " L: ");
lcd.print(lightVal);
lcd.setCursor(0, 1);
lcd.print("Lm: ");
lcd.print(minLight);
lcd.print(spaces2 + " LM: ");
lcd.print(maxLight);
}
void setup(){
Serial.begin(9600);
pinMode(photoPin, INPUT);
servo.attach(servoPin);
servo.write(startDegs);
lcd.begin(16, 2);
updateLCD();
}
void loop(){
lightVal = analogRead(photoPin);
// print light value in serial
Serial.println(lightVal);
if(lightVal > maxLight){
maxLight = lightVal;
updateLCD();
}
if(lightVal < minLight){
minLight = lightVal;
updateLCD();
}
if (lightVal > threshold){
minLight = 9999;
delay(50);
servo.write(startDegs - pressDegs);
delay(200);
servo.write(startDegs);
dodged++;
updateLCD();
delay(990); // "debouncing"
}
delay(10);
}