Delay on sensor and relay

Hello Fellas, how are you all? I hope we are always fine. Here, I want to ask about coding from Arduino. So I have a tool that uses 2 components namely infrared sensor and 1 channel relay. The way the tool works is when the sensor is on, the relay will turn on. But I want to give a delay for a few seconds to the infrared sensor. The infrared sensor must be on a few seconds first then relay 1 channel will turn on. I want to ask you guys about the problems that I am facing. Below I will attach my coding. Thank you for your attention :grin:

#include <EEPROMex.h>
#include <EEPROMVar.h>

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int ir = 9;                             //Sensor Infrared
const int relay = 8;                          //Relay 1 channel

int addressLong;

enum { ST_IDLE, ST_RDY, ST_ON, ST_OFF };
int  state;

unsigned long msecLst;
long OnTime;                                   //Timer

enum { Off = HIGH, On = LOW };


void setup(){
  lcd.begin ();
  Serial.begin(9600);
  
  pinMode(ir, INPUT);
  
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);

  EEPROM.readLong(0);
}

void loop (){

    EEPROM.readLong(0);
    unsigned long msec = millis ();
    
    switch (state)  {
    case ST_ON:
        if ( (msec - msecLst) > OnTime)  {
            state = ST_OFF;
            digitalWrite (relay, Off);
        }
        break;

    case ST_RDY:
        if (digitalRead(ir) == LOW)  {
            state   = ST_ON;
            msecLst = msec;
            digitalWrite (relay, On);
            lcd.clear();
            lcd.setCursor(1, 0);
            lcd.print("SEDANG MENGISI");
            lcd.setCursor(4, 1);
            lcd.print("BOTOL !!!");
            delay(OnTime);
            lcd.clear();
        }
        break;

    case ST_IDLE:
    default:
        if (digitalRead(ir) == LOW)
            state = ST_RDY;
        break;

     case ST_OFF:
        if (digitalRead(ir) == HIGH)
            state = ST_IDLE;
            digitalWrite (relay, Off);
        break;
    }
}
EEPROM.readLong(0);

Before I go any further, what does this line of code do ?

Oh it's only Eeprom as a timer storage. So actually my tool has several push buttons as a button to select a timer. But here I eliminate because I only need to ask about the infrared sensor delay. By the way, thank you for responding to my post :grin:

But what does the statement actually do and why do it every time through loop() ?

If you need to read or store variables of any type then the standard EEPROM library can handle it

So its just a useless code yeah? I'm sorry then, I just a beginner and still don't really understand about arduino

Instead of thinking "delay" for your code, think about ignoring the IR sensor for some period of time.

Those EEPROM statements don't do anything but read a value from the EEPROM and ignore it. The rest of the sketch may be fine but I was intrigued by the lines that I asked about

As to your original question, you say that the IR sensor must be on for a few seconds before the relay turns on. What should happen if the sensor stops sensing anything during the few seconds ? Should the relay still turn on and how long should it stay on for ?

Thank yous so much for your advice.

I've tried just using the function delay(2000); But, the result is that the relay will turn on when the sensor count has reached 2 seconds. However I want the sensor to be on for 2 seconds continuously for the relay to turn on. So it will be like this, the relay will turn on if the sensor is on for 2 seconds continuously. If the sensor is on for less than 2 seconds continuously, the relay will not turn on.

That is a better explanation of what you want

Take a deep breath and read this

The millis() function returns the number of milliseconds that have passed since the Arduino was powered up or reset so it can be used to time things. Unlike the delay() function millis() does not stop other things running when you call it. With that in mind here is a way to do what you want

  • when the sensor becomes triggered save the value of millis() as the start time
  • then, each time through loop() compare the current value of millis() with the saved start value
  • if the difference between them is 5000 milliseconds then the timing period has ended so turn on the relay

That is all well and good but suppose the sensor stops sensing an object during the timing period ?

One way round this is to use a boolean (true or false) variable to indicate whether timing is taking place or not. Set it to true when an object is detected and false when it is not. Only turn on the relay when the boolean is true

The result is that the relay is turned on based on 2 conditions. (a) the boolean is true, ie the sensor is currently active and (b) the timing period has ended

That is a lot to take in but there are examples and posts that might help. See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Allright, Thank you so much for yourexplanation. I really really really appreciated your answers.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.