user5609622
user5609622

Reputation:

Arduino and RGB LEDs

Suppose I have an array:

int rgbcolors = {{R1,G1,B1},{R2,G2,B2},{R3,G3,B3},{Rn,Gn,Bn}}

How do I cycle through the values of this array by pressing 1 button on the remote? So pressing once, gives the first {R1,G1,B1}, pressing twice, gives {R2,G2,B2} etc...

My current void loop for predefined colors:

if (irrecv.decode(&results))
{
  int i = 0;
  int j = 0;
  int k = 0;

  unsigned int val = results.value;
  irrecv.resume();

  switch(val) {
      case button1: //RED
        analogWrite(RedPin,255);
        analogWrite(GreenPin,0);
        analogWrite(BluePin,0);
        break;
      case button2: //GREEN
        analogWrite(RedPin,0);
        analogWrite(GreenPin,255);
        analogWrite(BluePin,0);
        break;
      case button3: //BLUE
        analogWrite(RedPin,0);
        analogWrite(GreenPin,0);
        analogWrite(BluePin,255);
        break;
      case button4: //YEL0
        analogWrite(RedPin,255);
        analogWrite(GreenPin,255);
        analogWrite(BluePin,0);
        break;
      case button5: //PURPLE
        analogWrite(RedPin,255);
        analogWrite(GreenPin,0);
        analogWrite(BluePin,255);
        break;
      case button6: //TURQUOISE
        analogWrite(RedPin,0);
        analogWrite(GreenPin,255);
        analogWrite(BluePin,255);
        break;
      case button7: //ORANGE
        analogWrite(RedPin,255);
        analogWrite(GreenPin,140);
        analogWrite(BluePin,0);
        break;
      case button8: //LIME !!! DON'T USE // CRASHES
        analogWrite(RedPin,133);
        analogWrite(GreenPin,255);
        analogWrite(BluePin,140);
        break;
      case button9: //PINK
        analogWrite(RedPin,50);
        analogWrite(GreenPin,50);
        analogWrite(BluePin,50);
        break;
      case buttonOn:
        analogWrite(RedPin,255);
        analogWrite(GreenPin,255);
        analogWrite(BluePin,255);
        break;
      case buttonOff:
        analogWrite(RedPin,0);
        analogWrite(GreenPin,0);
        analogWrite(BluePin,0);
        break;

  }

} 

Upvotes: 1

Views: 551

Answers (1)

Matthew Heironimus
Matthew Heironimus

Reputation: 356

I unfortunately cannot help you with your "IR receiver value suddenly changed" issue, but I think I can help with your "cycle through the values of this array by pressing 1 button on the remote" question. The following is a code sample that does what I think you want to do:

const int NumberOfModes = 4;
int CurrentMode = -1;

void loop() {

    int rgbcolors[NumberOfModes][3] = {
        { 255,0,0 },  // RED
        { 0,255,0 },  // GREEN
        { 0,0,255 },  // BLUE
        { 255,255,0 } // YELLOW
    };

    if (irrecv.decode(&results))
    {
        unsigned int val = results.value;
        irrecv.resume();

        if (val == button1) {

            // Switch to new mode
            CurrentMode++;
            if (CurrentMode >= NumberOfModes)
            {
                CurrentMode = 0;
            }

            // Set output values
            analogWrite(RedPin, rgbcolors[CurrentMode][0]);
            analogWrite(GreenPin, rgbcolors[CurrentMode][1]);
            analogWrite(BluePin, rgbcolors[CurrentMode][2]);
        }

    }
}

Upvotes: 1

Related Questions