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   ");

  }

I seem to remember this query, or something very similar, in another recent thread.
Was that yours ?

Hi UKHeliBob,

Yes there was an old thread a few weeks ago now. Since I wasn't able to resolve the problem for Martin and other people trying to help, I have reposted the same problem.

The other cases work well no problem but it's this particular case that is bugging me now.

Define a global boolean state variable say relayOn. Initialise it to false.
If code 0xFF629D is received when relayOn is false, set relayOn to true and switch the relay on.
If code 0xFF629D is received when relayOn is true, set relayOn to false and switch the relay off.

I have heard you can use button states but I am not comfortable with it. Is the any example code or how would the code look like this purpose?

In this case, it is not a "button state", it is a relay state, but in principle it is the same.

You define the variable like this. It is global if you define it outside of a function:

bool relayOn = false ;

you test it like this example:

if ( relayOn == false ) {
// do something
}

you set it like this example:

relayOn = true ;

Use it to "remember" the state of the relay so alternate button presses can have an effect dependent on the state of the relay.

Thank you for the explanation,

So I real terms, I would include something like this:

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

{

// Global variable part  

bool AutomaticMode = false ;

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 ( AutomaticMode == false ) { 

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

}
AutomaticMode = true ;    
break;

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

  }

How would it work in the case function like this?

You can't have a global variable, in your case automatic, defined in a function. It loses its value as soon as the function exits.
You could make it static, but you'd have to research the implications of doing this.

You are handling the case of receiving 0xFF629D where automatic is false. But you are not handling the case of receiving 0xFF629D where automatic is instead true.

Yes sorry that was a mistake from my part.

So in that case it would be the opposite? How would you use it in the code?

Thank you for your ongoing help