independent on and off timer

Hello
im working on a project consisting of a motion sensor, which once it detects motion a relay will be on and in return an led will be on. everything works fine, but all is left is to measure the on/off time when led is off and when led is on and display it on an LCD. Im using miilis() but its not resetting the time continues to count.
is there any way to measure time for each case (ON/OFF) separately using millis() or any other method?

#include <LiquidCrystal.h> // include the library code:

int sensor_pin = 8; // Initialized the pin for motion sensor
//int relay_pin = 9;  // Initialized the pin for relay module
int output;   // Variable to store the output state of motion sensor

// On and Off Times (as int, max=32secs)
const unsigned int onTime = 1000;
const unsigned int offTime = 500;

// Interval is how long we wait
unsigned long interval = onTime;

// Tracks the last time event fired
unsigned long previousMillis=0;

// Used to track if relay should be on or off
boolean relay_pin = true;
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins

void setup(){
  lcd.begin(16, 2); // set up the LCD's number of columns and rows
  
  Serial.begin(9600);
  pinMode(sensor_pin, INPUT);   // Declared motion sensor pin as Input
  pinMode(relay_pin, OUTPUT);   // Declared relay module pin as Output pin
  // Make the relay module initial state as low, Relay works opposite
    pinMode(13, OUTPUT);         //Set Pin13 as output
  digitalWrite(13, HIGH);  
  digitalWrite(relay_pin, LOW); 
 // set the cursor to column 0, line 1
  
}
void loop(){
  // Read the output state of motion sensor
  output = digitalRead(sensor_pin);
  
  // If output is High
  if(output == 1){
    digitalWrite(relay_pin, HIGH);
      lcd.setCursor(0, 0);
    lcd.print("LIGHT IS ONN"); // Print a message to the LCD.
      

  // print the number of seconds since reset:
  
  //lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  //lcd.print( millis() / 1000);
  
  }
  
  // If output is LOW
  else
  {
    digitalWrite(relay_pin, LOW);
      lcd.setCursor(0, 0);
    lcd.print("LIGHT IS OFF"); // Print a message to the LCD.
    

  // print the number of seconds since reset:
  
     //lcd.setCursor(0, 1); 

  // print the number of seconds since reset:
  //lcd.print(millis() / 1000);
   
  }
    
  Serial.println(output);
  delay(50);
  digitalWrite(13, relay_pin);
  // Grab snapshot of current time, this keeps all timing
  // consistent, regardless of how much code is inside the next if-statement
  //unsigned long currentMillis = millis();
 
  // Grab snapshot of current time, this keeps all timing
 long currentMillis = millis();

  // Compare to previous capture to see if enough time has passed
  if ((unsigned long)(currentMillis - previousMillis) >= interval)
  {
    // Change wait interval, based on current relay state
    if (relay_pin) 
    {
      // Relay is currently on, set time to stay off
      interval = offTime;
      // print the number of seconds since reset:
     lcd.setCursor(0, 1); 
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
    } 
    else 
    {
      // Relay is currently off, set time to stay on
      interval = onTime;
      // print the number of seconds since reset:
     lcd.setCursor(0, 1); 
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
    }
    // Toggle the relay's state
    relay_pin = !(relay_pin);
    // Save the current time to compare "later"
    previousMillis = currentMillis;
  }
}

Let's see your current code

Yes. When something happens remember the millis() in a "started" variable. When it stops happening remember the millis() in a "stopped" variable. Subtract one from the other. That's the time the something happened for.

If you need more detail about where/how to do that you'll have to post your code.

Steve

The beginner's guide to millis() might help.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

...R

UKHeliBob:
Let's see your current code

#include <LiquidCrystal.h> // include the library code:

int sensor_pin = 8; // Initialized the pin for motion sensor
//int relay_pin = 9;  // Initialized the pin for relay module
int output;   // Variable to store the output state of motion sensor

// On and Off Times (as int, max=32secs)
const unsigned int onTime = 1000;
const unsigned int offTime = 500;

// Interval is how long we wait
unsigned long interval = onTime;

// Tracks the last time event fired
unsigned long previousMillis=0;

// Used to track if relay should be on or off
boolean relay_pin = true;
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins

