Loop IR Code help please!

Hello, i would like to loop the case section until i get a new IR code then i want it to do the things that code points to. is there any way i could do this?

#include <IRremote.h>

/*~~~~ Define Paramaters For Flashy Flash (Flicker) ~~~~*/
int i = 0;

int sd1 = 500;
int sd2 = 1;
int ld1 = 500;
int ld2 = 501;

/*~~~~ Shit For IR Decode Loop ~~~~*/

int Code = 0;        // the ir code recived
int refCode = 1;     // same as prevCode- just kept for longer
int prevCode = 2;    // the previous ir code recived
bool change = true;  // was there a change in the ir code?

/*~~~~ Define Infrared Input Pin And LED Output Pins ~~~~*/

const int IRin = 2;
const int REDPIN = 3;
const int GREENPIN = 5;
const int BLUEPIN = 6;

/*~~~~ Setup Basic Sending (For LED Colors) ~~~~*/

void wr(int val) {
  analogWrite(REDPIN, val);
}

void wg(int val) {
  analogWrite(GREENPIN, val);
}

...

void off() {
  wr(0);
  wg(0);
  wb(0);
}

/*~~~~ Setup The Library For IR Input ~~~~*/

IRrecv irrecv(IRin);
decode_results results;

/*~~~~ General Setup ~~~~*/

void setup() {

  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);

  /*~~~~ Setup Serial Baud Rate And Send Warning ~~~~*/

  Serial.begin(9600);
  Serial.println("/~~~~~~~~~~~~~~~~~~~~~~~~~~ Debug Terminal ~~~~~~~~~~~~~~~~~~~~~~~~~~\\");
  Serial.println("|                                                                    |");
  Serial.println("|   !!! AUTHERIZED LIGHT-WAYZ OR MONSTERS ALIVE PERSONEL ONLY !!!    |");
  Serial.println("|                                                                    |");
  Serial.println("| THIS TERMINAL CAN MODIFY THE  CONTROL BOARD AND DESTROY YOUR LIGHT |");
  Serial.println("|                                                                    |");
  Serial.println("\\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/");

  /*~~~~ Start The Receiver Script ~~~~*/

  irrecv.enableIRIn();
}

/*~~~~ Prepare For LED Color Sending ~~~~*/

void Color_Wheel(int speed, int loopCount, int color) {
  switch (speed) {
    case 1:
      switch (color) {
        case 1:  //red
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            red();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 2:  //green
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 3:  //blue
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            blue();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 4:  //yellow
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 5:  //pale yellow
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 6:  //purple
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 7:  //pink
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 8:  //orange
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 9:  //white
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
      }
      break;

    case 2:
      switch (color) {
        case 1:  //red
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            red();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 2:  //green
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 3:  //blue
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            blue();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 4:  //yellow
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 5:  //pale yellow
          while (i <= loopCount) {
            delay(rand() % ld1 + sd2);
            green();
            delay(rand() % ld1 + sd2);
            off();
            i += 1;
          }
          break;
        case 6:  //purple
          while (i <= loopCount) {
            delay(rand() % ld1 + sd2);
            green();
            delay(rand() % ld1 + sd2);
            off();
            i += 1;
          }
          break;
        case 7:  //pink
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 8:  //orange
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 9:  //white
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
      }
      break;

    case 3:
      break;
  }
}

void flicker(int speed, int loopCount, int type) {
  switch (type) {
    case 1:
      Serial.println("Red/Orange Flicker");
    case 2:
      Serial.println("Red/Orange/Yellow Flicker");
      while (i <= loopCount) {
        Serial.println("BEFORE RED");
        Color_Wheel(speed, 1, 1);
        Serial.println("RED");
        delay(rand() % sd1 + sd2);
        Serial.println("BEFORE ORANGE");
        Color_Wheel(speed, 1, 4);
        Serial.println("ORANGE");
        delay(rand() % sd1 + sd2);
        Serial.println("BEFORE YELLOW");
        Color_Wheel(speed, 1, 5);
        Serial.println("YELLOW");
        delay(rand() % sd1 + sd2);
        Serial.println("BEFORE OFF");
        off();
        Serial.println("OFF");
        break;
      }
  }
}


/*~~~~ Watch For IR Commands ~~~~*/

