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?
Here is my 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;
}
}