IR WRGB Led circuit resets to red when timer button is hit

Im using an arduino pro mini to control four mosfets connected to a 12V 5050 rgb led strip. I'm controlling it using an ir remote, and have added both a fade button to cycle the colors, and a four/eight hour timer to turn it off after a set duration. The only issue is, when the lights are fading and I hit one of the timer buttons, the light resets to red and stays that way. I believe the problem lies somewhere in the cycleColors() function due to it starting on red, but I havent been able to figure it any further. Any help would be appreciated, I'm close to just accepting it the way it is!

#include <IRremote.h>
IRrecv IR(8);  //Define IR Reciever Pin
decode_results results;
// Define Pins
const int whitePin = 10;
const int redPin = 6;
const int greenPin = 9;
const int bluePin = 5;

float maxB = 1; //Define Max Brightness
unsigned int ircode = 2; //Starts on power off when plugged in
unsigned int lastcode = 2; //Stores last code so fade doesnt stop after brightness change
unsigned long currentmillis = 0;
unsigned int cw = 0; //Stores current color values to update after brightness change
unsigned int cr = 0;
unsigned int cg = 0;
unsigned int cb = 0;

//Define IR Codes
const int brightup = 0;
const int brightdown = 1;
const int poweroff = 2;
const int poweron = 3;

const int red = 4;
const int green = 5;
const int blue = 6;
const int white = 7;
const int redorange = 8;
const int pickle = 9;
const int darklightblue = 10;
const int pumpkin = 12;
const int lightblue = 13;
const int purple = 14;
const int orange = 16;
const int jeans = 17;
const int lightpurple = 18;
const int yellow = 20;
const int darkerlightblue = 21;
const int pussy = 22;

const int fourh = 11;
const int eighth = 15;
const int fade = 19;
const int pause = 23;

void setup() {
  // put your setup code here, to run once:
  IR.enableIRIn();  //Start IR Communication
  pinMode(whitePin, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  setColorWRGB(0, 0, 0, 0); //Start off
}

void loop() {
  // put your main code here, to run repeatedly:
  setColor();
}

void setColorWRGB(unsigned int wh, unsigned int re, unsigned int gr, unsigned int bl) {
  analogWrite(whitePin, int(round(maxB * wh)));
  analogWrite(redPin, int(round(maxB * re)));
  analogWrite(greenPin, int(round(maxB * gr)));
  analogWrite(bluePin, int(round(maxB * bl)));
  cw = wh;
  cr = re;
  cg = gr;
  cb = bl;
}

void setColor() {
  if (IR.decode()) {
    lastcode = ircode;
    ircode = IR.decodedIRData.command;
    switch (ircode) {
      case brightup:
        if (maxB < 1) {
          maxB += .1;
          setColorWRGB(cw, cr, cg, cb);
        }
        if (lastcode == fade) {
          ircode = fade;
        }
        break;
      case brightdown:
        if (maxB > .1) {
          maxB -= .1;
          setColorWRGB(cw, cr, cg, cb);
        }
        if (lastcode == fade) {
          ircode = fade;
        }
        break;

      case poweroff:
        setColorWRGB(0, 0, 0, 0);
        break;
      case poweron:
        setColorWRGB(255, 0, 0, 0);
        break;

      case red:
        setColorWRGB(0, 255, 0, 0);
        break;
      case green:
        setColorWRGB(0, 0, 255, 0);
        break;
      case blue:
        setColorWRGB(0, 0, 0, 255);
        break;
      case white:
        setColorWRGB(255, 0, 0, 0);
        break;

      case redorange:
        setColorWRGB(0, 255, 40, 0);
        break;
      case pickle:
        setColorWRGB(0, 144, 255, 144);
        break;
      case darklightblue:
        setColorWRGB(0, 10, 0, 139);
        break;

      case pumpkin:
        setColorWRGB(0, 255, 102, 24);
        break;
      case lightblue:
        setColorWRGB(0, 0, 255, 255 );
        break;
      case purple:
        setColorWRGB(0, 35, 0, 35);
        break;

      case orange:
        setColorWRGB(0, 255, 140, 24);
        break;
      case jeans:
        setColorWRGB(0, 10, 92, 191);
        break;
      case lightpurple:
        setColorWRGB(0, 128, 0, 128);
        break;

      case yellow:
        setColorWRGB(0, 255, 255, 0);
        break;
      case darkerlightblue:
        setColorWRGB(0, 19, 0, 127);
        break;
      case pussy:
        setColorWRGB(0, 255, 0, 255);
        break;


      case eighth:
      case fourh:
        currentmillis = millis();
        break;
      case fade:
        cycleColors();
        break;
      case pause:
        setColorWRGB(cw, cr, cg, cb);
        break;
    }

    IR.resume();
  }

  else if (ircode == fourh) {
    if ((millis() - currentmillis) >= 14400000) {
      setColorWRGB(0, 0, 0, 0);
    }
    else if (lastcode == fade) {
      cycleColors();
    }
  }

  else if (ircode == eighth) {
    if ((millis() - currentmillis) >= 28800000) {
      setColorWRGB(0, 0, 0, 0);
    }
    else if (lastcode == fade) {
      cycleColors();
    }
  }

  else if (ircode == fade) {
    cycleColors();
  }
}

void cycleColors() {
  unsigned int rgbColour[3] = {255, 0, 0}; //Start with Red
  IR.resume();
  // Choose the colours to increment and decrement.
  for (int decColour = 0; decColour < 3; decColour += 1) {
    int incColour = decColour == 2 ? 0 : decColour + 1;

    // cross-fade the two colours.
    for (int i = 0; i < 255; i += 1) {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;

      setColorWRGB(0, rgbColour[0], rgbColour[1], rgbColour[2]);
      delay(20);
      if (IR.decode()) {
        return;
      }
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.