Remote Control Fade with Millis()

Can someone please help me out? I’ve tried time and again to get a fade to turn on and off by IR remote. I borrowed sketch from bald engineer and I have a basic on/off sketch that I can turn an LED on and off but I just can’t get the fade to. Any help would be appreciated.

FADE

// define directions for LED fade
#define UP 0
#define DOWN 1
 int pwmLED=5;
// constants for min and max PWM
const int minPWM = 2;
const int maxPWM = 255;
 
// State Variable for Fade Direction
byte fadeDirection = UP;
 
// Global Fade Value
// but be bigger than byte and signed, for rollover
int fadeValue = 0;
 
// How smooth to fade?
byte fadeIncrement = 4;
 
// millis() timing Variable, just for fading
unsigned long previousFadeMillis;
 
// How fast to increment?
int fadeInterval = 130;
 
void setup() {
  // put pwmLED into known state (off)
  analogWrite(pwmLED, fadeValue); 
}
 
void doTheFade(unsigned long thisMillis) {
  // is it time to update yet?
  // if not, nothing happens
  if (thisMillis - previousFadeMillis >= fadeInterval) {
    // yup, it's time!
    if (fadeDirection == UP) {
      fadeValue = fadeValue + fadeIncrement;  
      if (fadeValue >= maxPWM) {
        // At max, limit and change direction
        fadeValue = maxPWM;
        fadeDirection = DOWN;
      }
    } else {
      //if we aren't going up, we're going down
      fadeValue = fadeValue - fadeIncrement;
      if (fadeValue <= minPWM) {
        // At min, limit and change direction
        fadeValue = minPWM;
        fadeDirection = UP;
      }
    }
    // Only need to update when it changes
    analogWrite(pwmLED, fadeValue);  
 
    // reset millis for the next iteration (fade timer only)
    previousFadeMillis = thisMillis;
  }
}
 
void loop() {
  // get the current time, for this time around loop
  // all millis() timer checks will use this time stamp
  unsigned long currentMillis = millis();
    
  doTheFade(currentMillis);

}

REMOTE

 /* 
    source: www.electroschematics.com
    You'll need to change the led pins and the codes 
    accordingly to your configuration and IR remote
    */
     
    #include <IRremote.h>
     
    int RECV_PIN = 12; // the pin where you connect the output pin of TSOP4838
    int ledPin = 5;
    
    int itsONled[] = {0,0,0};
   

    /* the initial state of LEDs is OFF (zero) 
    the first zero must remain zero but you can 
    change the others to 1's if you want a certain
    led to light when the board is powered */
    #define code1  41565 // code received from pwr button
    #define code2  57885 // code received from mute button
  
    IRrecv irrecv(RECV_PIN);
     
    decode_results results;
     
    void setup()
    {
      Serial.begin(9600);   // you can comment this line
      irrecv.enableIRIn();  // Start the receiver
      pinMode(ledPin, OUTPUT);
    
    }
     
    void loop() 
    {
      
      if (irrecv.decode(&results)) {
        unsigned int value = results.value;
        switch(value) {
           case code1:
             if(itsONled[1] == 1) {        // if first led is on then
                digitalWrite(ledPin, LOW);   // turn it off when button is pressed
                itsONled[1] = 0;           // and set its state as off
             } else {                      // else if first led is off
                 digitalWrite(ledPin, HIGH); // turn it on when the button is pressed
                 itsONled[1] = 1;          // and set its state as on
             }
             
                  
        }
        Serial.println(value); // you can comment this line
        irrecv.resume(); // Receive the next value
      }
    }

Your fade sketch doesn’t stop when it hits the limit. How about adding a value for “NEITHER” along with UP and DOWN. Then, when you reach the top when going UP or the bottom when going DOWN you can set the direction to NEITHER and not do anything:

    if (fadeDirection == UP)
    {
      fadeValue = fadeValue + fadeIncrement;
      if (fadeValue >= maxPWM)
      {
        // At max, limit and change direction
        fadeValue = maxPWM;
        fadeDirection = NEITHER;
      }
    }
    else if (fadeDirection == DOWN)
    {
      fadeValue = fadeValue - fadeIncrement;
      if (fadeValue <= minPWM)
      {
        // At min, limit and change direction
        fadeValue = minPWM;
        fadeDirection = NEITHER;
      }
    }

Then you would have the Power button set the direction to UP and the Mute button set the direction to DOWN.

Thanks the problem is I can’t get the remote incorporated in to the fade sketch. If I need to use one for on and one for off that’s fine. I’ve tried everything I’m capable of.

So what happens when you slap the two sketches together, like this?

#include <IRremote.h>


int RECV_PIN = 12; // the pin where you connect the output pin of TSOP4838
IRrecv irrecv(RECV_PIN);

const unsigned int code1 = 41565; // code received from pwr button
const unsigned int code2  = 57885; // code received from mute button

// define directions for LED fade
enum Direction {UP, DOWN, NEITHER} FadeDirection = UP;


int PWMLEDPin = 5;


// constants for min and max PWM
const int MinPWM = 2;
const int MaxPWM = 255;




// Global Fade Value
// but be bigger than byte and signed, for rollover
int FadeValue = 0;


// millis() timing Variable, just for fading
unsigned long PreviousFadeMillis;


// How often to increment?
const unsigned long FadeInterval = 130;
// How smooth to fade?
const byte FadeIncrement = 4;


void setup()
{
  Serial.begin(9600);   // you can comment this line
  irrecv.enableIRIn();  // Start the receiver


  // put pwmLED into known state (off)
  analogWrite(PWMLEDPin, FadeValue);
}




void loop()
{
  ReadIR();
  doTheFade();
}


void doTheFade()
{
  // get the current time, for this time around loop
  // all millis() timer checks will use this time stamp
  unsigned long currentMillis = millis();


  // is it time to update yet?
  // if not, nothing happens
  if (currentMillis - PreviousFadeMillis >= FadeInterval)
  {
    // yup, it's time!
    PreviousFadeMillis += FadeInterval;


    if (FadeDirection == UP)
    {
      FadeValue += FadeIncrement;
      if (FadeValue >= MaxPWM)
      {
        // At max, limit and change direction
        FadeValue = MaxPWM;
        FadeDirection = NEITHER;
      }
    }
    else if (FadeDirection == DOWN)
    {
      FadeValue -= FadeIncrement;
      if (FadeValue <= MinPWM)
      {
        // At min, limit and change direction
        FadeValue = MinPWM;
        FadeDirection = NEITHER;
      }
    }
    // Only need to update when it changes
    analogWrite(PWMLEDPin, FadeValue);
  }
}


void ReadIR()
{
  decode_results results;
  if (irrecv.decode(&results))
  {
    unsigned int value = results.value;
    switch (value)
    {
      case code1:
        FadeDirection = UP;
        break;


      case code2:
        FadeDirection = DOWN;
        break;


      default:
        Serial.print(F("Unknown code: "));
        Serial.println(value);
        break;
    }


    irrecv.resume(); // Receive the next value
  }
}

Well at upload to Arduino it fades up to max and stops. Down button (mute) fades to min. and stops. Power (up) fade to max and stops. Its not cycling. It's for a lighthouse I made, like to sit on a shelf and turn the fade on and off remotely. Thanks for your help.