Help me with millis function, I want to replace delay functionin the code i have

i have arduino for timer,

i use infrared remote to send signal to my arduino, when signal received arduino send LOW signal to relay and turning on the relay output.

i put 3 hours delay on it with delay function, once the delay started. i can't remote my other perhiperals that is also controlled with my arduino.

in order to make it work, i wan't to replace the delay funcion with millis function but i don't know how to do it. please help me.

the original code is like this:

#include <IRremote.h>
int before1;
int RECV_PIN = 2;
int out1 = 3;
const unsigned long SECOND = 1000;
const unsigned long HOUR = 3600*SECOND;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  irrecv.enableIRIn();      // Start the receiver
  before1=0;
  pinMode(out1,OUTPUT);
  digitalWrite(out1, HIGH); // off by default, i'am using relay.
}

void loop() {
  if (irrecv.decode(&results)) {
  if (results.value==0x5E7DB3DC){ // infrared remote input
    if (before1==0) {
  digitalWrite(out1, LOW);  // relay on
  before1=1;
  delay(5*SECOND);          // i change to 5 second for testing only, and it works. usually i use 3 hour.
  digitalWrite(out1, HIGH); // relay off
  before1=0;
    }
    else{
      digitalWrite(out1, HIGH);
      before1=0;
    }}
      irrecv.resume();

 }
  
  }
 

thanks in advance

Strange. You alter 'before1' and don't use it for anything before you assign a new value to it.

What does 'before1' mean? You should use variable names that express the purpose of the variable.

Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

Thank you for posting code with code tags. Please use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

It looks like what you want is:

if (results.value == 0x5E7DB3DC)
   if (the relay is off)
      Turn on the relay for 3 hours.
   else
       Turn off the relay.
#include <IRremote.h>
boolean RelayIsOff = true;
unsigned long RelayStartTime;

const int RECV_PIN = 2;
const int RelayPin = 3;
const unsigned long SECOND = 1000;
const unsigned long MINUTE = 60 * SECOND;
const unsigned long HOUR = 60 * MINUTE;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  irrecv.enableIRIn();      // Start the receiver
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, HIGH); // off by default, i'am using relay.
}

void loop()
{
  // If the relay is on, turn it off after 3 hours
  if (!RelayIsOff && millis() - RelayStartTime > 3 * HOUR)
  {
    digitalWrite(RelayPin, HIGH); // relay off
    RelayIsOff = true;
  }
  
  if (irrecv.decode(&results))
  {
    if (results.value == 0x5E7DB3DC) // infrared remote input
    {
      if (RelayIsOff)
      {
        digitalWrite(RelayPin, LOW);  // relay on
        RelayIsOff = false;
        RelayStartTime = millis();
      }
      else
      {
        digitalWrite(RelayPin, HIGH); // relay off
        RelayIsOff = true;
      }
    }
    irrecv.resume();
  }
}
2 Likes

Yes, your 'before1' is a "canard". Throw it out!

#include <IRremote.h>
int RECV_PIN = 2;
int out1 = 3;
const unsigned long SECOND = 1000;
const unsigned long HOUR = 3600 * SECOND;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  irrecv.enableIRIn();      // Start the receiver
  pinMode(out1, OUTPUT);
  digitalWrite(out1, HIGH); // off by default, i'am using relay.
}

void loop() {
  if (irrecv.decode(&results)) {
    if (results.value == 0x5E7DB3DC) { // infrared remote input
      digitalWrite(out1, LOW);  // relay on
      delay(5 * SECOND);        // i change to 5 second for testing only, and it works. usually i use 3 hour.
      digitalWrite(out1, HIGH); // relay off
    }
    else {
      digitalWrite(out1, HIGH);
    }
    irrecv.resume();
  }
}

Consider improving the names you are using. John has done so somewhat using RelayPin rather than out1, but since you know what the relay is controlling you can do better.

This will help keep your code clearer as you add your other peripherals.

thank you very much, it is working perfectly.
and wow, your code is way better in every way.

i'am amazed.

thanks again.

All it takes is decades of practice. :slight_smile:

3 Likes