Hey Guys,
I'm currently working on building a very simple iteration of what was eventually going to be a much more elaborate lighting control system for my friends Van build. He's got about 7 strips of NeoPixel LED's run through the ceiling and under the cabinets. Long term he wants to do a remote based system that will essentially let him turn the van into an instant disco with dancing colors and such. For now, I'm just trying to get him up and running with a basic system that will let him turn the lights on and off.
The idea here is to be able to set the "White Light Temperature" ahead of time. Then using a map function, adjust the values of R, G, and B for brightness. When running these functions in the loop, they all behave as expected, however now that I'm trying to use a button attached to an ISR to trigger how many LED's are active, the lights slowly dim to a very low level on each button press.
If I set the brightness to 255, the program functions as expected, each time i push the button more lights come on, untill they're all on, then they turn off. However, if I change the brightness to anything below 255, they dim each time the button is pressed. If the value is close to 255 it happens very slowly, and if the the value is below 128, it happens quickly. After about 6-10 presses the lights will "bottom out" in brightness and remain there. I'm assuming they're at minimum illumination.
Any chance you guys could tell me what the heck is goin on here, and what I'm missing?? As you will see, I tried making just about every variable that the ISR touches VOLATILE thinking perhaps that was the issue, but it didn't change anything.
Thanks in advance!
#include <Adafruit_NeoPixel.h>
#include <math.h>
#define NEOPIN 4
#define NUM_LEDS 30
volatile int whiteTemp[3]; //global container for white temperature values
//Lights will have 4 states, 0, 1, 2, 3 These states will determine the number of led's on (1,2, or 3 respectively. 0 for off)
volatile int lightState = 0;
volatile int brightness = 128;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, NEOPIN, NEO_GRB + NEO_KHZ800); //initialize the strip object
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(3, INPUT);
// attachInterrupt(0, changeColor, RISING);
attachInterrupt(digitalPinToInterrupt(3), switchPower, RISING);
strip.begin();
strip.show(); //initialize all pixels to off
setWhiteTemp(6);
}
void loop() {
// Nothin runs here.
}
///////////////////////////////////////////////////////////////////
///////////////////------- ISRs SECTION-------/////////////////////
///////////////////////////////////////////////////////////////////
void switchPower(){
if(lightState < 3){
lightState++;
setWhiteLights(lightState, brightness);
}
else {
lightState = 0;
clearPixels();
strip.show();
}
}
///////////////////////////////////////////////////////////////////
//////////////------- BASIC MANAGEMENT SECION-------///////////////
///////////////////////////////////////////////////////////////////
void clearPixels(){
for(int i = 0; i < NUM_LEDS; i++){
strip.setPixelColor(i, 0, 0, 0); //sets each pixel to 0
}
}
///////////////////////////////////////////////////////////////////
///////////------- WHITE LIGHT MANAGEMENT SECION-------////////////
///////////////////////////////////////////////////////////////////
/*Set White Lights.
*Args: Lights(1 = 1/3, 2 = 1/2, 3 = All), Brightness (0-255)
*/
void setWhiteLights(volatile int lights, volatile int br){
mapBrightness(br);
switch(lights){
case 1: //sets the first and every third pixel on to brightness
clearPixels();
for(int i = 0; i < NUM_LEDS; i += 3){
strip.setPixelColor(i, whiteTemp[0], whiteTemp[1], whiteTemp[2]);
}
strip.show();
break;
case 2: //sets the first and every third pixel plus the following pixel on to brightness, unless that pixel is beyond the scope of pixels
clearPixels();
for(int i = 0; i < NUM_LEDS; i += 3){
strip.setPixelColor(i, whiteTemp[0], whiteTemp[1], whiteTemp[2]);
if(i+1 != NUM_LEDS) {
strip.setPixelColor(i+1, whiteTemp[0], whiteTemp[1], whiteTemp[2]);
}
}
strip.show();
break;
case 3: //sets all lights on to brightness
clearPixels();
for(int i = 0; i < NUM_LEDS; i++){
strip.setPixelColor(i, whiteTemp[0], whiteTemp[1], whiteTemp[2]);
}
strip.show();
break;
default:
break;
}
}
void mapBrightness(volatile int br){
whiteTemp[0] = map(whiteTemp[0], 0, 255, 1, br);
whiteTemp[1] = map(whiteTemp[1], 0, 255, 1, br);
whiteTemp[2] = map(whiteTemp[2], 0, 255, 1, br);
}
/*Set White Temperature, 0-8:
**0---Candle, 1900
**1---40W Tungsten, 2600
**2---100W Tungsten, 2850
**3---Halogen, 3200
**4---Carbon Arc, 5200
**5---High Noon Sun, 5400
**6---Direct Sunlight, 6000
**7---Overcast Sky, 7000
**8---Clear Blue Sky, 20000
**/
void setWhiteTemp(int temp){
switch(temp){
case 0: //Candle, 1900k
whiteTemp[0]=255;
whiteTemp[1]=147;
whiteTemp[2]=41;
break;
case 1: //40W Tungsten, 2600k
whiteTemp[0]=255;
whiteTemp[1]=197;
whiteTemp[2]=143;
break;
case 2: //100W Tungsten, 2850k
whiteTemp[0]=255;
whiteTemp[1]=214;
whiteTemp[2]=170;
break;
case 3: //Halogen, 3200k
whiteTemp[0]=255;
whiteTemp[1]=241;
whiteTemp[2]=224;
break;
case 4: //Carbon Arc, 5200k
whiteTemp[0]=255;
whiteTemp[1]=250;
whiteTemp[2]=244;
break;
case 5: //High Noon Sun, 5400k
whiteTemp[0]=255;
whiteTemp[1]=255;
whiteTemp[2]=251;
break;
case 6: //Direct Sunlight, 6000k
whiteTemp[0]=255;
whiteTemp[1]=255;
whiteTemp[2]=255;
break;
case 7: //Overcast Sky, 7000k
whiteTemp[0]=201;
whiteTemp[1]=226;
whiteTemp[2]=255;
break;
case 8: //Clear Blue Sky, 20000k
whiteTemp[0]=64;
whiteTemp[1]=156;
whiteTemp[2]=255;
break;
default:
whiteTemp[0]=255;
whiteTemp[1]=255;
whiteTemp[2]=255;
break;
}
}