-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSBLibrary.cpp
More file actions
317 lines (272 loc) · 8.22 KB
/
LSBLibrary.cpp
File metadata and controls
317 lines (272 loc) · 8.22 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <Arduino.h>
#include "LSBLibrary.h"
//////////////////////////////////////////////////////////////////
// //
// PUBLIC, ACCESSIBLE //
// //
//////////////////////////////////////////////////////////////////
Board::Board(int covered, int dark, uint8_t pin) {
sensorPin = pin;
coveredVal = covered;
darkVal = dark;
}
/**
* The main function used to check for changes in light value. Will check if the light level is below two thresholds,
* the threshold for tuning on and off the board just while the light level is too high, or the threshold for turning
* off the board regardless of light level.
* @param code function pointer
*/
void Board::checkOnOff(void (*code)()) {
pinsOut();
delay(50);
second = 1;
sensorVal = analogRead(sensorPin);
if (sensorVal > darkVal) {turnOffAll();}
else {
if (isOn == true) {
if (sensorVal <= coveredVal) {turnOffAll(); isOn = false; blinkThing(); blinkThing(); delay(50);}
else {code();}
sensorVal = analogRead(sensorPin);
} else if (isOn == false) {
if (sensorVal <= coveredVal) {blinkThing(); blinkThing(); delay(50); code(); isOn = true;}
else {turnOffAll();}
sensorVal = analogRead(sensorPin);
delay(150);
}
}
}
/**
* Delay function that checks if the photoresistor (pin A2) is reading a value below the threshold for turning on/off
* the board. If it is, it will turn on/off the board depending on the current state of the board.
*
* @param duration int
*/
void Board::DPEADelay(float duration) {
duration = duration * second;
sensorVal = analogRead(sensorPin);
checkLight();
duration *= second;
for (int i = 0; i < duration; i++) {
sensorVal = analogRead(sensorPin);
checkLight();
duration *= second;
Serial.println(duration);
delay(1);
}
}
void Board::DPEADelayMicroseconds(float duration) {
duration *= second;
sensorVal = analogRead(sensorPin);
checkLight();
duration *= second;
for (int i = 0; i < duration / 1000; i++) {
// long oldTime = millis();
sensorVal = analogRead(sensorPin);
checkLight();
duration *= second;
// Serial.println(millis() - oldTime);
delayMicroseconds(1000);
}
}
/////////////////////////Setup functions//////////////////////////
void Board::setupAll() {
pinsOut();
Serial.begin(9600);
Serial.println("Listening on 9600");
pinMode(sensorPin, INPUT);
}
/////////////////Useful led blink/fade functions/////////////////
//Blinks
void Board::blinkThing() {
for (int x = 2; x <= 15; x++) {digitalWrite(x, HIGH);}
delay(100);
for (int x = 2; x <= 15; x++) {digitalWrite(x, LOW);}
delay(100);
}
/**
* Blinks the provided pin for the provided duration.
*
* @param pin int
* @param duration uint8_t
*/
void Board::ledBlink(uint8_t pin, unsigned int duration) {
digitalWrite(pin, HIGH);
DPEADelay(duration);
digitalWrite(pin, LOW);
}
/**
* Blinks all leds for the provided duration.
*
* @param duration int
*/
void Board::ledBlinkAll(unsigned int duration) {
turnOnAll();
DPEADelay(duration);
turnOffAll();
}
//Fades
/**
* Fades up the given pin for the given duration
*
* @param pin uint8_t
* @param duration int
*/
void Board::fadeUp(uint8_t pin, unsigned int duration) {
if (duration >= 1000) duration = 999;
for (float fractionOn = 0; fractionOn < 1; fractionOn += fadingStepSize) {
pwm(pin, gammaCorrect(fractionOn) * 10000);
updateFadingStepSize(duration);
}
// digitalWrite(pin, HIGH); // make sure pin is at full power before leaving fadeUp()
}
/**
* Fades down the given pin for the given duration
*
* @param pin uint8_t
* @param duration int
*/
void Board::fadeDown(uint8_t pin, unsigned int duration) {
if (duration >= 1000) duration = 999;
for (float fractionOn = 1; fractionOn > 0; fractionOn -= fadingStepSize) {
pwm(pin, gammaCorrect(fractionOn) * 10000);
updateFadingStepSize(duration);
}
}
/**
* Fades up all LEDs for the given duration
*
* @param duration int
*/
void Board::fadeUpAll(unsigned int duration) {
if (duration >= 1000) duration = 999;
for (float fractionOn = 0; fractionOn < 1; fractionOn += fadingStepSize) {
allPWM(gammaCorrect(fractionOn) * 10000);
updateFadingStepSize(duration);
}
}
/**
* Fades down all LEDs for the given duration
*
* @param duration int
*/
void Board::fadeDownAll(unsigned int duration) {
if (duration >= 1000) duration = 999;
for (float fractionOn = 1; fractionOn > 0; fractionOn -= fadingStepSize) {
allPWM(gammaCorrect(fractionOn) * 10000);
updateFadingStepSize(duration);
}
}
////////////////Array-based blink/fade functions////////////////
/**
* Blinks all LEDs in the given array for the given duration.
*
* @param pins uint8_t[]
* @param duration int
* @param arrsize size_t
*/
void Board::arrayLedBlink(uint8_t pins[], unsigned int duration, size_t arrsize) {
arrayOn(pins, arrsize);
DPEADelay(duration);
arrayOff(pins, arrsize);
}
/**
* Fades up all LEDs in the given array for the given duration.
*
* @param pins uint8_t[]
* @param duration int
* @param arrsize size_t
*/
void Board::fadeUpArray(uint8_t pins[], unsigned int duration, size_t arrsize) {
for (float fractionOn = 0; fractionOn < 1; fractionOn += fadingStepSize) {
arrPWM(pins, gammaCorrect(fractionOn) * 10000, arrsize);
updateFadingStepSize(duration);
}
}
/**
* Fades down all LEDs in the given array for the given duration.
*
* @param pins uint8_t[]
* @param duration int
* @param arrsize size_t
*/
void Board::fadeDownArray(uint8_t pins[], unsigned int duration, size_t arrsize) {
for (float fractionOn = 1; fractionOn > 0; fractionOn -= fadingStepSize) {
arrPWM(pins, gammaCorrect(fractionOn) * 10000, arrsize);
updateFadingStepSize(duration);
}
}
//////////////////////////QOL functions//////////////////////////
void Board::turnOnAll() {
for (int x = 2; x <= 13; x++) {
digitalWrite(x, HIGH);
}
}
void Board::turnOffAll() {
for (int x = 2; x <= 13; x++) {
digitalWrite(x, LOW);
}
}
void Board::arrayOn(uint8_t arr[], size_t arrsize) {
for (int x = 0; x <= arrsize; x++) {
digitalWrite(arr[x], HIGH);
}
}
void Board::arrayOff(uint8_t arr[], size_t size) {
for (int x = 0; x <= size; x++) {
digitalWrite(arr[x], LOW);
}
}
void Board::printSensorValue() {
Serial.println(sensorValue());
}
int Board::sensorValue() {
int a = analogRead(A2);
return a;
}
//////////////////////////////////////////////////////////////////
// //
// PRIVATE, INACCESSIBLE //
// //
//////////////////////////////////////////////////////////////////
void Board::pwm(uint8_t pin, float time) {
sensorVal = analogRead(sensorPin);
checkLight();
digitalWrite(pin, HIGH);
DPEADelayMicroseconds(time);
digitalWrite(pin, LOW);
DPEADelayMicroseconds(10000 - time);
}
void Board::allPWM(int time) {
sensorVal = analogRead(sensorPin);
checkLight();
turnOnAll();
DPEADelayMicroseconds(time);
turnOffAll();
DPEADelayMicroseconds(10000 - time);;
}
void Board::arrPWM(uint8_t arr[], int time, size_t arrsize) {
sensorVal = analogRead(sensorPin);
checkLight();
arrayOn(arr, arrsize);
DPEADelayMicroseconds(time);
arrayOff(arr, arrsize);
DPEADelayMicroseconds(10000 - time);
}
void Board::pinsOut() {
for (int x = 2; x <= 15; x++) {
pinMode(x, OUTPUT);
}
}
void Board::pinsIn() {
for (int x = 2; x <= 15; x++) {
pinMode(x, INPUT);
}
}
void Board::updateFadingStepSize(unsigned int duration) {
float knobRatio = 750 / 768;
float stepsOfDuration = duration * (1 - knobRatio) / 10;
fadingStepSize = 1 / stepsOfDuration;
}
void Board::checkLight() {
if (sensorVal <= coveredVal || sensorVal >= darkVal) {turnOffAll(); second = 0; pinsIn(); return;}
}