void Light_Controller() {

  int Code = results.value;

      switch (Code) {

// this is the case section i want to repeat

      /*
      * Red
      * Green
      * Blue
      * Yellow
      * Pale Yellow
      * Purple
      * Pink
      * Orange
      * White
      */
        case 0xFFA25D:
          Serial.println("Code 1");
          off();
          flicker(1, 10, 2);
          break;

        case 0xFFE21D:
          Serial.println("Code 2");
          off();
          Color_Wheel(1, 5, 1);
          break;

        case 0xFF629D:
          Serial.println("Code 3");
          off();
          green();
          break;

        case 0xFF22DD:
          Serial.println("Code 4");
          off();
          blue();
          break;

        case 0xFF02FD:
          Serial.println("Code 5");
          break;

        case 0xFFC23D:
          Serial.println("Code 6");
          break;

        case 0xFFE01F:
          Serial.println("Code 7");
          break;

        case 0xFFA857:
          Serial.println("Code 8");
          break;

        case 0xFF906F:
          Serial.println("Code 9");
          break;

        case 0xFF9867:
          Serial.println("Code 10");
          break;

        case 0xFFB04F:
          Serial.println("Code 11");
          break;

        case 0xFF6897:
          Serial.println("Code 12");
          break;

        case 0xFF30CF:
          Serial.println("Code 13");
          break;

        case 0xFF18E7:
          Serial.println("Code 14");
          break;

        case 0xFF7A85:
          Serial.println("Code 15");
          break;

        case 0xFF10EF:
          Serial.println("Code 16");
          break;

        case 0xFF38C7:
          Serial.println("Code 17");
          break;

        case 0xFF5AA5:
          Serial.println("Code 18");
          break;

        case 0xFF42BD:
          Serial.println("Code 19");
          break;

        case 0xFF4AB5:
          Serial.println("Code 20");
          break;

        case 0xFF52AD:
          Serial.println("Code 21");
          break;


          /*
      * Red
      * Green
      * Blue
      * Yellow
      * Pale Yellow
      * Purple
      * Pink
      * Orange
      * White
      */
        default:
          Serial.println("***         !! ERROR !!         ***");
          Serial.println("*** !! UNKNOWN CODE RECEIVED !! ***");
          Serial.println("***         !! ERROR !!         ***");
          Serial.println();
          Serial.println("Please Report \"ERR: 001\" to 573-***-****OR Email ************@outlook.com");
      }
    delay(500);


    }

  } else if (Code == prevCode) {
    Serial.println("**********");
    Serial.println("* Repeat *");
    Serial.println("**********");

  } else {
    Serial.println("Now Ya Done Fucked Yourself!");
  }

  prevCode = Code;
}

/*~~~~ Start The Main Command Loop ~~~~*/

void loop() {
  if (irrecv.decode(&results)) {
    Light_Controller();
    Serial.println();
    irrecv.resume();
  }
}

Yes.

