IR switch case

Hello all,

So I nearly have the code working the way I want it to. However, how would I be able to have the automatic mode on the loop forever unless I press the same button I used to turn on automatic mode? A bit like a toggle switch on or off?

Code below:

#include "IRremote.h"
#include<NoDelay.h>

/*-----( Declare Constants )-----*/
int IR = 4; // pin 1 of IR receiver to Arduino digital pin 11
int LEDACT = 3; // LED activity
int RELAY = 6; // RELAY

//int voltage = 0.50 ;
  
// Flashing LED with photo
void ledBlink();//Must declare function before noDelay 

noDelay LEDtime(1, ledBlink);//Creats a noDelay varible set to 1000ms, will call ledBlink function
int LEDpin = 2;
int ledState = LOW;

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

/*--------- delays -----*/

const unsigned long delay_1h30 = 5400000; // 1h30 to ms is 5400000
const unsigned long delay_12h = 43200000; // 12h to ms is 43200000
const unsigned long interval = 1; // 1000Hz
const unsigned long interval1 = 10000; // read voltage every 1 second

unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;

unsigned long currentMillis = millis();
unsigned long lastStateChangeMillis = currentMillis;

/* ------ PHOTO --------*/
//
//  int sensorValue = analogRead(A0);
//
//  float voltage = sensorValue * (5.0 / 1023.0);
  

// FUNCTION FOR FLASH

void ledBlink()
{
  // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LEDpin, ledState);
}

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("Snow detector");
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LEDACT, OUTPUT);
  pinMode(LEDpin, OUTPUT);
  pinMode(RELAY, OUTPUT); 
  digitalWrite(RELAY, LOW);
   
}/*--(end setup )---*/


void loop(){   /*----( LOOP: RUNS CONSTANTLY )----*/ 
  int sensorValue = analogRead(A0);

  float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);
    LEDtime.fupdate();//will check if set time has past and if so will run set function
  
  if (irrecv.decode(&results)) // have we received an IR signal?

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

/*-----( Declare User-written Functions )-----*/
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 > 1.00) {
  digitalWrite(RELAY, HIGH);
  Serial.println(" RELAY ON");
  delay(10000); 
  digitalWrite(RELAY, LOW);
  Serial.println(" RELAY OFF");
    digitalWrite(LEDACT, LOW);
  } 


    break;

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

  }

} //END translateIR

This is the case I am talking about:

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 > 1.00) {
  digitalWrite(RELAY, HIGH);
  Serial.println(" RELAY ON");
  delay(10000);
  digitalWrite(RELAY, LOW);
  Serial.println(" RELAY OFF");
    digitalWrite(LEDACT, LOW);
  }


    break;

First, detect a change of state of the IR input. See the state change detection tutorial.

Receive any incoming data from the BT module. See the serial input basics tutorial.

Make a boolean variable "ledState".

If the switch has changed state toggle the state of ledState (if it is on turn it off and off if it's on).

ledState= !ledState;

If the proper command has come from BT, toggle the state of ledState

digital write ledState to the LED pin.