Hello,
Thank you for helping me:
I would like that when we press the push button First Presss (buttonPushCounter % 2 == 1) we turn on the arduino, if we press it again Second Press (buttonPushCounter % 2 == 0) we turn off the ardhuino.
If you press again (buttonPushCounter % 2 == 1) and it will start again with the void Setup. This will reset all values to 0, Serial.println (Starting the program: Hello ...) and set all variables to default.
To summarize I would like to transform the push button into the start button of the device.
Here is the code:
Thanks
// Library :
// 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 = 7; // the pin that the LED is attached to
const int ledPindoor = 8; //
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int buttonPushCounter = 0; // counter for the number of button presses
int doorState = 0; // current state of the Door A
int lastdoorState = 0; // previous state of the Door A
// Function Setup
int Operation = 0; //
int DOCounter = 0; // counter for the number of Door Opened
unsigned long onTime = 0;
unsigned long SonTime = 0;
unsigned long offTime = 0;
unsigned long temps = 0; // Duration of the operation
unsigned long Stemps = 0; // Duration of the operation in second
unsigned long Ctemps = 0;
unsigned long SCtemps = 0;
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;
float fCtemps = 0;
float fDOTotaltime =0;
bool singleAction1 = false;
bool singleAction2 = false;
bool singleAction3 = false;
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(" ");
} //void setup
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// 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)
// First Press ( 0N )
if (buttonPushCounter % 2 == 1) {
//turn LED BUTTON ON:
digitalWrite(ledPinbutton, HIGH);
if (! singleAction1) {
onTime = millis();
SonTime = 0.001 * onTime;
singleAction1 = true;
}
// DOOR ***************************************
// read the pushbutton input pin:
doorState = digitalRead(doorPin);
// compare the DoorState to its previous state
if (doorState != lastdoorState) {
if (doorState == LOW) {// if the door A is Open
// turn LED DOOR A ON:
digitalWrite(ledPindoor, HIGH);
//
DOCounter++;
DtimeO = millis();
SDtimeO = 0.001 * DtimeO;// Expression of time in seconds
} //if (doorState == LOW)
else { // door A is closed
// turn LED DOOR OFF:
digitalWrite(ledPindoor, LOW);
//
DtimeC = millis();
SDtimeC = 0.001 * DtimeC;// Expression of time in seconds
//
DOtime = DtimeC - DtimeO;
SDOtime = 0.001 * DOtime; // Expression of time in seconds
//
Ctemps = DtimeC - onTime;
SCtemps = 0.001 * Ctemps;
//
DOTotaltime = DOTotaltime + DOtime;
SDOTotaltime = 0.001 * DOTotaltime; // Expression of time in seconds
fCtemps = float(Ctemps);
fDOTotaltime = float (DOTotaltime);
DOpercent = fDOTotaltime/fCtemps;
} //else
}//if (doorState != lastdoorState)
// save the current state as the last state, for next time through the loop
lastdoorState = doorState;
// DOOR ****************************************
} // if (buttonPushCounter % 2 == 1)
// Second Press ( OFF )
if (buttonPushCounter % 2 == 0) {
// turn LED BUTTON OFF:
digitalWrite(ledPinbutton, LOW);
digitalWrite(ledPindoor, LOW);
singleAction1 = false;
DOCounter = 0;
Ctemps = 0;
DOtime = 0; // Door Previous opening time
// Display Function Loop Turn off device
} //(buttonPushCounter % 2 == 0)
delay(10);
} //void loop
Wiring in attachment.
Thanks a lot.