You check the IR receive twice before the received data is decoded, if (irrecv.decode(&results)) { Why? I ask because the first time the decode does the thing is the receive buffer cleared so that when the 2nd read is done the buffer is empty?

i guess your confusing me?

Huh?

It is your code. You wrote it.

You wrote

void loop() {
  if (irrecv.decode(&results)) {
    Light_Controller();
    Serial.println();
    irrecv.resume();
  }
}

and

void Light_Controller() {

  int Code = results.value;

  if (Code != prevCode) {


      if (irrecv.decode(&results)) {
        int Code = results.value;

        irrecv.resume();
      }

Why are you checking the received data twice?

What is your code supposed to do? What is it doing?

i did not mean to leave that second one in... whoops... that was from me experimenting around. let me update my main post rq

when you change the code post the changed code in a new message.

#include <IRremote.h>

/*~~~~ Define Paramaters For Flashy Flash (Flicker) ~~~~*/
int i = 0;

int sd1 = 500;
int sd2 = 1;
int ld1 = 500;
int ld2 = 501;

/*~~~~ Shit For IR Decode Loop ~~~~*/

int Code = 0;        // the ir code recived
int refCode = 1;     // same as prevCode- just kept for longer
int prevCode = 2;    // the previous ir code recived
bool change = true;  // was there a change in the ir code?

/*~~~~ Define Infrared Input Pin And LED Output Pins ~~~~*/

const int IRin = 2;
const int REDPIN = 3;
const int GREENPIN = 5;
const int BLUEPIN = 6;

/*~~~~ Setup Basic Sending (For LED Colors) ~~~~*/

void wr(int val) {
  analogWrite(REDPIN, val);
}

void wg(int val) {
  analogWrite(GREENPIN, val);
}

...

void off() {
  wr(0);
  wg(0);
  wb(0);
}

/*~~~~ Setup The Library For IR Input ~~~~*/

IRrecv irrecv(IRin);
decode_results results;

/*~~~~ General Setup ~~~~*/

void setup() {

  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);

  /*~~~~ Setup Serial Baud Rate And Send Warning ~~~~*/

  Serial.begin(9600);
  Serial.println("/~~~~~~~~~~~~~~~~~~~~~~~~~~ Debug Terminal ~~~~~~~~~~~~~~~~~~~~~~~~~~\\");
  Serial.println("|                                                                    |");
  Serial.println("|   !!! AUTHERIZED LIGHT-WAYZ OR MONSTERS ALIVE PERSONEL ONLY !!!    |");
  Serial.println("|                                                                    |");
  Serial.println("| THIS TERMINAL CAN MODIFY THE  CONTROL BOARD AND DESTROY YOUR LIGHT |");
  Serial.println("|                                                                    |");
  Serial.println("\\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/");

  /*~~~~ Start The Receiver Script ~~~~*/

  irrecv.enableIRIn();
}

/*~~~~ Prepare For LED Color Sending ~~~~*/

void Color_Wheel(int speed, int loopCount, int color) {
  switch (speed) {
    case 1:
      switch (color) {
        case 1:  //red
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            red();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 2:  //green
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 3:  //blue
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            blue();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 4:  //yellow
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 5:  //pale yellow
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 6:  //purple
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 7:  //pink
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 8:  //orange
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
        case 9:  //white
          while (i <= loopCount) {
            delay(rand() % sd1 + sd2);
            green();
            delay(rand() % sd1 + sd2);
            off();
            i += 1;
          }
          break;
      }
      break;

    case 2:
      switch (color) {
        case 1:  //red
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            red();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 2:  //green
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 3:  //blue
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            blue();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 4:  //yellow
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 5:  //pale yellow
          while (i <= loopCount) {
            delay(rand() % ld1 + sd2);
            green();
            delay(rand() % ld1 + sd2);
            off();
            i += 1;
          }
          break;
        case 6:  //purple
          while (i <= loopCount) {
            delay(rand() % ld1 + sd2);
            green();
            delay(rand() % ld1 + sd2);
            off();
            i += 1;
          }
          break;
        case 7:  //pink
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 8:  //orange
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
        case 9:  //white
          while (i <= loopCount) {
            delay(rand() % ld1 + ld2);
            green();
            delay(rand() % ld1 + ld2);
            off();
            i += 1;
          }
          break;
      }
      break;

    case 3:
      break;
  }
}

void flicker(int speed, int loopCount, int type) {
  switch (type) {
    case 1:
      Serial.println("Red/Orange Flicker");
    case 2:
      Serial.println("Red/Orange/Yellow Flicker");
      while (i <= loopCount) {
        Serial.println("BEFORE RED");
        Color_Wheel(speed, 1, 1);
        Serial.println("RED");
        delay(rand() % sd1 + sd2);
        Serial.println("BEFORE ORANGE");
        Color_Wheel(speed, 1, 4);
        Serial.println("ORANGE");
        delay(rand() % sd1 + sd2);
        Serial.println("BEFORE YELLOW");
        Color_Wheel(speed, 1, 5);
        Serial.println("YELLOW");
        delay(rand() % sd1 + sd2);
        Serial.println("BEFORE OFF");
        off();
        Serial.println("OFF");
        break;
      }
  }
}


/*~~~~ Watch For IR Commands ~~~~*/

void Light_Controller() {

  int Code = results.value;

      switch (Code) {

// this is the case section i want to repeat

      /*
      * Red
      * Green
      * Blue
      * Yellow
      * Pale Yellow
      * Purple
      * Pink
      * Orange
      * White
      */
        case 0xFFA25D:
          Serial.println("Code 1");
          off();
          flicker(1, 10, 2);
          break;

        case 0xFFE21D:
          Serial.println("Code 2");
          off();
          Color_Wheel(1, 5, 1);
          break;

        case 0xFF629D:
          Serial.println("Code 3");
          off();
          green();
          break;

        case 0xFF22DD:
          Serial.println("Code 4");
          off();
          blue();
          break;

        case 0xFF02FD:
          Serial.println("Code 5");
          break;

        case 0xFFC23D:
          Serial.println("Code 6");
          break;

        case 0xFFE01F:
          Serial.println("Code 7");
          break;

        case 0xFFA857:
          Serial.println("Code 8");
          break;

        case 0xFF906F:
          Serial.println("Code 9");
          break;

        case 0xFF9867:
          Serial.println("Code 10");
          break;

        case 0xFFB04F:
          Serial.println("Code 11");
          break;

        case 0xFF6897:
          Serial.println("Code 12");
          break;

        case 0xFF30CF:
          Serial.println("Code 13");
          break;

        case 0xFF18E7:
          Serial.println("Code 14");
          break;

        case 0xFF7A85:
          Serial.println("Code 15");
          break;

        case 0xFF10EF:
          Serial.println("Code 16");
          break;

        case 0xFF38C7:
          Serial.println("Code 17");
          break;

        case 0xFF5AA5:
          Serial.println("Code 18");
          break;

        case 0xFF42BD:
          Serial.println("Code 19");
          break;

        case 0xFF4AB5:
          Serial.println("Code 20");
          break;

        case 0xFF52AD:
          Serial.println("Code 21");
          break;


          /*
      * Red
      * Green
      * Blue
      * Yellow
      * Pale Yellow
      * Purple
      * Pink
      * Orange
      * White
      */
        default:
          Serial.println("***         !! ERROR !!         ***");
          Serial.println("*** !! UNKNOWN CODE RECEIVED !! ***");
          Serial.println("***         !! ERROR !!         ***");
          Serial.println();
          Serial.println("Please Report \"ERR: 001\" to 573-***-****OR Email ************@outlook.com");
      }
    delay(500);


    }

  } else if (Code == prevCode) {
    Serial.println("**********");
    Serial.println("* Repeat *");
    Serial.println("**********");

  } else {
    Serial.println("Now Ya Done Fucked Yourself!");
  }

  prevCode = Code;
}