void setup(){
  lcd.begin(16, 2); // set up the LCD's number of columns and rows
  
  Serial.begin(9600);
  pinMode(sensor_pin, INPUT);   // Declared motion sensor pin as Input
  pinMode(relay_pin, OUTPUT);   // Declared relay module pin as Output pin
  // Make the relay module initial state as low, Relay works opposite
    pinMode(13, OUTPUT);         //Set Pin13 as output
  digitalWrite(13, HIGH);  
  digitalWrite(relay_pin, LOW); 
 // set the cursor to column 0, line 1
  
}
void loop(){
  // Read the output state of motion sensor
  output = digitalRead(sensor_pin);
  
  // If output is High
  if(output == 1){
    digitalWrite(relay_pin, HIGH);
      lcd.setCursor(0, 0);
    lcd.print("LIGHT IS ONN"); // Print a message to the LCD.
      

  // print the number of seconds since reset:
  
  //lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  //lcd.print( millis() / 1000);
  
  }
  
  // If output is LOW
  else
  {
    digitalWrite(relay_pin, LOW);
      lcd.setCursor(0, 0);
    lcd.print("LIGHT IS OFF"); // Print a message to the LCD.
    

  // print the number of seconds since reset:
  
     //lcd.setCursor(0, 1); 

  // print the number of seconds since reset:
  //lcd.print(millis() / 1000);
   
  }
    
  Serial.println(output);
  delay(50);
  digitalWrite(13, relay_pin);
  // Grab snapshot of current time, this keeps all timing
  // consistent, regardless of how much code is inside the next if-statement
  //unsigned long currentMillis = millis();
 
  // Grab snapshot of current time, this keeps all timing
 long currentMillis = millis();

  // Compare to previous capture to see if enough time has passed
  if ((unsigned long)(currentMillis - previousMillis) >= interval)
  {
    // Change wait interval, based on current relay state
    if (relay_pin) 
    {
      // Relay is currently on, set time to stay off
      interval = offTime;
      // print the number of seconds since reset:
     lcd.setCursor(0, 1); 
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
    } 
    else 
    {
      // Relay is currently off, set time to stay on
      interval = onTime;
      // print the number of seconds since reset:
     lcd.setCursor(0, 1); 
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
    }
    // Toggle the relay's state
    relay_pin = !(relay_pin);
    // Save the current time to compare "later"
    previousMillis = currentMillis;
  }
}

slipstick:
Yes. When something happens remember the millis() in a "started" variable. When it stops happening remember the millis() in a "stopped" variable. Subtract one from the other. That's the time the something happened for.

If you need more detail about where/how to do that you'll have to post your code.

Steve

#include <LiquidCrystal.h> // include the library code:

int sensor_pin = 8; // Initialized the pin for motion sensor
//int relay_pin = 9;  // Initialized the pin for relay module
int output;   // Variable to store the output state of motion sensor

// On and Off Times (as int, max=32secs)
const unsigned int onTime = 1000;
const unsigned int offTime = 500;

// Interval is how long we wait
unsigned long interval = onTime;

// Tracks the last time event fired
unsigned long previousMillis=0;

// Used to track if relay should be on or off
boolean relay_pin = true;
 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins

void setup(){
  lcd.begin(16, 2); // set up the LCD's number of columns and rows
  
  Serial.begin(9600);
  pinMode(sensor_pin, INPUT);   // Declared motion sensor pin as Input
  pinMode(relay_pin, OUTPUT);   // Declared relay module pin as Output pin
  // Make the relay module initial state as low, Relay works opposite
    pinMode(13, OUTPUT);         //Set Pin13 as output
  digitalWrite(13, HIGH);  
  digitalWrite(relay_pin, LOW); 
 // set the cursor to column 0, line 1
  
}
void loop(){
  // Read the output state of motion sensor
  output = digitalRead(sensor_pin);
  
  // If output is High
  if(output == 1){
    digitalWrite(relay_pin, HIGH);
      lcd.setCursor(0, 0);
    lcd.print("LIGHT IS ONN"); // Print a message to the LCD.
      

  // print the number of seconds since reset:
  
  //lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  //lcd.print( millis() / 1000);
  
  }
  
  // If output is LOW
  else
  {
    digitalWrite(relay_pin, LOW);
      lcd.setCursor(0, 0);
    lcd.print("LIGHT IS OFF"); // Print a message to the LCD.
    

  // print the number of seconds since reset:
  
     //lcd.setCursor(0, 1); 

  // print the number of seconds since reset:
  //lcd.print(millis() / 1000);
   
  }
    
  Serial.println(output);
  delay(50);
  digitalWrite(13, relay_pin);
  // Grab snapshot of current time, this keeps all timing
  // consistent, regardless of how much code is inside the next if-statement
  //unsigned long currentMillis = millis();
 
  // Grab snapshot of current time, this keeps all timing
 long currentMillis = millis();

  // Compare to previous capture to see if enough time has passed
  if ((unsigned long)(currentMillis - previousMillis) >= interval)
  {
    // Change wait interval, based on current relay state
    if (relay_pin) 
    {
      // Relay is currently on, set time to stay off
      interval = offTime;
      // print the number of seconds since reset:
     lcd.setCursor(0, 1); 
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
    } 
    else 
    {
      // Relay is currently off, set time to stay on
      interval = onTime;
      // print the number of seconds since reset:
     lcd.setCursor(0, 1); 
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
    }
    // Toggle the relay's state
    relay_pin = !(relay_pin);
    // Save the current time to compare "later"
    previousMillis = currentMillis;
  }
}

You appear to have understood how to set a timer and test it (if the code is your own work) :

. . . 
// Tracks the last time event fired
unsigned long previousMillis=0;
. . .

if ((unsigned long)(currentMillis - previousMillis) >= interval)
  { . . . }

It looks like you simply have to create a one or two more more.