How can I find amount of downtime was made in infrared sensor

Using this code I would like to find how long it was open and how long closed ;

#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int InfraredSensorPin =7;//Connect the signal pin to the digital pin 4
const int LedDisp = 13;
unsigned long interval = 0;

void setup() {
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
Serial.begin(57600);

Serial.println(“Start!”);
pinMode(InfraredSensorPin,INPUT);
pinMode(LedDisp,OUTPUT);
digitalWrite(LedDisp,LOW);
}

void loop()
{
if(digitalRead(InfraredSensorPin) == LOW) digitalWrite(LedDisp,HIGH);
else digitalWrite(LedDisp,LOW);
Serial.print(“Infrared Switch Status:”);
Serial.println(digitalRead(InfraredSensorPin),BIN);

lcd.print(“Infrared Switch Status:”);
lcd.println(digitalRead(InfraredSensorPin),BIN);
delay(50);
}

You'll need to have a variable storing the state of the pin, so you can compare that with the state as you read it at the top of loop(). From that you'll be able to see if the pin has just gone low or high in which case start a timer for the new state and end the timer for the old state which you would have started when the state last changed. If the state hasn't changed, then don't do anything, leave the timer running.

By timer I mean record millis() into a variable to "start" and subtract that from the millis() later on, to "stop".

Have a look here to see how to handle the state change.

I tried but the timer no stop

#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int InfraredSensorPin =7;//Connect the signal pin to the digital pin 4
const int LedDisp = 13;
unsigned long time;
void setup() {

  • // set up the LCD’s number of columns and rows:*
  • lcd.begin(16, 2);*
  • // Print a message to the LCD.*
  • Serial.begin(9600);*
  • Serial.println(“Start!”); *
  • pinMode(InfraredSensorPin,INPUT);*
  • pinMode(LedDisp,OUTPUT);*
  • digitalWrite(LedDisp,LOW);*
    }

void loop()
{

  • if(digitalRead(InfraredSensorPin) == LOW) digitalWrite(LedDisp,HIGH);*

  • else digitalWrite(LedDisp,LOW);*

  • Serial.print(“Infrared Switch Status:”);*

  • Serial.println(digitalRead(InfraredSensorPin),BIN);*

  • //lcd.print(“Infrared Switch Status:”);*
    // lcd.println(digitalRead(InfraredSensorPin),BIN);

  • Serial.print("Time: ");*

  • time = millis();*

  • //prints time since program started*

  • Serial.println(time);*

  • // wait a second so as not to send massive amounts of data*

  • delay(50);*

  • }*
    I am a novice assistance for the variable[/b]

with assistance FRIEND I did

const int InfraredSensorPin =8;//Connect the signal pin to the digital pin 4
const int LedDisp = 13;
unsigned long xronos_ON=0;
unsigned long xronos_OFF=0;
unsigned long xronos_NOW=0;
boolean ON = false;
boolean OFF= false;

void setup()
{
Serial.begin(9600);
pinMode(InfraredSensorPin,INPUT_PULLUP);
pinMode(LedDisp,OUTPUT);
digitalWrite(LedDisp,LOW);
}

void loop()
{
xronos_NOW=millis();
if(digitalRead(InfraredSensorPin) == LOW)
{
OFF=false;
if(ON == false)
{
Serial.print("Xronos_OFF itan = ");
Serial.print(xronos_NOW-xronos_OFF);
Serial.println(" mSec");
ON=true;
xronos_ON=millis();
}
digitalWrite(LedDisp,HIGH);
}
else
{
ON=false;
if(OFF == false)
{
Serial.print("Xronos_ON itan = ");
Serial.print(xronos_NOW-xronos_ON);
Serial.println(" mSec");
OFF=true;
xronos_OFF=millis();
}
digitalWrite(LedDisp,LOW);
}

delay(50);
}

And is that working for you, or are you asking a question?

yes it works

mSec fore sec

const int InfraredSensorPin =8;//Connect the signal pin to the digital pin 4
const int LedDisp = 13;
unsigned long xronos_ON=0;
unsigned long xronos_OFF=0;
unsigned long xronos_NOW=0;
float time_ON=0;
float time_OFF=0;
boolean ON = false;
boolean OFF= false;

void setup()
{
Serial.begin(9600);
pinMode(InfraredSensorPin,INPUT_PULLUP);
pinMode(LedDisp,OUTPUT);
digitalWrite(LedDisp,LOW);
}
void loop()
{
xronos_NOW=millis();
if(digitalRead(InfraredSensorPin) == LOW)
{
OFF=false;
if(ON == false)
{
time_OFF=(xronos_NOW-xronos_OFF)/1000.0;
Serial.print("Xronos_OFF itan = ");
Serial.print(time_OFF,1);
Serial.println(" Sec");
ON=true;
xronos_ON=millis();
}
digitalWrite(LedDisp,HIGH);
}
else
{
ON=false;
if(OFF == false)
{
time_ON=(xronos_NOW-xronos_ON)/1000.0;
Serial.print("Xronos_ON itan = ");
Serial.print(time_ON,1);
Serial.println(" Sec");
OFF=true;
xronos_OFF=millis();
}
digitalWrite(LedDisp,LOW);
}

delay(50);
}

And is that post a question or just an update?

For future reference if you use the </> icon after selecting text, you will get your code to

look like this
{
which is way easier
}
to read