-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
228 lines (194 loc) · 5.46 KB
/
main.cpp
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
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <EEPROM.h>
//PINOUT definitions
#define CHANGE_BUTTON_UP PB2
#define CHANGE_BUTTON_PIN_DOWN PB4
#define LED_PIN PB1
#define VOTLAGE_READ A3
//Constants
#define RAINBOW_SPEED 128 // 65 536 / 128 = 512 steps. 62steps per second = 512/62 = 8seconds (rounded) lasting one loop of animation
#define NUMPIXELS 1
#define HOLD_TIME 1000 //in ms
#define HOLD_TIME_DELAY 20 //in ms
#define BRIGHTNESS_STEPS 32 //PS: 256 / 32 = 8 brightness steps
#define BLINKING_SPEED 30
#define EEPROM_UPDATE_SPEED 480 //want to save every 30s if something has changed
#define EEPROM_EFFECT_ADDRESS 0
#define EEPROM_BRIGHTNESS_ADDRESS 1
//variables and instances
Adafruit_NeoPixel pixels(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
uint8_t butLasState = 1, butLasState2 = 1, effectIndex = 0, brightLevel = 255;
uint8_t isInHoldMode = 0, helpVarTimer = 0, helpVarDelay = 0;
uint16_t rainbowHue = 0, helpVarEEPROMDelay = 0;
long startTime = 0;
void timer_setup()
{
TCNT1 = 0; //set timer1 to 0
TCCR1 = 0b00001001; //setting up prescaler for timer1 to 8192x = every 1.024ms value will increse after 316.2ms timer will overflow
TIMSK |= (1 << TOIE1); //set up overflow interrupt for timer1
sei(); //Enable interrupts
}
void read_from_eeprom()
{
effectIndex = EEPROM.read(EEPROM_EFFECT_ADDRESS);
brightLevel = EEPROM.read(EEPROM_BRIGHTNESS_ADDRESS);
}
ISR (TIMER1_OVF_vect)
{
rainbowHue += RAINBOW_SPEED;
if (helpVarDelay > BLINKING_SPEED)
{
if (isInHoldMode == 1 && helpVarTimer == 0)
{
pixels.setBrightness(1);
helpVarTimer = 1;
}
else
{
pixels.setBrightness(brightLevel);
helpVarTimer = 0;
}
helpVarDelay = 0;
}
else
helpVarDelay++;
if (helpVarEEPROMDelay > EEPROM_UPDATE_SPEED) //Save to EEPROM
{
EEPROM.update(EEPROM_EFFECT_ADDRESS, effectIndex);
EEPROM.update(EEPROM_BRIGHTNESS_ADDRESS, brightLevel);
helpVarEEPROMDelay = 0;
}
else
helpVarEEPROMDelay++;
}
void brightness_set(uint8_t value)
{
if (value == 1)
{
if (brightLevel > (UINT8_MAX - BRIGHTNESS_STEPS)) //check for uint overflow
brightLevel = 255; //set max brightness if overflow would happened
else
brightLevel += BRIGHTNESS_STEPS;
}
else
{
if (brightLevel < BRIGHTNESS_STEPS + 1)
brightLevel = BRIGHTNESS_STEPS;
else
brightLevel -= BRIGHTNESS_STEPS;
}
pixels.setBrightness(brightLevel);
}
void button_state_check1()
{
//get current button state, LOW(0) = Pressed, HIGH(1) = not pressed
uint8_t butCurState = digitalRead(CHANGE_BUTTON_PIN_DOWN);
if (butCurState == 1 && butLasState == 1)
return;
else if (butCurState == 0 && butLasState == 1)
startTime = millis();
else if (butCurState == 0 && butLasState == 0)
{
if ((millis() - startTime) > HOLD_TIME && (millis() - startTime) < HOLD_TIME + HOLD_TIME_DELAY)
isInHoldMode = 1; //isInHoldMode = !isInHoldMode; //toggle value
}
else if (butCurState == 1 && butLasState == 0)
{
if (isInHoldMode == 1)
brightness_set(1);
else
effectIndex++;
}
butLasState = butCurState;
}
void button_state_check2()
{
uint8_t butCurState = digitalRead(CHANGE_BUTTON_UP);
if (butCurState == 1 && butLasState2 == 1)
return;
else if (butCurState == 0 && butLasState2 == 1)
startTime = millis();
else if (butCurState == 0 && butLasState2 == 0)
{
if ((millis() - startTime) > HOLD_TIME && (millis() - startTime) < HOLD_TIME + HOLD_TIME_DELAY)
isInHoldMode = 0; //isInHoldMode = !isInHoldMode; //toggle value
}
else if (butCurState == 1 && butLasState2 == 0)
{
if (isInHoldMode == 1)
brightness_set(0);
else
effectIndex--;
}
butLasState2 = butCurState;
}
void select_effect()
{
if (isInHoldMode == 1)
return;
switch (effectIndex)
{
case 0:
pixels.setPixelColor(0, pixels.Color(255, 255, 255)); //white
break;
case 1:
pixels.setPixelColor(0, pixels.Color(255, 244, 150)); //warm white
break;
//Start of color spectrum from red to blue (simple rainbow spectrum)
case 2:
pixels.setPixelColor(0, pixels.Color(255,0,0)); //red
break;
case 3:
pixels.setPixelColor(0, pixels.Color(255,127,0)); //orange
break;
case 4:
pixels.setPixelColor(0, pixels.Color(255,255,0)); //yellow
break;
case 5:
pixels.setPixelColor(0, pixels.Color(128,255,0)); //light green
break;
case 6:
pixels.setPixelColor(0, pixels.Color(0,255,0)); //green
break;
case 7:
pixels.setPixelColor(0, pixels.Color(0,255,128)); //green-blue
break;
case 8:
pixels.setPixelColor(0, pixels.Color(0,255,255)); //turquoise
break;
case 9:
pixels.setPixelColor(0, pixels.Color(0,128,255)); //light blue
break;
case 10:
pixels.setPixelColor(0, pixels.Color(0,0,255)); //blue
break;
case 11:
pixels.setPixelColor(0, pixels.Color(128,0,255)); //dark-purple
break;
case 12:
pixels.setPixelColor(0, pixels.Color(255,0,255)); //purple/slightly ping?
break;
case 13:
pixels.setPixelColor(0, pixels.ColorHSV(rainbowHue,255,255));
break;
default:
effectIndex = 0;
break;
}
}
void setup()
{
read_from_eeprom();
timer_setup();
pinMode(CHANGE_BUTTON_UP, INPUT_PULLUP);
pinMode(CHANGE_BUTTON_PIN_DOWN, INPUT_PULLUP);
pixels.begin();
}
void loop()
{
button_state_check1();
button_state_check2();
select_effect();
pixels.show();
}