/*~~~~ Start The Main Command Loop ~~~~*/

void loop() {
  if (irrecv.decode(&results)) {
    Light_Controller();
    Serial.println();
    irrecv.resume();
  }
}

What is the issue?

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frustrating for you and frustrating for us. The people who try to help with your pro…

It will help you get the best out of the forum in the future.

Your OS and version can be valuable information, please include it along with extra security you are using.

Always list the version of the IDE you are using and the board version if applicable.

Use quote or add error messages as an attachment NOT a picture.

How to insert an image into your post. ( Thanks @sterretje )

Add your sketch where applicable but please use CODE TAGS ( </> )

Add a SCHEMATIC were needed even if it is hand drawn

Add working links to any specific hardware as needed (NOT links to similar items)

Remember that the people trying to help cannot see your problem so give as much information as you can

COMMON ISSUES

Ensure you have FULLY inserted the USB cables.

Check you have a COMMON GROUND where required. ( Thanks @Perry)

Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.

Try other computers where possible.

Try other USB leads where possible.

You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI

There may be a problem with the board check or remove your wiring first.

Remove any items connected to pins 0 and 1.

COMPUTER RELATED

Close any other serial programs before opening the IDE.

Ensure you turn off any additional security / antivirus just to test.

There may be a problem with the PC try RESTARTING it.

You may be selecting the wrong COM port.

Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.

Clear your browsers CACHE.

Close the IDE before using any other serial programs.

Preferably install IDE’s as ADMINISTRATOR or your OS equivalent

ARDUINO SPECIFIC BOARDS

CH340/341 based clones do not report useful information to the “get board info” button.

NANO (Old Types) some require you to use the OLD BOOTLOADER option.

NANO (ALL Types) See the specific sections lower in the forum.

NANO (NEW Types) Install your board CORE’s.

Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default “AVRISP mkII”.

Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.

CREATE editor install locations.

On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini

On Linux ~/ArduinoCreateAgent-1.1/config.ini

On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1

Performing the above actions may help resolve your problem without further help.
Language problem ?
Try a language closer to your native language:

中文 (Chinese)

Deutsch - German

Español - Spanish

Français - French

অসমীয়া, বাংলা, बड़ो - India

Italiano - Italian

Nederlands - Dutch

Portugues - Portuguese

Россия - Russia

Scandinavia Swedish, Suomi - Finnish, Norsk - Norwegian

Thanks to all those who helped and added to this list.

Thank you.

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