Advertisement
MrAlvin

Arduino - Wait for button presses before running sketch

Nov 3rd, 2023
1,363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.15 KB | Software | 0 0
  1. /*
  2.  *  This sketch waits for 5 seconds before it completely starts up,
  3.  *  it then waits for two different button presses,
  4.  *  one button pressed for 3 seconds
  5.  *  and then another button pressed for 2 seconds
  6.  *  before it runs the rest of the sketch
  7.  *  
  8.  *  Button_1 is connected to D4  -- waiting for 3 second long press
  9.  *  Button_2 is connected to D5  -- waiting for 2 second long press
  10.  *  
  11.  *  How to connect the buttons:
  12.  *     Connect one side (one leg) of a button to the Arduino pin,
  13.  *     connect the other side (the other leg) of the button to GND (minus)
  14.  *    
  15.  *    
  16.  *  by MrAlvin - 2023 Nov. 03 - in the Public Domain
  17.  *  
  18.  *  This sketch has been testet to function on an Arduino UNO
  19.  *  
  20.  */
  21.  
  22.  
  23. //// setting the pin numbers for the Button pins
  24. const int Btn_1_pin = 4;   //pin connected to Button 1 - (other side of button is connected to ground/minus)
  25. const int Btn_2_pin = 5;   //pin connected to Button 2 - (other side of button is connected to ground/minus)
  26.  
  27. //// variables used for keeping track of the button presses
  28. int Btn_1_status = 1;      // when Btn_1_status becomes 0 then the button has been pressed for at least 3 seconds
  29. int Btn_2_status = 1;      // when Btn_2_status becomes 0 then the button has been pressed for at least 2 seconds
  30.  
  31. //// random variables to help support the running of this sketch
  32. unsigned long currentMillis = 0;     // keeps the millis() number in a local variable
  33. unsigned long btn_press_millis = 0;  // used when making sure that buttons are pressed long enough
  34. 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
  35.  
  36. //// some debug variables
  37. const int debugLED = A0;             // a debug LED needs to be connected to pin A0 - user selectable
  38. int debugIdx = 0;                    // used for showing state in a debug print line
  39.  
  40.  
  41. //*****************************************************//
  42. void setup() {
  43.   // ready serial port for showing some debug infoprints
  44.   // initialize serial communication at 115200 bits per second:
  45.   Serial.begin(115200);
  46.  
  47.   // ready Built-In LED for blinking use
  48.   pinMode(LED_BUILTIN, OUTPUT);
  49.  
  50.   // ready debug LED for blinking use
  51.   pinMode(debugLED, OUTPUT);
  52.  
  53.   // prepare two Arduino pins for button presses
  54.   pinMode(Btn_1_pin, INPUT_PULLUP);
  55.   pinMode(Btn_2_pin, INPUT_PULLUP);
  56.  
  57.   // show some debug info
  58.   Serial.println( F("Start 5 sec wait") );  // show that debug-time has started
  59.  
  60.   // wait for 5 seconds (5 seconds = 5000 milli seconds)
  61.   delay(5000);
  62.  
  63.   // show some debug info
  64.   Serial.println( F("Ready for button 1") );
  65.  
  66.   // wait until button 1 has been pressed for at least 3 seconds, then move on
  67.   while (Btn_1_status){
  68.     read_button_1();
  69.     blink_LED();     // blink LED once if button has been pressed for 3 seconds
  70.   }
  71.  
  72.   // show some debug info
  73.   Serial.println( F("Ready for button 2") );
  74.  
  75.   // wait until button 2 has been pressed for at least 2 seconds, then move on
  76.   while (Btn_2_status){
  77.     read_button_2();
  78.     blink_LED();     // blink LED once if button has been pressed for 2 seconds
  79.   }
  80.  
  81.   // button 2  hasbeen pressed, go on with the rest of the skatch
  82.  
  83.   //// blink_n is used in the main demo loop
  84.   blink_n = -1;      // set to  -1  so  blink_LED() in the  loop()  keeps blinking continously
  85.  
  86.    
  87.   // put the rest of your setup code here, to run once:
  88.  
  89.  
  90. } // END setup()
  91.  
  92.  
  93. //*****************************************************//
  94. void loop() {
  95.  
  96.   // demo info to indicate that the button presses succeeded
  97.   blink_LED();
  98.  
  99.  
  100.   // put your main code here, to run repeatedly:
  101.  
  102.  
  103. } // END loop()
  104.  
  105.  
  106. //*****************************************************//
  107. void read_button_1(){
  108.   static int btn1_idx = 0;       // index for the state machine
  109.   const int interval_1 = 3000;   // the number of miliseconds to wait, until moving on
  110.   const int debounce_time = 75;  // number of mili seconds to allow for debounce of the button
  111.   currentMillis = millis();
  112.   debugIdx = btn1_idx;
  113.  
  114.   switch (btn1_idx){
  115.     case 0: // waiting for button 1 to be pressed
  116.       if( digitalRead(Btn_1_pin) == LOW ){
  117.         // Button 1 has been pressed
  118.        
  119.         //get ready to time the 3 second long press of button 1
  120.         btn_press_millis = currentMillis; // save the current time
  121.        
  122.         //debounce btn_1 for 75 milli seconds
  123.         delay(debounce_time);
  124.  
  125.         // move to the next state of the state machine
  126.         btn1_idx++;
  127.       } //end if()
  128.       break;
  129.      
  130.     case 1: // check for 3 second keypress
  131.       // check to see if button has been released too soon
  132.       if( digitalRead(Btn_1_pin) == HIGH ){
  133.         // button has been released too soon
  134.        
  135.         //so reset button testing
  136.         btn1_idx = 0;
  137.        
  138.       } else {
  139.        
  140.         // test to see if the 3 seconds has passed
  141.         if (currentMillis - btn_press_millis >= interval_1) {
  142.           // the 3 seconds have passed, so it is okay to move on
  143.           Btn_1_status = 0; // 0 is signal that button 1 has been pressed for a minimum of 3 seconds
  144.           btn1_idx = 0; // just to keep things clean
  145.  
  146.           // 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
  147.           blink_n = 1;
  148.          
  149.         } // end else
  150.       } // end if()
  151.       break;
  152.      
  153.     default:    
  154.       btn1_idx = 0;  // start over for button 1 if not waiting for a key press
  155.       break;
  156.      
  157.   } // END switch()
  158.   debugInfo();
  159. } // END read_button_1()
  160.  
  161.  
  162. //*****************************************************//
  163. void read_button_2(){
  164.   static int btn2_idx = 0;       // index for the state machine
  165.   const int interval_2 = 2000;   // the number of miliseconds to wait, until moving on
  166.   const int debounce_time = 75;  // number of mili seconds to allow for debounce of the button
  167.   currentMillis = millis();
  168.   debugIdx = btn2_idx;
  169.  
  170.   switch (btn2_idx){
  171.     case 0: // waiting for button 2 to be pressed
  172.       if( digitalRead(Btn_2_pin) == LOW ){
  173.         // Button 2 has been pressed
  174.        
  175.         //get ready to time the 2 second long press of button 2
  176.         btn_press_millis = millis(); // save the current time
  177.        
  178.         //debounce btn_1 for 75 milli seconds
  179.         delay(debounce_time);
  180.  
  181.         // move to the next state of the state machine
  182.         btn2_idx++;
  183.       } //end if()
  184.       break;
  185.      
  186.     case 1: // check for 2 second keypress
  187.       // check to see if button has been released too soon
  188.       if( digitalRead(Btn_2_pin) == HIGH ){
  189.         // button has been released too soon
  190.        
  191.         //so reset button testing
  192.         btn2_idx = 0;
  193.        
  194.       } else {
  195.        
  196.         // test to see if the 3 seconds has passed
  197.         if (millis() - btn_press_millis >= interval_2) {
  198.           // the 3 seconds have passed, so it is okay to move on
  199.           Btn_2_status = 0; // 0 is signal that button 1 has been pressed for a minimum of 3 seconds
  200.           btn2_idx = 0; // just to keep things clean
  201.  
  202.           // 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
  203.           blink_n = 1;  // this blink might not have time to happen,
  204.                         // as the blink_n variable is set to -1 immidiately
  205.                         // after escaping the while() loop
  206.          
  207.         } // end else
  208.       } // end if()
  209.       break;
  210.      
  211.     default:    
  212.       btn2_idx = 0;  // start over for button 1 if not waiting for a key press
  213.       break;
  214.      
  215.   } // END switch()
  216.   debugInfo();
  217. } // END read_button_2()
  218.  
  219.  
  220. //*****************************************************//
  221. void blink_LED() {
  222.   static unsigned long blink_millis = 0;
  223.   static int blink_interval = 0;
  224.   static boolean do_on = true;
  225.   const int blink_pin = LED_BUILTIN;
  226.   currentMillis = millis();
  227.          
  228.   if( (blink_n > 0)  ||  (blink_n < 0)  ){  //so, if = 0 then do nothing
  229.     if ( currentMillis - blink_millis >  blink_interval )  { // if its time to change the blink
  230.       if (do_on) { //use a flag to determine wether to turn on or off the Blink LED
  231.         digitalWrite(blink_pin, HIGH);   // set the LED on, if okay to use power for it
  232.         blink_millis = currentMillis;    // ready for next action after an interval has passed
  233.         blink_interval = 100;            // set the ON interval to wait for the next action
  234.         do_on = false;
  235.       }else{
  236.         digitalWrite(blink_pin, LOW);    // set the LED off
  237.         // set the time to do next blink
  238.         blink_millis = currentMillis;    // ready for next action after an interval has passed
  239.         blink_interval = 100;            // set the OFF interval to wait for the next action
  240.         do_on = true;
  241.         if(blink_n > 0) {
  242.           blink_n--;  //decrease number of blinks, until 0
  243.           Serial.println( F("blink") ); // a possible debug blink marking
  244.         }
  245.       } // end else
  246.     } // end if(time)
  247.  } // end if(blink)
  248.  debugInfo();
  249. } // END Blink_LED()
  250.  
  251.  
  252. //*****************************************************//
  253. void debugInfo() {
  254.   static unsigned long previousDebugMillis = 0;  // save when last debugInfo was shows
  255.   const int intervalDebug = 1000;                // update SerialMonitor once every second
  256.   static bool ledDebugState = 0;                 // used to aid blinking of the debugLED
  257.  
  258.   // is it time to update the SerialMonitor
  259.   if(currentMillis - previousDebugMillis > intervalDebug) {
  260.     //save the time when the Serial Monitor was updated
  261.     previousDebugMillis = currentMillis;  
  262.  
  263.     //// I-am-alive-blink
  264.     // change LED debug state
  265.     ledDebugState = !ledDebugState;
  266.    
  267.     // blink the debugLED
  268.     digitalWrite(debugLED, ledDebugState);
  269.  
  270.     //// show some  debug info via SerialMonitor
  271.     Serial.print( F("Idx: ") );     // Funktionen F() holder "tekst" i Flash Memory only
  272.     Serial.print(debugIdx);
  273.     Serial.print( F(" : ") );
  274.     Serial.print( Btn_1_status );
  275.     Serial.print( F(" : ") );
  276.     Serial.println( Btn_2_status );
  277.   } // end if(time)
  278. } // END debugInfo()
  279.  
  280.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement