-
Notifications
You must be signed in to change notification settings - Fork 6
/
rgbled.ino
195 lines (175 loc) · 5.09 KB
/
rgbled.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
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
void setupPixels() {
hue[USERLED] = 0;
hue[REMOTELED] = 0;
saturation[USERLED] = 0;
saturation[REMOTELED] = 0;
value[USERLED] = 0;
value[REMOTELED] = 0;
FastLED.addLeds<WS2812, WS2812PIN, RGB>(leds, NUMPIXELS);
fill_solid(leds, NUMPIXELS, CRGB(0, 0, 0));
FastLED.show();
delay(1000);
blinkRGB();
}
void rgbLedHandler() {
unsigned long millisCheck = millis();
if (millisCheck - prevPixelMillis > PIXELUPDATETIME) {
if (isSelectingColour == true) {
cycleHue(USERLED);
}
prevPixelMillis = millisCheck;
if (ledChanged[USERLED]) {
ledChanged[USERLED] = false;
saturation[USERLED] = 255;
if (value[USERLED] == 0) {
//Turn off
leds[USERLED] = CHSV(0, 0, 0);
} else {
leds[USERLED] = CHSV(hue[USERLED], saturation[USERLED], value[USERLED]);
}
FastLED.show();
}
if (ledChanged[REMOTELED]) {
ledChanged[REMOTELED] = false;
saturation[REMOTELED] = 255;
if (value[REMOTELED] == 0) {
//turn off
leds[REMOTELED] = CHSV(0, 0, 0);
} else {
leds[REMOTELED] = CHSV(hue[REMOTELED], saturation[REMOTELED], value[REMOTELED]);
}
FastLED.show();
}
//long fade
longFadeHandler();
}
//updating every 5 seconds to make sure the leds dont lose their colours
if (millisCheck - prevlongPixelMillis > PIXELUPDATETIMELONG) {
prevlongPixelMillis = millisCheck;
FastLED.show();
}
//short fade on remote led receive or user led trigger
fadeRGBHandler();
}
void cycleHue(int led) {
value[led] = 255;
if (hue[led] < 255) {
hue[led]++;
} else {
hue[led] = 0;
}
ledChanged[led] = true;
}
uint16_t getUserHue() {
return hue[USERLED];
}
void blinkRGB() {
leds[0] = CHSV(1, 255, 255);
leds[1] = CHSV(127, 255, 255);
FastLED.show();
delay(100);
leds[0] = CHSV(1, 0, 0);
leds[1] = CHSV(127, 0, 0);
FastLED.show();
}
void fadeRGB(int led) {
if (readyToFadeRGB[led] == false) {
readyToFadeRGB[led] = true;
}
}
void fadeRGBHandler() {
for (byte i = 0; i < NUMPIXELS; i++) {
if (readyToFadeRGB[i] == true && isFadingRGB[i] == false) {
ledChanged[i] = true;
isFadingRGB[i] = true;
fadeTimeRGB[i] = millis();
value[i] = 255;
}
if (millis() - fadeTimeRGB[i] > RGBFADEMILLIS && isFadingRGB[i] == true) {
fadeTimeRGB[i] = millis();
if (value[i] > RGBLEDPWMSTART) {
value[i]--;
leds[i] = CHSV(hue[i], saturation[i], value[i]);
FastLED.show();
} else {
isFadingRGB[i] = false;
readyToFadeRGB[i] = false;
}
}
}
}
void startLongFade(byte LED) {
isLongFade[LED] = true;
longFadeMinutes[LED] = LONGFADEMINUTESMAX;
prevLongFadeVal[LED] = 0;
}
void longFadeHandler() {
for (byte i = 0; i < NUMPIXELS; i++) {
if (isLongFade[i]) {
if (millis() - prevLongFadeMillis[i] > LONGFADECHECKMILLIS) {
prevLongFadeMillis[i] = millis();
longFadeMinutes[i]--;
unsigned long currLongFadeVal = long((float)longFadeMinutes[i] / ((float)LONGFADEMINUTESMAX / (float)RGBLEDPWMSTART));
if (currLongFadeVal != prevLongFadeVal[i]) {
prevLongFadeVal[i] = currLongFadeVal;
if (currLongFadeVal > 0) {
currLongFadeVal = currLongFadeVal - 1;
}
value[i] = (byte)fscale(0, RGBLEDPWMSTART, 0, RGBLEDPWMSTART, currLongFadeVal, -3);
ledChanged[i] = true;
Serial.print("LED:");
Serial.println(i);
Serial.println(value[i]);
}
if (longFadeMinutes[i] <= 0 || value[i] == 0) {
isLongFade[i] = false;
}
}
}
}
}
int fscale(float originalMin, float originalMax, float newBegin, float newEnd, float inputValue, float curve) {
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
boolean invFlag = 0;
// condition curve parameter
// limit range
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
// Check for out of range inputValues
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
// Zero Refference the values
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin) {
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax ) {
return 0;
}
if (invFlag == 0) {
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
}
else // invert the ranges
{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}
return (int)rangedValue;
}