Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This sketch waits for 5 seconds before it completely starts up,
- * it then waits for two different button presses,
- * one button pressed for 3 seconds
- * and then another button pressed for 2 seconds
- * before it runs the rest of the sketch
- *
- * Button_1 is connected to D4 -- waiting for 3 second long press
- * Button_2 is connected to D5 -- waiting for 2 second long press
- *
- * How to connect the buttons:
- * Connect one side (one leg) of a button to the Arduino pin,
- * connect the other side (the other leg) of the button to GND (minus)
- *
- *
- * by MrAlvin - 2023 Nov. 03 - in the Public Domain
- *
- * This sketch has been testet to function on an Arduino UNO
- *
- */
- //// setting the pin numbers for the Button pins
- const int Btn_1_pin = 4; //pin connected to Button 1 - (other side of button is connected to ground/minus)
- const int Btn_2_pin = 5; //pin connected to Button 2 - (other side of button is connected to ground/minus)
- //// variables used for keeping track of the button presses
- int Btn_1_status = 1; // when Btn_1_status becomes 0 then the button has been pressed for at least 3 seconds
- int Btn_2_status = 1; // when Btn_2_status becomes 0 then the button has been pressed for at least 2 seconds
- //// random variables to help support the running of this sketch
- unsigned long currentMillis = 0; // keeps the millis() number in a local variable
- unsigned long btn_press_millis = 0; // used when making sure that buttons are pressed long enough
- int blink_n = 0; // an option to blink the built-in LED continously or n times // -1 == blink continously // 0 == no blink // n > 0 == blink N times
- //// some debug variables
- const int debugLED = A0; // a debug LED needs to be connected to pin A0 - user selectable
- int debugIdx = 0; // used for showing state in a debug print line
- //*****************************************************//
- void setup() {
- // ready serial port for showing some debug infoprints
- // initialize serial communication at 115200 bits per second:
- Serial.begin(115200);
- // ready Built-In LED for blinking use
- pinMode(LED_BUILTIN, OUTPUT);
- // ready debug LED for blinking use
- pinMode(debugLED, OUTPUT);
- // prepare two Arduino pins for button presses
- pinMode(Btn_1_pin, INPUT_PULLUP);
- pinMode(Btn_2_pin, INPUT_PULLUP);
- // show some debug info
- Serial.println( F("Start 5 sec wait") ); // show that debug-time has started
- // wait for 5 seconds (5 seconds = 5000 milli seconds)
- delay(5000);
- // show some debug info
- Serial.println( F("Ready for button 1") );
- // wait until button 1 has been pressed for at least 3 seconds, then move on
- while (Btn_1_status){
- read_button_1();
- blink_LED(); // blink LED once if button has been pressed for 3 seconds
- }
- // show some debug info
- Serial.println( F("Ready for button 2") );
- // wait until button 2 has been pressed for at least 2 seconds, then move on
- while (Btn_2_status){
- read_button_2();
- blink_LED(); // blink LED once if button has been pressed for 2 seconds
- }
- // button 2 hasbeen pressed, go on with the rest of the skatch
- //// blink_n is used in the main demo loop
- blink_n = -1; // set to -1 so blink_LED() in the loop() keeps blinking continously
- // put the rest of your setup code here, to run once:
- } // END setup()
- //*****************************************************//
- void loop() {
- // demo info to indicate that the button presses succeeded
- blink_LED();
- // put your main code here, to run repeatedly:
- } // END loop()
- //*****************************************************//
- void read_button_1(){
- static int btn1_idx = 0; // index for the state machine
- const int interval_1 = 3000; // the number of miliseconds to wait, until moving on
- const int debounce_time = 75; // number of mili seconds to allow for debounce of the button
- currentMillis = millis();
- debugIdx = btn1_idx;
- switch (btn1_idx){
- case 0: // waiting for button 1 to be pressed
- if( digitalRead(Btn_1_pin) == LOW ){
- // Button 1 has been pressed
- //get ready to time the 3 second long press of button 1
- btn_press_millis = currentMillis; // save the current time
- //debounce btn_1 for 75 milli seconds
- delay(debounce_time);
- // move to the next state of the state machine
- btn1_idx++;
- } //end if()
- break;
- case 1: // check for 3 second keypress
- // check to see if button has been released too soon
- if( digitalRead(Btn_1_pin) == HIGH ){
- // button has been released too soon
- //so reset button testing
- btn1_idx = 0;
- } else {
- // test to see if the 3 seconds has passed
- if (currentMillis - btn_press_millis >= interval_1) {
- // the 3 seconds have passed, so it is okay to move on
- Btn_1_status = 0; // 0 is signal that button 1 has been pressed for a minimum of 3 seconds
- btn1_idx = 0; // just to keep things clean
- // it might be a good idea to blink the built_in_LED, to signal that button 1 has been pressed for at least 3 seconds
- blink_n = 1;
- } // end else
- } // end if()
- break;
- default:
- btn1_idx = 0; // start over for button 1 if not waiting for a key press
- break;
- } // END switch()
- debugInfo();
- } // END read_button_1()
- //*****************************************************//
- void read_button_2(){
- static int btn2_idx = 0; // index for the state machine
- const int interval_2 = 2000; // the number of miliseconds to wait, until moving on
- const int debounce_time = 75; // number of mili seconds to allow for debounce of the button
- currentMillis = millis();
- debugIdx = btn2_idx;
- switch (btn2_idx){
- case 0: // waiting for button 2 to be pressed
- if( digitalRead(Btn_2_pin) == LOW ){
- // Button 2 has been pressed
- //get ready to time the 2 second long press of button 2
- btn_press_millis = millis(); // save the current time
- //debounce btn_1 for 75 milli seconds
- delay(debounce_time);
- // move to the next state of the state machine
- btn2_idx++;
- } //end if()
- break;
- case 1: // check for 2 second keypress
- // check to see if button has been released too soon
- if( digitalRead(Btn_2_pin) == HIGH ){
- // button has been released too soon
- //so reset button testing
- btn2_idx = 0;
- } else {
- // test to see if the 3 seconds has passed
- if (millis() - btn_press_millis >= interval_2) {
- // the 3 seconds have passed, so it is okay to move on
- Btn_2_status = 0; // 0 is signal that button 1 has been pressed for a minimum of 3 seconds
- btn2_idx = 0; // just to keep things clean
- // it might be a good idea to blink the built_in_LED, to signal that button 1 has been pressed for at least 3 seconds
- blink_n = 1; // this blink might not have time to happen,
- // as the blink_n variable is set to -1 immidiately
- // after escaping the while() loop
- } // end else
- } // end if()
- break;
- default:
- btn2_idx = 0; // start over for button 1 if not waiting for a key press
- break;
- } // END switch()
- debugInfo();
- } // END read_button_2()
- //*****************************************************//
- void blink_LED() {
- static unsigned long blink_millis = 0;
- static int blink_interval = 0;
- static boolean do_on = true;
- const int blink_pin = LED_BUILTIN;
- currentMillis = millis();
- if( (blink_n > 0) || (blink_n < 0) ){ //so, if = 0 then do nothing
- if ( currentMillis - blink_millis > blink_interval ) { // if its time to change the blink
- if (do_on) { //use a flag to determine wether to turn on or off the Blink LED
- digitalWrite(blink_pin, HIGH); // set the LED on, if okay to use power for it
- blink_millis = currentMillis; // ready for next action after an interval has passed
- blink_interval = 100; // set the ON interval to wait for the next action
- do_on = false;
- }else{
- digitalWrite(blink_pin, LOW); // set the LED off
- // set the time to do next blink
- blink_millis = currentMillis; // ready for next action after an interval has passed
- blink_interval = 100; // set the OFF interval to wait for the next action
- do_on = true;
- if(blink_n > 0) {
- blink_n--; //decrease number of blinks, until 0
- Serial.println( F("blink") ); // a possible debug blink marking
- }
- } // end else
- } // end if(time)
- } // end if(blink)
- debugInfo();
- } // END Blink_LED()
- //*****************************************************//
- void debugInfo() {
- static unsigned long previousDebugMillis = 0; // save when last debugInfo was shows
- const int intervalDebug = 1000; // update SerialMonitor once every second
- static bool ledDebugState = 0; // used to aid blinking of the debugLED
- // is it time to update the SerialMonitor
- if(currentMillis - previousDebugMillis > intervalDebug) {
- //save the time when the Serial Monitor was updated
- previousDebugMillis = currentMillis;
- //// I-am-alive-blink
- // change LED debug state
- ledDebugState = !ledDebugState;
- // blink the debugLED
- digitalWrite(debugLED, ledDebugState);
- //// show some debug info via SerialMonitor
- Serial.print( F("Idx: ") ); // Funktionen F() holder "tekst" i Flash Memory only
- Serial.print(debugIdx);
- Serial.print( F(" : ") );
- Serial.print( Btn_1_status );
- Serial.print( F(" : ") );
- Serial.println( Btn_2_status );
- } // end if(time)
- } // END debugInfo()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement