Switching between functions using an IR remote

Hello! I am new to programming in Arduino and I don't know how to switch between functions, while they are still running when I use my IR remote. My code lets me switch between many functions using my IR remote only after one function is finished, which is what I wanted to do. Now I want my functions to run continuously (as if I am using an infinite loop) and let me switch with another function when I press a button.

For example, I select to press button #1 from my IR remote, my code is running function #1 and it never stops. Now I press button #2 from my IR remote, my code switches from function #1 to function #2 and this function never stops running until I press another button.

This is my code:

#include "IRremote.h"

int receiver = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11

int tDelay = 200;
int latchPin = 11;      // (11) ST_CP [RCK] on 74HC595
int clockPin = 10;      // (10) SH_CP [SCK] on 74HC595
int dataPin = 12;     // (12) DS [S1] on 74HC595

int index = 0;

byte leds = 0;

IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void translateIR() // takes action based on IR code received
{
  switch(results.value)
  {
    
  case 0xFF30CF: Serial.println("1"); One_After_Another();   break;
  case 0xFF18E7: Serial.println("2");  One_At_a_Time();   break;
  case 0xFF7A85: Serial.println("3");  Ping_Pong();  break;
  case 0xFF10EF: Serial.println("4");  Random_LED_ON();  break;
  case 0xFF38C7: Serial.println("5");   Marquee();  break;
  case 0xFF5AA5: Serial.println("6");  Binary_Count();  break;
  

  default: 
    Serial.println(" other button ");

  }// End Case
  delay(500); // Do not get immediate repeat
}

void setup() 
{
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode"); 
  irrecv.enableIRIn(); // Start the receiver
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}

void shiftWrite(int Pin, boolean State){
  digitalWrite(latchPin, HIGH);
  bitWrite(leds, Pin, State);
  shiftOut(dataPin, clockPin, MSBFIRST, leds);
  digitalWrite(latchPin, LOW);
}

void loop() 
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    translateIR();
    irrecv.resume(); // receive the next value
  }
}
void One_After_Another(){
  for(int i=0;i<=4;i++){
    for(index=0; index<=7; index++){
      shiftWrite(index, HIGH);
      delay(tDelay);
    }
    for(index=7; index>=0; index--){
      shiftWrite(index, LOW);
      delay(tDelay);
    }
  }
}

void One_At_a_Time(){
  for(int i=0;i<=7;i++){
    for(index=0; index<=7; index ++){
      shiftWrite(index, HIGH);
      delay(tDelay);
      shiftWrite(index, LOW);
    }
  }
}

void Ping_Pong(){
  for(int i=0; i<=4; i++){
    for(index=0; index<=7; index++){
      shiftWrite(index, HIGH);
      delay(tDelay);
      shiftWrite(index, LOW);
    }
    for(index=7; index>=0; index--){
      shiftWrite(index, HIGH);
      delay(tDelay);
      shiftWrite(index, LOW);
    }
  } 
}

void Random_LED_ON(){
  for(int i=0; i<=69; i++){
    index=random(8);
    shiftWrite(index, HIGH);
    delay(tDelay);
    shiftWrite(index, LOW);
  }
}

void Marquee(){
  for(int i=0; i<=4;i++){
    for(index=0; index<=3; index++){
      shiftWrite(index, HIGH);
      shiftWrite(index+4, HIGH);
      delay(tDelay);
      shiftWrite(index, LOW);
      shiftWrite(index+4, LOW);
    }
  }
}

void Binary_Count(){
  int delayCount = 600;
  shiftOut(dataPin, clockPin, MSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
  digitalWrite(latchPin, LOW);
  leds++;
  delay(delayCount);
}

I would apreciate your help.

Something like:

#include "IRremote.h"

enum {
   NO_FUNCTION,
   ONE_AFTER_ANOTHER,
   ONE_AT_A_TIME,
   PING_PONG,
   RANDOM_LED_ON,
   MARQUEE,
   BINARY_COUNT
} functionState = NO_FUNCTION;
.
.
.
void translateIR() // takes action based on IR code received
{
  switch(results.value)
  {
    
  case 0xFF30CF: Serial.println("1"); functionState = ONE_AFTER_ANOTHER;   break;
  case 0xFF18E7: Serial.println("2"); functionState = ONE_AT_A_TIME;   break;
  case 0xFF7A85: Serial.println("3"); functionState = PING_PONG;  break;
  case 0xFF10EF: Serial.println("4"); functionState = RANDOM_LED_ON;  break;
  case 0xFF38C7: Serial.println("5"); functionState = MARQUEE;  break;
  case 0xFF5AA5: Serial.println("6"); functionState = BINARY_COUNT;  break;
  

  default: 
    Serial.println(" other button "); 
    functionState = NO_FUNCTION;

  }// End Case
  delay(500); // Do not get immediate repeat
}
.
.
.
void loop() 
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    translateIR();
    irrecv.resume(); // receive the next value
  }
  switch(functionState)
  {
    
    case ONE_AFTER_ANOTHER: One_After_Another();   break;
    case ONE_AT_A_TIME:  One_At_a_Time();   break;
    case PING_PONG: Ping_Pong();  break;
    case RANDOM_LED_ON: Random_LED_ON();  break;
    case MARQUEE: Marquee();  break;
    case BINARY_COUNT: Binary_Count();  break;
  }
}
.
.
.
1 Like

Hi, @absol125. If I understand your question correctly, you want a selected effect to be active indefinitely and at any time another effect can be selected via the remote. If so, you'll need to get rid of all the delay() and change your program fundamentally. See the example in the Arduino IDE Blink Without Delay for reference.

1 Like

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