Good morning, sir:
I'd like to check the duration and number of time a door is opened with a limit sensor (Micro limit switch) and an Arduino Nano.

If the circuit is closed = Door Open ( condition 1) : The stopwatch starts.
Once the circuit is open = Door Closed ( condition 2) : The stopwatch stops.
and after recovering the following data:
- N: The number of times the door is opened.
- Tn: The duration of each door opening ... example :
.....
- Nth opening : 3 : Tn = 70 ms
- and finally The total duration of all door openings: Tt = (T1+T2+T3+ .....+ Tn)
Thank you so much for helping me with the code.

What have you tried so far ?
Can you read the state of the switch (open or closed) and display it on the Serial monitor ?
If not then look at the Button example in the IDE
Your duplicate topic has been deleted
Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting will result in a suspension from the forum.
In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
Thanks for the advice...
Actually I'm new on the forum and I don't know which topic to post the question on.
I've read "How to use this forum - please read" and understood... I'll delete the other post.
UKHeliBob:
Can you read the state of the switch (open or closed) and display it on the Serial monitor ?
If not then look at the Button example in the IDE
I'm trying but I can't... can you please send me a link that can help me view the Limit Swicth (Open or Closed)
Have you tried the Button example ?
Hi,
With one switch, you will be measuring the opening plus closing time, not just the "Duration of closure".
Can you post the code you have?
How have you got your switch wired?
Thanks.. Tom... 
UKHeliBob:
What have you tried so far ?
Can you read the state of the switch (open or closed) and display it on the Serial monitor ?
If not then look at the Button example in the IDE
I managed to display the status of with Button "Open-Closed" here is the code:
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// We use the computer to read what the Arduino card says.
Serial.begin (9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
//Program Start Phrase
Serial.println( " Demarage du programme : Bonjour ");
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// delais de reperage
delay(1000);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
//Display on screen "Door Open"
Serial.print(" Door Open ");
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
//Display on screen Door Closed
Serial.print(" Door Closed ");
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Now I have to display it:
- The number of openings
- Start the stopwatch as soon as it opens.
Thank you for help
I managed to display the status of with Button "Open-Closed"
OK so far but you really need to detect when the door becomes open, ie was closed and is now open, rather than when it is open. If you can do that then you will be able to add 1 to a counter and save the time it became open.
Take a look at the StateChangeDetection example in the IDE to see the principle in action
Read the door state. If it is now open and last time we looked it was closed then it has become open so add 1 to our count variable and print it, save the value of millis() in an unsigned long variable as the start time and save the current door state as the previous door state
Later in loop() detect whether the door has become closed and if so save the value of millis() in another unsigned long variable as the end time. Subtract the start time from the end time and you have the duration that the door was open in milliseconds to do anything that you want with
TomGeorge:
Can you post the code you have?
How have you got your switch wired?
I tried it with Button ... it displays the status (open - closed) ....
I still have to :
- The number of openings
- Start the stopwatch as soon as it opens.
Thank you for help
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// We use the computer to read what the Arduino card says.
Serial.begin (9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
//Program Start Phrase
Serial.println( " Demarage du programme : Bonjour ");
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// delais de reperage
delay(1000);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
//Display on screen "Door Open"
Serial.print(" Door Open ");
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
//Display on screen Door Closed
Serial.print(" Door Closed ");
// turn LED off:
digitalWrite(ledPin, LOW);
}
}




