I have an LED controller that I am working on. Currently I am running into crashes whenever I try to iterate one of my variables. I'm hoping someone here has some advice I've not tried yet to work through this.
The LED controller has a couple basic commands right now. On, Off, Red, Green, Blue, Red+, Green+, and Blue+. On/Off turn the LED on or off. Red, Green, and Blue set the color to their respect color. Red+, Green+, and Blue+ add 10 to the their respective colors current value.
The problem I'm facing is that any press of the Blue+ button causes my sketch to crash and become unresponsive. It doesn't matter if I press Blue + first, second, third, etc, it always results in a crash. The only exception to this is if On is not pressed first. I included more detail on this in my troubleshooting description below.
A breadboard view is attached, and here are the part # for my components.
Arduino UNO
IR Reciever: TSOP38238
LED: RL5-RGB-DCC-2
Resistor: 68 Ohm'
Here is my code:
//---Libraries---
#include <IRremote.h> //Library included to allow IR input and decoding
//---IR Reciver & Decoder---
IRrecv myReceiver(2); //Create IR Receiver 'myReceiver' on pin 2
decode_results results;
//---LED PINS---
byte redPin = 9; //PWN pin used for Red LED
byte greenPin = 10; //PWM pin used for Green LED
byte bluePin = 11; //PWM pin used for Blue LED
//---Variables---
//byte cMin = 0; //Min value for RGB brightness (0 by default)
//byte cMax = 255; //Max value for RGB brightness (255 by default)
byte cR = 0; //Brightness value for color Red (Between 0-255 by default)
byte cG = 0; //Brightness value for color Green (Between 0-255 by default)
byte cB = 0; //Brightness value for color Blue (Between 0-255 by default)
//unsigned long prevMillis = 0; //Used to store the last time the LED was updated
byte pwr = 0;
void setup()
{
Serial.begin(9600); //starts serial communication
myReceiver.enableIRIn(); // Starts the receiver
pinMode(redPin, OUTPUT); // Setup output pin for Red LED with value 'redPin' above (9 by default)
pinMode(greenPin, OUTPUT); //Setup output pin for Green LED with value 'greenPin' above (10 by default)
pinMode(bluePin, OUTPUT); //Setup output pin for Blue LED with value 'bluepin' above (11 by default)
}
void loop()
{
if (myReceiver.decode(&results))
{
Serial.println(results.value);
//Serial.println("Signal Detected");
switch(results.value)
{
case 16203967: //Off
Serial.println("Off");
pwr = 0;
break;
case 16236607: //On
Serial.println("On");
pwr = 1;
break;
case 16195807: //Red +
//Serial.println("Red +");
cR = cR + 10;
break;
case 16228447: //Green +
//Serial.println("Green +");
cG = cG + 10;
break;
case 16212127: //Blue +
//Serial.println("Blue +");
cB = cB + 10;
break;
case 16244767: //White
//Serial.println("White");
break;
case 16191727: //Red
//Serial.println("Red");
cR = 255;
cG = 0;
cB = 0;
break;
case 16224367: //Green
//Serial.println("Green");
cR = 0;
cG = 255;
cB = 0;
break;
case 16208047 : //Blue
//Serial.println("Blue");
cR = 0;
cG = 0;
cB = 255;
break;
default: break;
} //end of decoder switch
myReceiver.resume(); //Restart the IR receiver 'myReciever'
Serial.println("Receiver rebooted");
} //end of decoder IF
switch(pwr)
{
case 0:
setColor(0,0,0);
break;
case 1:
setColor(cR,cG,cB);
break;
default: break;
}
}//end of loop
void setColor(int red, int green, int blue) //Light up RGB LED
{
analogWrite(redPin, red); //Send analog PWM pulse to pin 'redpin'(9 by default) with brightness 'red' as sent in by 'cR' from above
analogWrite(greenPin, green); //Send analog PWM pulse to pin 'redpin'(10 by default) with brightness 'green' as sent in by 'cG' from above
analogWrite(bluePin, blue); //Send analog PWM pulse to pin 'redpin'(11 by default) with brightness 'blue' as sent in by 'cB' from above
}
Troubleshooting steps taken:
-Switched from IRLib2 library to IRremote library for IR remote input and decoding
-Tried other LEDs
-Tried another IR receiver
-Swapped blue and green LED output pins
-Tore down and re-built the circuit
-Double checked for any kind of short
-Tried iterating cR, cG, and cB variable by other amounts (1,2,5, etc)
-Tried using the cG input to iterate byte cB
-Setup Serial prints of all variables at the end of the loop to check for odd values, found that serial output stopped before printing variables on the crashing loop, looked normal previous run.
-Defined byte cB earlier in the sketch to see if the creation/load order mattered
-Tried sending Blue+ command before sending On command. Found that this caused cB to iterate and the program to continue to loop, Afterwards the decoder Switch was never entered again so variables could not be changed.
I'm at a loss at what else to try. The closest thing I have to a hint at this point is what my last troubleshooting step revealed. Does anyone have any suggestions? Please let me know if more information is needed, I tried to include everything I could think of.
