Good morning,
I'm having trouble with the Arduino code:
I would like that after pressing a second time...
all the storytellers reset themselves ( DOCounter ; Dtemps ; SDtemps ; DOtime ; SDOtime ; DOTotaltime ; SDOTotaltime ; DOpercent ; )
and also we add 1 in the counter (Operation).
At the moment only (DOCounter) is reset.
The other counters don't reset. And there is no +1 added to (Operation)
Thank you very much for helping me if possible.
Wiring and Result in attachment.
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int doorPin = 3; // the pin that the pushbutton is attached to
const int ledPinbutton = 13; // the pin that the LED is attached to
const int ledPindoor = 12; //
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int DeviceState = 0; // current state of DEVICE
int buttonPushCounter = 0; // counter for the number of button presses
unsigned int DOCounter = 0; // counter for the number of Door Opened
unsigned int Operation = 0; //
int doorState = 0; // current state of the Door
int lastdoorState = 0; // previous state of the Door
unsigned long Dtemps = 0; // Duration of the operation
unsigned long SDtemps = 0; // Duration of the operation in second
unsigned long DtimeO = 0; // Exact time the door is opened
unsigned long SDtimeO = 0; // Exact time the door is opened in seconds
unsigned long DtimeC = 0; // Exact time the door is closed
unsigned long SDtimeC = 0; // Exact time the door is Closed in seconds
unsigned long DOtime = 0; // Door Previous opening time
unsigned long SDOtime = 0; // Door Previous opening time in seconds
unsigned long DOTotaltime = 0; // Cumulative time of all Door openings
unsigned long SDOTotaltime = 0; // Cumulative time of all Door openings in seconds
float DOpercent = 0; // Percentage of door opening time
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the door pin as a input:
pinMode(doorPin, INPUT);
// initialize the LED as an output:
pinMode(ledPinbutton, OUTPUT);
// initialize the LED as an output:
pinMode(ledPindoor, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
Serial.println( " Starting the program: Hello ... ");
Serial.println(" ");
Serial.println("*****************************************************************************");
Serial.println(" Ope | Opn | D. O .T. | D. C. T. | D. O. | C. D. O. | D. S. O. | % ");
Serial.println("-----------------------------------------------------------------------------");
} //void setup
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++;
} //if (buttonState == HIGH)
} //if (buttonState != lastButtonState)
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// First Press
if (buttonPushCounter % 2 == 1) {
//turn LED BUTTON ON:
digitalWrite(ledPinbutton, HIGH);
Dtemps = millis(); // Exact time the operation began
// read the pushbutton input pin:
doorState = digitalRead(doorPin);
// compare the DoorState to its previous state
if (doorState != lastdoorState) {
// if the state has changed, increment the counter
if (doorState == LOW) {// if the door is Open
DOCounter++; // Adds 1 to the counter
// turn LED DOOR ON:
digitalWrite(ledPindoor, HIGH);
DtimeO = millis(); // Exact time the door is opened
} //if (doorState == LOW)
else { // door is closed
// turn LED DOOR OFF:
digitalWrite(ledPindoor, LOW);
// if the current state is LOW then the button went from on to off:
SDtemps = 0.001 * Dtemps; // Expression of time in seconds
SDtimeO = 0.001 * DtimeO; // Expression of time in seconds
DtimeC = millis(); // Exact time the door is closed
SDtimeC = 0.001 * DtimeC;// Expression of time in seconds
DOtime = DtimeC - DtimeO; // Calculation of door opening time
SDOtime = 0.001 * DOtime; // Expression of time in seconds
DOTotaltime = DOTotaltime + DOtime; // Calculation of the total duration of all door openings
SDOTotaltime = 0.001 * DOTotaltime; // Expression of time in seconds
float fDtimeC = (float)DtimeC; // Conversion to Float
float fDOTotaltime = (float)DOTotaltime; // Conversion to Float
DOpercent = 100 * (fDOTotaltime / fDtimeC); // percentage calculation
// Display values
// Ope:
if (Operation < 10){Serial.print(" ");}
else if (Operation < 100){Serial.print(" ");}
else if (Operation < 1000){Serial.print(" ");}
Serial.print(Operation);
Serial.print(" |");
// Opn:
if (DOCounter < 10){Serial.print(" ");}
else if (DOCounter < 100){Serial.print(" ");}
else if (DOCounter < 1000){Serial.print(" ");}
Serial.print(DOCounter);
Serial.print(" | ");
// D. O. T.
if ( SDtimeO < 10){ Serial.print(" ");}
else if ( SDtimeO < 100){ Serial.print(" ");}
else if ( SDtimeO < 1000){ Serial.print(" ");}
else if ( SDtimeO < 10000){ Serial.print(" ");}
Serial.print(SDtimeO);
Serial.print(" | ");
// D. C. T.
if ( SDtimeC < 10){ Serial.print(" ");}
else if ( SDtimeC < 100){ Serial.print(" ");}
else if ( SDtimeC < 1000){ Serial.print(" ");}
else if ( SDtimeC < 10000){ Serial.print(" ");}
Serial.print(SDtimeC);
Serial.print(" | ");
// D. O.
if ( SDOtime < 10){ Serial.print(" ");}
else if ( SDOtime < 100){ Serial.print(" ");}
else if ( SDOtime < 1000){ Serial.print(" ");}
else if ( SDOtime < 10000){ Serial.print(" ");}
Serial.print(SDOtime);
Serial.print(" | ");
// C. D. O.
if ( SDOTotaltime < 10){ Serial.print(" ");}
else if ( SDOTotaltime < 100){ Serial.print(" ");}
else if ( SDOTotaltime < 1000){ Serial.print(" ");}
else if ( SDOTotaltime < 10000){ Serial.print(" ");}
Serial.print(SDOTotaltime);
Serial.print(" | ");
// D. S. O.
if ( SDtemps < 10){ Serial.print(" ");}
else if ( SDtemps < 100){ Serial.print(" ");}
else if ( SDtemps < 1000){ Serial.print(" ");}
else if ( SDtemps < 10000){ Serial.print(" ");}
Serial.print(SDtemps);
Serial.print(" | ");
// %
if ( DOpercent < 10){ Serial.print(" ");}
else if ( DOpercent < 100){ Serial.print(" ");}
Serial.println(DOpercent);
Serial.println("-----------------------------------------------------------------------------");
} //else
} //if (doorState != lastdoorState)
// save the current state as the last state, for next time through the loop
lastdoorState = doorState;
} // if (buttonPushCounter % 2 == 1)
// Second Press
if (buttonPushCounter % 2 == 0) {
// turn LED BUTTON OFF:
digitalWrite(ledPinbutton, LOW);
// turn LED DOOR OFF:
digitalWrite(ledPindoor, LOW);
//
Operation = Operation++ ; // Adds 1
DOCounter = 0;
Dtemps = 0;
SDtemps = 0;
DOtime = 0;
SDOtime = 0;
DOTotaltime = 0;
SDOTotaltime = 0;
DOpercent = 0;
} //(buttonPushCounter % 2 == 0)
delay(10);
} //void loop



