How to wait for IR remote signal and then execute a code if no signal received?

Here's what I want to do:

  1. Wait for a particular IR remote signal (e.g.- button 5 on remote)
  2. If no signal is detected within 60s, turn on LED for 30s, then turn everything off (if that's possible in arduino)

This is the code I have now. It turns on pin 9 when I press "5" on my remote.

/*
 Some Sample code of how to use your IR remote
 
 * Lets get started:
 
 The IR sensor's pins are attached to Arduino as so:
 Pin 1 to Vout (pin 11 on Arduino)
 Pin 2 to GND
 Pin 3 to Vcc (+5v from Arduino)

*/

#include <IRremote.h>

int IRpin = 11;  // pin for the IR sensor
int LED = 9;    // LED pin  
IRrecv irrecv(IRpin);
decode_results results;

boolean LEDon = false; // initializing LEDon as true

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED, OUTPUT);
}

void loop() 
{
   
  if (irrecv.decode(&results)) 
    {
      
      irrecv.resume();   // Receive the next value
    }
  
   if (results.value == 0xFF38C7)  // change zero to your IR remote button number
     {
       if (LEDon == false)   // is LEDon equal to true? 
         {
           LEDon = true;   
           digitalWrite(LED, HIGH);
           delay(100);      // keeps the transistion smooth
            
         }
         
        else
          {
            LEDon = false;
            digitalWrite(LED, LOW);
            delay(100);
            
          }
          
     }

}

My goal is to start the code in case IR remote is out of range. Can't think of an algorithm for that without thinking about running something in parallel. So, it's like this: we got IR signal? Great! Let remote control take over. No IR signal within 60s? Time to run that contingency code.

  1. Wait for a particular IR remote signal

Wait starting when?

  1. If no signal is detected within 60s

Within 60 seconds of what? The big bang? Oops, too late for that.

At the start of the timing interval you need to save the value of millis() - for example

startMillis = millis();

Then you need to check regularly if the interval has elapsed

if (millis() - startMillis >= interval) {
   // timeout period has expired
}

If you want the receipt of a particular IR code to restart the timing interval then you need code like

  if (results.value == 0xFF38C7)  // change zero to your IR remote button number
     {
            startMillis = millis();
            // your other stuff

...R

Robin2:
At the start of the timing interval you need to save the value of millis() - for example

startMillis = millis();

Then you need to check regularly if the interval has elapsed

if (millis() - startMillis >= interval) {

// timeout period has expired
}




If you want the receipt of a particular IR code to restart the timing interval then you need code like


if (results.value == 0xFF38C7)  // change zero to your IR remote button number
    {
           startMillis = millis();
           // your other stuff




...R
My goal is to start the code in case IR remote is out of range. Can't think of an algorithm for that without thinking about running something in parallel.

So, it's like this: we got IR signal? Great! Let remote control take over. No IR signal within 60s? Time to run that contingency code.

srk999:
So, it's like this: we got IR signal? Great! Let remote control take over. No IR signal within 60s? Time to run that contingency code.

Yes.

...R

My goal is to start the code in case IR remote is out of range.

How will you distinguish between "out of range" and "no button being pressed"?

If I get your car running in a circle, slowly, I don't want you stopping it because I've waited more than 60 seconds before getting bored.