-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.ino
107 lines (89 loc) · 3.27 KB
/
status.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <FastLED.h>
//define constants
//in our case, we start with 15 batteries to be controlled by 15 RGB LEDs
#define NUM_LEDS 15
#define NUM_SENSORS 15
#define DATA_PIN 10
#define DEFAULT_THRESH 10
#define LED_BRIGHTNESS 30
#define MAX_TICKS 500
#define LOOP_DELAY 30
CRGB leds[NUM_LEDS];
uint8_t light_sensor_pins[NUM_SENSORS] = {A1, A2, A3, A6, A5, A4, A7, A8, A9, A12, A11, A10, A13, A14, A15};
int thresh[NUM_SENSORS];
unsigned long startTime;
unsigned long stopTime;
int greenTicks[NUM_SENSORS] = {0};
int redTicks[NUM_SENSORS] = {0};
void setup() {
// Setup LEDs
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5,300);
FastLED.setBrightness(LED_BRIGHTNESS);
for(int i = 0; i<NUM_SENSORS; i++) {
// Setup light sensor
pinMode(light_sensor_pins[i], INPUT);
}
//Setting thresh values
for(int i = 0; i<NUM_SENSORS; i++) {
// Set light sensor threshold
thresh[i] = DEFAULT_THRESH;
}
for (int i=0; i<NUM_SENSORS; i++) {
redTicks[i] = {MAX_TICKS};
}
//Set modified thresh values here
//Setup console
Serial.begin(9600); //begin Serial Communication
}
// the loop function runs over and over again forever
//greenTicks = how many times the photoresistor counts a green light from the charger
//redTicks = how many times the photoresistor counts no light from the charger
void loop() {
for(int i=0; i<NUM_SENSORS; i++)
{
//set variable for light
int raw_light = analogRead(light_sensor_pins[i]); // read the raw value from light_sensor pin
int light = map(raw_light, 0, 1023, 0, 100); // map the value from 0, 1023 to 0, 100
//Serial.print(String(light_sensor_pins[i]));
//Serial.println("Sensor [" + String(i) + "]" + String(light));
//set counter [i] for number of times a light is counted
//if [i] is greater than or equal to 100, then the light value (green, pulsing, or none) is confirmed
//if the charger light is greater than the threshold, it means that the charger is currently displaying green - therefore, a green tick is counted
if (light>=thresh[i]){
if (redTicks[i]>0) {
redTicks[i] = 0;
}
greenTicks[i]++;
}
//if the charger light is less than the threshold, it means that the charger is not currently displaying any color - a red tick is counted
else {
if (greenTicks[i]>0) {
greenTicks[i] = 0;
}
redTicks[i]++;
}
//if the number of green ticks is larger than 100, it means the LED should turn green
if (greenTicks[i]>=MAX_TICKS) {
leds[i] = CRGB::Green;
greenTicks[i] = MAX_TICKS;
if (i==2) {
Serial.println("Sensor [" + String(i) + "] reset: Green");
}
}
//if the number of red ticks is larger than 100, it means the LED should turn red
else if (redTicks[i]>=MAX_TICKS) {
leds[i] = CRGB::Red;
redTicks[i] = MAX_TICKS;
if (i==2) {
Serial.println("Sensor [" + String(i) + "] reset: Red");
}
}
//if neither condition is satisfied, that means the charger light is pulsing (going back and forth between green and no color), so the LED should be orange
else {
leds[i] = CRGB(255, 96, 0); //Change this to add different state color
}
}
FastLED.show();
delay(LOOP_DELAY);
}