UKHeliBob:
OK so far but you really need to detect when the door becomes open, ie was closed and is now open, rather than when it is open. If you can do that then you will be able to add 1 to a counter and save the time it became open.
I'm going to detect when the door is open and count down the time the door was open.
In my sensor setup:
- Opened circuit = Button not pressed = Door closed.
- And closed circuit = Button pressed = Door open.
Thanks. I'll try.
mondoha:
I'm going to detect when the door is open and count down the time the door was open.
In my sensor setup:
- Opened circuit = Button not pressed = Door closed.
- And closed circuit = Button pressed = Door open.
Thanks. I'll try.
No, you need to;
Detect when the door opens to start the timer, other words detect the CHANGE in the input, not the STATE of the input.
Detect when the door closes to start stop the timer, other words detect the CHANGE in the input, not the STATE of the input.
In other words WHEN the switch opens and closes, no IF the switch is open or closed.
In post #9 @UKHeliBob has guided you to an example of doing this, it is an example in the IDE.
Tom.. 
UKHeliBob:
OK so far but you really need to detect when the door becomes open, ie was closed and is now open, rather than when it is open.
Do I modify the code like this ?
Thank you
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// We use the computer to read what the Arduino card says.
Serial.begin (9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
//Program Start Phrase
Serial.println( " Demarage du programme : Bonjour ");
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// delais de reperage
delay(1000);
// check if the pushbutton is not pressed. If it is, the buttonState is HIGH:
if (buttonState == LOW) { // Pushbutton is not pressed
//Display on screen "Door Close"
Serial.println(" Door Closed ");
// turn LED on:
digitalWrite(ledPin, LOW);
}
else { // Pushbutton is pressed
//Display on screen Door Closed
Serial.print(" Door OPEN ");
// turn LED off:
digitalWrite(ledPin, HIGH);
}
}
Do I modify the code like this
No
Did you look at the StateChangeDetection example in the IDE ?
Where are you saving the previous button state so that you can test whether it has changed ?
How are you planning to “Store” your door time values?
It is pretty trivial to simply count how many times the door changes state, but your going to need to create a array to index into memory.
If this was my project I would add a SD card writer and just append the door cycles to a single file.
What type of “door” is this that would open and close in 50-300ms?
UKHeliBob:
No
Did you look at the StateChangeDetection example in the IDE ?
Where are you saving the previous button state so that you can test whether it has changed ?
UKHeliBob:
No
Did you look at the StateChangeDetection example in the IDE ?
Where are you saving the previous button state so that you can test whether it has changed ?
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
//Program Start Phrase
Serial.println( " Demarage du programme : Bonjour ");
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("Door Closed ");
// turn LED OFF:
digitalWrite(ledPin, LOW);
}
else {
// if the current state is LOW then the button went from on to off:
Serial.print("The Door is Open For the ");
Serial.print(buttonPushCounter);
Serial.println("th Time");
// turn LED ON:
digitalWrite(ledPin, HIGH);
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
Yes, I looked... now I have the number counter. I still have time.
I know the duration of each opening and the total duration of all the openings.
Thank you very much, thank you very much.
Slumpert:
How are you planning to "Store" your door time values?
I think I'll use a real time clock module and an SD card for memory.
and output the files in csv format.
Slumpert:
What type of “door” is this that would open and close in 50-300ms?
It's just an example .... Maybe 5000 - 10000 - 54600 milliseconds.
thank you
Good morning,
Can you please help me with the code;
the Time of the opening preceding " dtemps " and the Cumulative Time of all openings " dtempsTotal " always show 0 milliseconds,
I think I made a mistake in the code. Thanks for helping me.
This is the code.
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
//Program Start Phrase
Serial.println( " Starting the program: Hello ");
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
//
unsigned long dtempsO = millis(); // Opening times
unsigned long dtempsC = millis(); // Closing times
unsigned long dtemps = 0; // Previous opening time
unsigned long dtempsTotal = 0; // Cumulative time of all openings
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
dtemps = dtempsO - dtempsC;
Serial.println("----------------------------------");
Serial.println(" Door Closed... ");
Serial.print(" Door closing time : ");
Serial.println(dtempsC);
Serial.print(" The last opening lasted ");
Serial.print(dtemps);
Serial.println(" Milliseconds");
Serial.print(" Cumulative opening time ");
Serial.print(dtempsTotal);
Serial.println(" Milliseconds");
// turn LED OFF:
digitalWrite(ledPin, LOW);
}
else {
// if the current state is LOW then the button went from on to off:
Serial.println("----------------------------------");
Serial.print(" Door open for the ");
Serial.print(buttonPushCounter);
Serial.println("th Time");
Serial.print(" Door opening time ");
Serial.println(dtempsO);
// turn LED ON:
digitalWrite(ledPin, HIGH);
}
// Delay a little bit to avoid bouncing
delay(10);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
dtempsTotal = dtempsTotal + dtemps;
}
unsigned long dtempsO = millis(); // Opening times
unsigned long dtempsC = millis(); // Closing times
unsigned long dtemps = 0; // Previous opening time
unsigned long dtempsTotal = 0; // Cumulative time of all openings
You are declaring these variables each time through loop() so no wonder dtemps and dtempsTotal are always zero. The same goes for dtempsO and dtemptsC except they will always be set to millis()
You are supposed to be saving the millis() value when the door opens and again when it shuts but you are not doing that