How to read the duration of the on time from a relay module?

Hi,
Is there any function to read the time duration of Relay when its HIGH?

For example :

  1. Relay is HIGH for 30 second.
  2. The value of the 30 second time is entered into variable "relaytime"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define trig 5
#define echo 4
Servo servo;
const int sensor = 3; 
const int Relay = 6;
int state;
int value; 
long duration;
int distance;
void setup() {
  lcd.begin();
  lcd.print("Wash your hands");b
  lcd.setCursor(0,1);
  lcd.print("before you enter");
  servo.attach(9); // The servo motor is connected to D9
  pinMode(trig, OUTPUT); // Set the trigger pin as OUTPUT
  pinMode(echo, INPUT); // Set the echo pin as INPUT
  pinMode(sensor, INPUT); // Configure the pin of the IR sensor as INPUT
  pinMode(Relay, OUTPUT); // Configure the pin of the relay module as OUTPUT
  Serial.begin(9600); // Set baud rate as 9600
}
void loop() {
  digitalWrite(trig, LOW); 
  delayMicroseconds(5);
  digitalWrite(trig, HIGH); // Set the trigger pin HIGH to send the ultrasonic wave (pulse)
  delayMicroseconds(10);
  digitalWrite(trig, LOW); 
  value=digitalRead(sensor); // Read the digital signal sent by the IR sensor and store it in the 'value' variable
  duration = pulseIn(echo, HIGH); // Calculate time taken (in microseconds) for the pulse emitted by the trigger pin to reach the echo pin.
  distance = (duration/2) * (331.3/10000); // Calculate the distance from the sensor to the obstacle in cm, using the speed of sound in air(m/s) and the time taken (stored in duration variable)
  Serial.println(distance);
  if(distance>1 && distance<15){ // If you place your hands within 15 cm
    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("Hands detected");
    lcd.setCursor(0,1);
    lcd.print("Wash hands - 20s");
    digitalWrite(Relay, HIGH); //Turns on the submersible water pump or solenoid water valve
    state=1; // Assign state variable to 1
    delay(1000); // Delay period of 1 second must be used to prevent clicking of the relay module
    lcd.clear();
    lcd.print("You are safe now");
  }else{
    digitalWrite(Relay, LOW); //Turns off the submersible water pump or solenoid water valve
   }
   if((state==1)&&(value==LOW)){ // If you place your hands in front of the IR sensor after washing your hands
    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("You may go in");
    lcd.setCursor(1,1);
    lcd.print("10 seconds left");
    servo.write(90);
    delay(10000); // The door will be opened for 10 seconds
    servo.write(0);
    lcd.clear();
    lcd.print("Wash your hands");
    lcd.setCursor(0,1);
    lcd.print("before you enter");
    state=0;
   } else if((state==0)&&(value==LOW)){ // If you do not wash your hands before you enter
    lcd.clear();
    lcd.print("Wash your hands");
    lcd.setCursor(0,1);
    lcd.print("to grant access");
   }  
}

How can you access the relay module?

Access with Arduino Uno

:crazy_face:
:roll_eyes:

1 Like

We can help you after you have written the code yourself. Realize your CODE will tell the relay module to either turn on or turn off and you can compute the time it is on.
How do you propose the 30 second time will be entered?
Will the relay ever turn off?
Some things to consider in your code.

:rofl:
:rofl:

@rxrad doesn’t seem to have much self-confidence…
posts a question, then deletes his own evolving thoughts.

It will get better, I promise.

Just, as you get used to the forum, it's polite etiquette to post the solution, and then 'solve'or close the thread.

Newbies can learn from your experiences, and it will help other members from wasting their time looking at
Screenshot 2022-03-04 213624

I take back my words , not solved.
Please admin delete this post ASAP :pray:.
I have been flagged this post as inappropriate.

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