Global Variable becomes zero after some time

I am using IRLib2 and PWM for a mood light project where I have a "volatile unsigned long" variable that is used to keep the value of the last pressed key. This value is used to resolves cases in a switch case which calls the relevant function.

At first I found out that IRLib is conflicting with a few Pins. The pins where I found the PWM output correct were 5, 6 and 9, so I moved my RGB LED connections to these.

My problem is that the global variable "volatile unsigned long val;" resets to zero after some time.

My Arduino is Powered from a AC/DC Adapter.

The code is this

#include "IRLibAll.h"

//My Remote Codes
#define POWER  551489775
#define ONE    551520375
#define TWO    551504055
#define THREE  551536695
#define FOUR   551495895
#define FIVE   551528535
#define SIX    551512215
#define SEVEN  551544855
#define EIGHT  551491815
#define NINE   551524455
#define ZERO   551487735
#define RED    551505585
#define GREEN  551521905
#define YELLOW 551536185
#define BLUE   551519865
#define UP     551486205
#define DOWN   551518845
#define LEFT   551542815
#define RIGHT  551510175
#define VOL+   551502015
#define VOL-   551534655
#define CH+    551485695
#define CH-    551518335


IRrecv myReceiver(2);
IRdecode myDecoder;

int redPin = 9;
int greenPin = 5;
int bluePin = 6;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  
  setColor(255, 0, 0);
  delay(500);
  setColor(0, 255, 0);
  delay(500);
  setColor(0, 0, 255);
  delay(500);
  setColor(0, 0 , 0);

  myReceiver.enableIRIn();
  Serial.println("Ready!");
}

volatile unsigned long val;

void loop()
{
  if (myReceiver.getResults()) {
    myDecoder.decode();
    //myDecoder.dumpResults(true);
    myReceiver.enableIRIn();
    if (myDecoder.value != 0xFFFFFFFF)
      val = myDecoder.value;
    Serial.println(val, HEX);
  }

  Serial.println(val);
  switch (val) {
    case RED:
      fade(redPin);
      break;

    case GREEN:
      fade(greenPin);
      break;

    case BLUE:
      fade(bluePin);
      break;

    case YELLOW:
      fadeYellow();
      break;

    case ONE:
      rainbow();
      break;

    case TWO:
      faderTwo();
      break;

    case THREE:
    faderThree();
      break;

    case FOUR:
      faderFour();
      break;

    case FIVE:
      faderFive();
      break;

    case SIX:
      faderSix();
      break;

    case SEVEN:
    faderSeven();
      break;

    default:
      faderTwo();
      break;
  }
}

void fade(int pin)
{
  setColor(0, 0, 0);

  for (int fadeValue = 0; fadeValue <=255; fadeValue += 5) {
    analogWrite(pin, fadeValue);
    delay(30);
  }

  for (int fadeValue = 255; fadeValue >= 0; fadeValue -=5) {
    analogWrite(pin, fadeValue);
    delay(30);
  }
}


void fadeYellow()
{
  setColor(255, 255, 0);
  for (int fadeValue = 0; fadeValue <=255; fadeValue += 5) {
    analogWrite(redPin, fadeValue);
    analogWrite(greenPin, fadeValue);
    delay(30);
  }

  for (int fadeValue = 255; fadeValue >= 0; fadeValue -=5) {
    analogWrite(redPin, fadeValue);
    analogWrite(greenPin, fadeValue);
    delay(30);
  }
  
}

void setColor(unsigned char r,unsigned char g,unsigned char b)
{
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);
}

//ONE
void rainbow()
{
  int r = 148;
  int g = 0;
  int b = 211;
  setColor(r, g, b);
  delay(1000);

  setColor(75, 0, 130);
  delay(1000);

  setColor(0, 0, 255);
  delay(1000);

  setColor(0, 255, 0);
  delay(1000);

  setColor(255, 255, 0);
  delay(1000);

  setColor(255, 127, 0);
  delay(1000);

  setColor(255, 0, 0);
  delay(1000);

  setColor(0, 0, 0);
}

//TWO
void faderTwo()
{
  setColor(255, 0, 255);
  for (int fadeValue = 0; fadeValue <=255; fadeValue += 5){
    analogWrite(greenPin, fadeValue);
    delay(30);
  }

  for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5){
    analogWrite(greenPin, fadeValue);
    delay(30);
  }
  

}

void faderThree()
{
  // fade from green to red
  for(int i=0; i<255; i++) {
    analogWrite(redPin, i);
    analogWrite(greenPin, 255-i);
    analogWrite(bluePin, 0);
    delay(30);
  }

  // fade from red to blue
  for(int i=0; i<255; i++) {
    analogWrite(redPin, 255-i);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, i);
    delay(30);
  }

  // fade from blue to green
  for(int i=0; i<255; i++) {
    analogWrite(redPin, 0);
    analogWrite(greenPin, i);
    analogWrite(bluePin, 255-i);
    delay(30);
  }
}


void faderFour()
{
  setColor(0, 255, 255);
  for (int fadeValue = 0; fadeValue <=255; fadeValue += 5){
    analogWrite(redPin, fadeValue);
    delay(30);
  }

  for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5){
    analogWrite(redPin, fadeValue);
    delay(30);
  }
}


void faderFive()
{
  setColor(0, 255, 255);
  for (int fadeValue = 0; fadeValue <=255; fadeValue += 5){
    analogWrite(redPin, fadeValue);
    delay(30);
  }

  for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5){
    analogWrite(redPin, fadeValue);
    delay(30);
  }
  
}


void faderSix()
{
  setColor(255, 127, 80);
  for (int fadeValue = 80; fadeValue <=255; fadeValue += 5){
    analogWrite(bluePin, fadeValue);
    delay(30);
  }

  for (int fadeValue = 255; fadeValue >= 80; fadeValue -= 5){
    analogWrite(bluePin, fadeValue);
    delay(30);
  }
  
}


void faderSeven()
{
  setColor(128, 0, 0);
  delay(1000);
  setColor(139, 0, 0);
  delay(1000);
  setColor(165, 42, 42);
  delay(1000);
  setColor(178, 34, 34);
  delay(1000);
  setColor(220, 0, 0);
  delay(1000);
  setColor(255, 0, 0);
  delay(1000);


  setColor(220, 0, 0);
  delay(1000);
  setColor(178, 34, 34);
  delay(1000);
  setColor(165, 42, 42);
  delay(1000);
  setColor(139, 0, 0);
  delay(1000);
  setColor(128, 0, 0);
  delay(1000);
}

MoodLight.ino (5.11 KB)

Just tried doing it. It shows the same behaviour, volatile or not.

You’ll sometimes get interference which gives stray values from the IR receiver.
Here , you should check if the value returned is one of the 15 or so that you are expecting, before updating Val :
if (myDecoder.value != 0xFFFFFFFF)
. . .

Okay!!... I'll try that.

So it's been around 15 min, since I did the modification you suggested and its working fine.

It used to reset within 15 min.

So, I think this is the solution and will work perfectly. Thank you so much!!