IR & Timer

please help:

I want to press IR button to start the timer of a project. Let the project start working for 1 hour then stop. But the program I write below can not do what I want it to do . I press the IR button but the timer do nothig. Is there something wrong?

unsigned long HR;
unsigned long MILLIS;
const long timingH1 = 5000;
const byte sw = 13;
#include <IRremote.h>
const byte RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
HR = millis();
irrecv.enableIRIn();
pinMode(sw,OUTPUT);
pinMode(sw,HIGH);
digitalWrite(sw,LOW);
Serial.begin(9600);
}
void loop() {
if (irrecv.decode(&results)) {
if (results.value == 16724175){
MILLIS = millis();
Serial.print(“HR:”);
Serial.println(HR);
Serial.print(“MILLIS:”);
Serial.println(MILLIS);
if (MILLIS - HR >= timingH1){
HR = MILLIS;
digitalWrite(sw,HIGH);
}}
irrecv.resume();
}
}

Your mistake is that you only check the timer once when the timer is started and you don’t check it again until the button is pressed again. You have to check the timer frequently, like every time through loop(), when the timer is running. One way to keep track of if the timer is running or not is to use 0 for the timer start time if the timer is not running.

unsigned long HR;
unsigned long MILLIS;
const unsigned long timingH1 = 5000;  // Five seconds
const byte  sw = 13;
#include <IRremote.h>
const byte RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup()
{
  Serial.begin(115200);


  irrecv.enableIRIn();


  pinMode(sw, OUTPUT);
  HR = 0; // Timer is not running
  digitalWrite(sw, HIGH);
}


void loop()
{
  MILLIS = millis();


  if (HR != 0 && MILLIS - HR  >= timingH1)
  {
    // Timer has completed
    HR = 0;  // Stop the timer
    digitalWrite(sw, HIGH);
  }


  if (irrecv.decode(&results))
  {
    if (results.value == 16724175)
    {
      Serial.print("HR:");
      Serial.println(HR);
      Serial.print("MILLIS:");
      Serial.println(MILLIS);


      digitalWrite(sw, LOW);
      HR = MILLIS;  // Start the timer
    }
    irrecv.resume();
  }
}

Thank you very much. You answered my question and showed me the right coding. After a few days of thinking what you meant ,I finally understood. I used your program on my board and it worked! Thanks a lot.