Trouble iterating variable without sketch crashing

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.

Whatever picture you are trying to show is not there.

All I can suggest is doing Serial prints.

Don't regurgitate the same thing. Your post from July 5, 2017 is still there.

ieee488:
Whatever picture you are trying to show is not there.

All I can suggest is doing Serial prints.

Don't regurgitate the same thing. Your post from July 5, 2017 is still there.
http://forum.arduino.cc/index.php?topic=487488.msg3327935#msg3327935

I've attached the image since imbedding from Imgur didn't seem to be working.

I wasn't trying to regurgitate from my previous post, that Sketch was trying to do random blink at a random interval. I've put that aside and started working on this and ran into a crash, but under what seem like very different circumstances. I wasn't trying to ask the same question again, this felt very different to me.


First off, 68 Ω is to low.

.

See:

Left to right: Out, GND, Vss

larryd:
First off, 68 Ω is to low.

I did the math on this one, maybe I've just done it incorrectly? My LEDs have a forward voltage of 2V and a peak forward current of 50mA. The supply from the Arduino is 5V.

Using R=V/I that gets me R= 3v/.05A which comes out to 60. Are the 8 Ohms per leg going to make that big of a difference or have I missed something?

larryd:
See:
https://www.vishay.com/docs/82491/tsop382.pdf

Left to right: Out, GND, Vss

I'm not having any issues getting input from the IR Sensor, all other buttons work, and the signal from the Blue+ button is making it in, I see part of the receive start to print before the crash sometimes. Sorry it looks like I mocked it up backwards in Fritzing. I have corrected this and reattached.

Well you removed the image so things are messed up now.

When driving a LED with an Arduino pin, limit the current to 20mA.

3v/20mA= 150 Ω

.

larryd:
Well you removed the image so things are messed up now.

I had to re-upload to correct my mistake, your link just references the previous file.

larryd:
When driving a LED with an Arduino pin, limit the current to 20mA.

3v/20mA= 120 Ω

Ah, that's what I had missed then. Thank you for that info. :slight_smile: I'll go swap those out and see if that helps the problem.

Sorry, 150 Ω.

larryd:
Sorry, 150 Ω.

Swapped out the 68 Ohm for 150 Ohm and same results. Thanks for the correction on the math though, I'll make sure to add that to my notes so I don't make the same mistake in the future. :slight_smile:

The problem I'm facing is that any press of the Blue+ button causes my sketch to crash and become unresponsive.

I just tried things here, no problem seen, using a NEC remote with NEC codes swapped into the sketch.

Correction, Blue + also crashes here.

.

Let's see your wiring.

.

instead of:
cB
Use:
cBlue
.

Forget this idea :frowning:

BTW
void setColor(int red, int green, int blue) //Light up RGB LE
Change to:
void setColor(byte red, byte green, byte blue) //Light up RGB LE

Looks like others have had this same problem with IRremote library and pin 11 when using analogWrite. IRremote will cause a conflict. IRremote and analogWrite() wind up trying to use the same timer.

Give other PWM pins a try, maybe use 2 for the ir receiver and 5, 6, and 9 for the rgb led.

//---LED PINS---
const byte redPin   = 5;   //PWN pin used for Red LED
const byte greenPin = 6;   //PWM pin used for Green LED
const byte bluePin  = 9;   //PWM pin used for Blue LED

.

larryd:
Correction, Blue + also crashes here.

Ok, so we've confirmed I'm not crazy or hitting a local hardware problem! :slight_smile: Thanks for testing.

larryd:
Let's see your wiring.

At work now, but I can get a picture posted later tonight.

larryd:
BTW
void setColor(int red, int green, int blue) //Light up RGB LE
Change to:
void setColor(byte red, byte green, byte blue) //Light up RGB LE

Looks like others have had this same problem with IRremote library and pin 11 when using analogWrite. IRremote will cause a conflict. IRremote and analogWrite() wind up trying to use the same timer.

Give other PWM pins a try, maybe use 2 for the ir receiver and 5, 6, and 9 for the rgb led.

//---LED PINS---

const byte redPin   = 5;   //PWN pin used for Red LED
const byte greenPin = 6;   //PWM pin used for Green LED
const byte bluePin  = 9;   //PWM pin used for Blue LED



.

Good catch on my setColor variables, never went back and moved those to bytes when I changed the variables around.

And very interesting on the pin11 issue. I'm curious how you found that out, what kind of keywords did you search, or was that just one of those things you recall seeing in the past? I'll move the pins around tonight when I get home and try again. :slight_smile:

Lastly, I see you changed the pin variables to const. I know this locks in the value, but what is the purpose of that here? Is it just best practice, or does it offer some kind of overflow protection?

No, you are not crazy.

const makes it so no SRAM is used, therefore saving scarce memory.
Just a simple substitution, but does check the 'type'

Somewhat similar to #define.

Google >>>-----> iRremote analogWrite

.

//---LED PINS---
const byte redPin = 5; //PWN pin used for Red LED
const byte greenPin = 6; //PWM pin used for Green LED
const byte bluePin = 9; //PWM pin used for Blue LED

Solves the problem here.

.

larryd:
//---LED PINS---
const byte redPin = 5; //PWN pin used for Red LED
const byte greenPin = 6; //PWM pin used for Green LED
const byte bluePin = 9; //PWM pin used for Blue LED

Solves the problem here.

Hot damn it worked! This also fixed my previous random blink sketch. Thanks for the tip larryd, I was beating my head against the wall on that one. :S

I've now got the strobe, granular RGB, and preset patterns in the main controller. Now I'm working on storing the last cR,cG,cB values so that the last non strobe color is saved and is returned to after ending strobe.

Thanks again for the help and for answering my questions. :slight_smile: