IR Remote - LED Loop

I know… the code is a mess. I am trying to create a program where a loop (the LED loops) will run when a signal is received from an IR remote until another IR signal is received. Once another signal is received it should end the LED loop that it is running, translate the signal, and select a new loop based on the input and run that loop until a signal is received again.

Right now I have it to where it will run the corresponding LED loop when a particular button on the remote is pressed. However, it doesn’t receive/translate a new signal until the LED loop has ended.

#include "IRremote.h"

/*-----( Declare Constants )-----*/
int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11

/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'
/*-----( Declare Variables )-----*/

void setup()   /*----( SETUP: RUNS ONCE )----*/
  Serial.println("YourDuino IR Receiver Button Decode Test");
  irrecv.enableIRIn(); // Start the receiver
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
  if (irrecv.decode(&results)) // have we received an IR signal?

//    Serial.println(results.value, HEX);  UN Comment to see raw values
    irrecv.resume(); // receive the next value
}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
void translateIR() // takes action based on IR code received

// describing KEYES Remote IR codes 




  case 0x20DF40BF: Serial.println(results.value);    //print button value
  forwards();                                       //perform forward led loop

  case 0x20DFC03F: Serial.println(results.value); //print button value
  backwards();                                   //perform forward led loop

    Serial.println(" other button   ");

  }// End Case



//////LED LOOPS/////

    void backwards () {
    // loop from the highest pin to the lowest:
  for (int f = 7; f >= 5; f--) {
    // turn the pin on:
    digitalWrite(f, HIGH);
    // turn the pin off:
    digitalWrite(f, LOW);


  void forwards () {
     for (int f=5; f<8; f++ ) {
    int g = f+1;


The keyword you are looking for is non-blocking code. With your led cycles all programmed with for loops and delays, your code is stuck there doing all the led stuff for a long time and not getting back to the lines about reading the remote. You need to refactor the code so that it doesn't do that. See the "Blink Without Delay" example for some inspiration. Instead of thinking about code telling the whole story from beginning to end in a linear fashion, think about your code like a checklist that can tell you at any given time what the next small step you need to take is.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code. Do not post double spaced code.