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
#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;
}
}
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
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 ?
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.
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