Arduino looping in switch case until button pressed again

I am having issues with this part of the code. What I would like to happen is that when I press IR button 0xFF629D, I would like it to run forever unless I press the same button again. What I am trying to do is if the value of voltage is > 0.30 , the relay will turn on. After, it will continue looping until I want to exit the case by pressing the same button.

Is this possible to do?

I will not copy the entire code but this is the switch case part:

  void translateIR() // takes action based on IR code received

{

  switch(results.value)

  {

  case 0xFFA25D:
    Serial.println(" TOGGLE RELAY ON            ");
  digitalWrite(LEDACT, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LEDACT, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
  digitalWrite(RELAY, HIGH);
  digitalWrite(LEDACT, HIGH);
    break;


  case 0xFFE21D:
    Serial.println(" 1h30            ");
  for (int i = 0; i <= 2; i++) {
  digitalWrite(LEDACT, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LEDACT, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);
  }
  digitalWrite(LEDACT, HIGH);    
  digitalWrite(RELAY, HIGH);
  Serial.println(" RELAY ON");     
  delay(10000);           //1h30
  Serial.println(" RELAY OFF");
  digitalWrite(RELAY, LOW); 
  digitalWrite(LEDACT, LOW);
  break;

  case 0xFF22DD:
    Serial.println(" 12h           ");
  for (int i = 0; i <= 3; i++) {
  digitalWrite(LEDACT, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LEDACT, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);

  }
  digitalWrite(LEDACT, HIGH);
  digitalWrite(RELAY, HIGH);
  Serial.println(" RELAY ON");     
  delay(10000);           //12 hour
  Serial.println(" RELAY OFF");  
      digitalWrite(RELAY, LOW);
    digitalWrite(LEDACT, LOW);
    break;

  case 0xFF02FD:
    Serial.println(" TOGGLE RELAY OFF           ");
  for (int i = 0; i <= 3; i++) {
      digitalWrite(LEDACT, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(250);                       // wait for a second
      digitalWrite(LEDACT, LOW);    // turn the LED off by making the voltage LOW
      delay(250);
  }
     digitalWrite(RELAY, LOW);
     digitalWrite(LEDACT, LOW);
    break;



  case 0xFF629D: 

    int sensorValue = analogRead(A0);

  float voltage = sensorValue * (5.0 / 1023.0);

    Serial.println(" Automatic mode            ");   

  digitalWrite(LEDACT, HIGH);   // turn the LED on (HIGH is the voltage level)

  if (voltage > 0.30) {
  digitalWrite(RELAY, HIGH);
  Serial.println(" RELAY ON");
  delay(1000); 
  digitalWrite(RELAY, LOW);
  Serial.println(" RELAY OFF");
    digitalWrite(LEDACT, LOW);

}
    break;

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

  }

French forum here… C’est le forum en Français ici…

I will not copy the entire code but this is the switch case part:

Don’t post snippets (Snippets R Us!)

You need to restructure your code to match what you describe. A simple way to do this is with a simple state machine. something like this probably would get you started

#include <IRremote.h>
const byte receivePin = 11;
IRrecv irrecv(receivePin);
decode_results results;

const uint32_t magicCode = 0x00FF629D;
uint32_t lastIRCode = 0;

enum : byte {SYSTEM_IDLE, SYSTEM_RUNNING} systemState = SYSTEM_IDLE;


// ------------------------------------------
// You need to fill those in. lastIRCode will hold the last received command
void startSystem() {}
void stopSystem() {}
void runSystem() {}

// ------------------------------------------
void handleIRCommand()
{
  if (irrecv.decode(&results)) {
    lastIRCode = results.value ;
    if (results.value == magicCode) {
      switch (systemState) {
        case SYSTEM_IDLE:
          startSystem();
          systemState = SYSTEM_RUNNING;
          break;
        case SYSTEM_RUNNING:
          stopSystem();
          systemState = SYSTEM_IDLE;
          break;
      }
    }
    irrecv.resume(); // Receive the next value
  }
}


void setup()
{
  Serial.begin(115200);
  irrecv.enableIRIn();
}

void loop() {
  handleIRCommand();
  if (systemState == SYSTEM_RUNNING) runSystem();
}

You need to make sure that the code in runSystem() takes only one step and does not use delay() etc → might also be a state machine structure

void translateIR() // takes action based on IR code received
{


  switch (results.value)
  {


    /* ... */


    case 0xFF629D:
      {
        boolean repeat = true;


        irrecv.resume();


        // The part to repeat
        do
        {
          int sensorValue = analogRead(A0);


          float voltage = sensorValue * (5.0 / 1023.0);


          Serial.println(" Automatic mode            ");


          digitalWrite(LEDACT, HIGH);   // turn the LED on (HIGH is the voltage level)


          if (voltage > 0.30)
          {
            digitalWrite(RELAY, HIGH);
            Serial.println(" RELAY ON");
            delay(1000);
            digitalWrite(RELAY, LOW);
            Serial.println(" RELAY OFF");
            digitalWrite(LEDACT, LOW);
          }


          if (irrecv.decode(&results))
          {
            if (results.value == 0xFF629D)
              repeat = false;
            else
              irrecv.resume();  // No match this time, try again
          }
        }
        while (repeat);


        break;


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