measuring LED ON/OFF time using millis

Hello

Im trying to measure LED ON/OFF time using millis to display the time on LCD, the project idea is to use an IR sensor to detect motion, if there is motion then LED is on, otherwise it's off. I want to measure the time duration for on and off apart. But the timer keeps counting without resetting. how to make it reset? please help, thanks in advance<3.

this is my code:

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

[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

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);
}

Do you mean the millis() counter is not resetting? How about storing it's value and building the difference?

uint32_t mytimestamp = millis();
// do something
uint32_t used_millis = millis() - mytimestamp;

Yes!, i tried the code u posted, still it's not resetting :frowning:

ToxicBullet:
Yes!, i tried the code u posted, still it's not resetting :frowning:

You will have to show your new code so we can see what you did.

Okayy i've modified my code to this, replaced lcd.print( millis() / 1000) to lcd.print( used_millis / 1000)
sorry but i might be doing something wrong, cuz im a beginner

#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
  uint32_t mytimestamp = millis();

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:



uint32_t used_millis = millis() - mytimestamp;

  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
    lcd.print( used_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:
  
 

uint32_t used_millis = millis() - mytimestamp;

     lcd.setCursor(0, 1); 

  // print the number of seconds since reset:
  lcd.print( used_millis / 1000);
   
  }
    
  Serial.println(output);
  delay(50);
}

When the light goes on, you have to update mytimestamp to the current timestamp (millis()).

oke i did this, when the light goes on, the timer on lcd resets to zero but stop counting, and when the light goes off the timer doesnt reset and continue counting

#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
  uint32_t mytimestamp = millis();

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:



 mytimestamp = millis();
uint32_t used_millis = millis() - mytimestamp;

  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
    lcd.print( used_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:
  
 

uint32_t used_millis = millis() - mytimestamp;

     lcd.setCursor(0, 1); 

  // print the number of seconds since reset:
  lcd.print( millis() / 1000);
   
  }
    
  Serial.println(output);
  delay(50);
}
 mytimestamp = millis();
uint32_t used_millis = millis() - mytimestamp;

The value in used_millis will never be more than 2. It will almost always be 0.

What is the purpose of this?

You REALLY need to look at the state change detection example. You want to record, it seems to me, to record when the switch BECOMES pressed (or released or both), NOT when the switch IS pressed (or IS released).

yes it gives zero and doesnt count again

Read what PaulS wrote, over and over, until the light bulb comes on.

okaay :grinning: