Make a push button as start button of the Arduino.

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.

(deleted)

Seriously, you would just create your own custom setup and loop, rename them my_setup() and my_loop(), for example, and apply the button logic to those

spycatcher2k:
Just fit a toggle type power button! Done. :slight_smile:

Thanks, but I wanted to use the Arduino's USB power supply.
That's why I'd like to have a switch that turns off and on the Arduino without disconnecting the usb cable.

(deleted)

void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile ("  jmp 0"); 
}

I've tried it works to turn it off, but if I want to turn it on it doesn't work.

I would like if you can help me with a system :

  • If I press the push button the first time the program starts.

  • If I press it a second time the program stops.

  • If I press a third time the program restarts from the setup.

So on and so on.

Thank you

PS: I use the USB cable as a power source so I can't put a switch in the power supply.

Thank you

I gave you the solution to that in reply #2.

You have 3 actions, so need these tests when you detect a change of state of the push button:
buttonPushCounter % 3 == 0 // action 1
buttonPushCounter % 3 == 1 // action 2
buttonPushCounter % 3 == 2 // action 3

You can’t fully stop the program because it would then not be able to detect any further button press. All you can do is stop it performing a section of code.

Exactly. That is why you have to retain the "real" setup() and loop().