Turn on and control an electronic device with a single push-button.

Good morning, sir,

I would like to make a system to start and control a Device with only one push button and 2 LEDs.

Here’s what I want to do:

If we push the push button:

  • 1st press: the system is switched on. (Red LED ON - Blue LED OFF) Display (" Device is started… Press Button to start the program " ).
  • 2nd press: we start the program. (Red LED ON - Blue LED ON) Display (“Program is started … Press button to stop the program”)
  • 3th press: we stop the program. (Red LED ON - Blue LED OFF) Display (“Program stopped”)
  • 4th press : turns off the system. (Red LED OFF - Blue LED OFF).
// this constant won't change:
int button = 2;// the pin that the pushbutton is attached to
int redled = 13;// the pin that the Red LED is attached to
int blueled = 12;// the pin that the Blue LED is attached to

// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // pevious state of the button
int DeviceState = 0; // current state of DEVICE
int redledState = 0; // current state of the Red LED
int blueledState = 0; // current state of the Blue LED


void setup() {
 
// initialize serial communication:
Serial.begin (9600);
// initialize the button pin as a input:
pinMode(button, INPUT);
// initialize the Red LED as an output:
pinMode(redled, OUTPUT);
// initialize the Blue LED as an output:
pinMode(blueled, OUTPUT); 
			
			 }


void loop() {

	// read the pushbutton input pin:
	buttonState = digitalRead(button);
	
  	// compare the buttonState to its previous state
	if (buttonState != lastButtonState) {
	  //if the button state is not 1
      if (!buttonState) {
		//and if Red LED State = 1
        if (redledState)
        //Switch to : Red LED = 0 
		redledState = 0;

		else
		// else Switch : RED LED = 1
		redledState = 1;
        Serial.println ("Device is started... Press Button to start the program");
						}
	  // save the current state as the last state, for next time through the loop
      lastButtonState = buttonState;
										}
     
//display led status on serial communication
Serial.println (redledState);
// turn RED LED State:
digitalWrite(redled, redledState);
// Delay a little bit to avoid bouncing
delay(20);

			 }
/***********************   **************************/

Thank you for help.

Wiring of the system as an attachment.

Take a look at the StateChangeDetection example in the IDE as a starting point

You can get button press_count.

If ((count% 4) == 0) => the system is switched on.
If ((count% 4) == 1) => we start the program
If ((count% 4) == 2) => we stop the program.
If ((count% 4) == 3) => turns off the system.

See button get count and button control led tutorial

IoT_hobbyist:
You can get button press_count.

If ((count% 4) == 0) => the system is switched on.
If ((count% 4) == 1) => we start the program
If ((count% 4) == 2) => we stop the program.
If ((count% 4) == 3) => turns off the system.

I’ve made it work well… Thank you very much.
I would like to do now that if you press long time ( > 1000 & < 2000 ) milliseconds you turn on the device.
and if the device is switched on, pressing long time ( > 1000 & < 2000 ) turns the device off.

and if you press very long time ( > 10000 ) milliseconds it resets the device.

Thank you for help

// this constant won't change:
int button = 2;// the pin that the pushbutton is attached to
int redled = 13;// the pin that the Red LED is attached to
int blueled = 12;// the pin that the Blue LED is attached to

// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // pevious state of the button
int DeviceState = 0; // current state of DEVICE
int redledState = 0; // current state of the Red LED
int blueledState = 0; // current state of the Blue LED
int buttonPushCounter = 0; // counter for the number of button presses


void setup() {
 
// initialize serial communication:
Serial.begin (9600);
// initialize the button pin as a input:
pinMode(button, INPUT);
// initialize the Red LED as an output:
pinMode(redled, OUTPUT);
// initialize the Blue LED as an output:
pinMode(blueled, OUTPUT); 
 
 }

void loop() {
  
  // read the pushbutton input pin:
  buttonState = digitalRead(button);

  // 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++;
     } 
    }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

//turns on the Device four button pushes by checking the modulo of the button push counter.
  
// first press
if (buttonPushCounter % 4 == 1) {
       digitalWrite(redled, HIGH);
       digitalWrite(blueled, LOW);
       Serial.println("Device is started... Press Button to start the program");
     }
// Second Press   
if (buttonPushCounter % 4 == 2) {
     digitalWrite(redled, HIGH);
        digitalWrite(blueled, HIGH);
        Serial.println("Program is started ... Press button to stop the program");
       
    } 
// Third Press  
if (buttonPushCounter % 4 == 3) {
       digitalWrite(redled, HIGH);
       digitalWrite(blueled, LOW);
       Serial.println("Program stopped");
    }
// Fourth Press  
if (buttonPushCounter % 4 == 4) {
       digitalWrite(redled, LOW);
       digitalWrite(blueled, LOW);
    }

 Serial.print(buttonPushCounter %4);
 Serial.println(" th Press");
  
// Delay a little bit to avoid bouncing
   delay(10);
 }

Hi,
OPs picture;

Tom... :slight_smile:

TomGeorge:
Hi,
OPs picture;

Tom... :slight_smile:

Thank you
I did it on: https://www.tinkercad.com/
it also allows me to simulate the UNO without having to plug in ...