MrAlvin

Arduino BlinkWithoutDelay + only when button is pressed

Jun 23rd, 2023 (edited)
1,280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | Software | 0 0
  1. //***** Basic Blink example, but without the use of delay()
  2. //***** and only blink if a button is pressed
  3. /*
  4.  *  Hardware:
  5.  *     - a LED on 'blink_pin'
  6.  *     - a button connected to 'button_pin'
  7.  *    
  8.  *   Function
  9.  *      When button is pressed
  10.  *       - the LED will blink a few times ('number_of_short_blinks' times)
  11.  *       - then the LED will be left on, for some time ('long_delay' time in ms (milli seconds)
  12.  *       - and the LED will blink a fewtimes againb.
  13.  *    
  14.  *      When button is released
  15.  *       - blinking will stop
  16.  *       - and blink sequence will start from fresh, upon next button press
  17.  *  
  18.  */
  19.  
  20. const int blink_pin = 13;       // the pin number the LED is connected to
  21. const int button_pin = 2;       // the pin number the Button is connected to
  22. int on_delay = 75;              // sets ON time for the LED in milli seconds (ms)
  23. int off_delay = 75;             // sets OFF time for the LED in milli seconds (ms)
  24. int long_delay = 1500;          // sets ON time for the LED in milli seconds (ms)
  25. int number_of_short_blinks = 5;
  26.  
  27. bool button_state = HIGH;    // default state of the button pin
  28. bool reset_blink = true;     // raise flag in order to reset blink sequences
  29. int blink_count = 0;         // counts the number of times a short blink has occured
  30.  
  31. //*****************************************************//
  32. //****       setup()
  33. void setup() {
  34.   // put your setup code here, to run once:
  35.  
  36.   // initialize LED pin as an output.
  37.   pinMode(blink_pin, OUTPUT);
  38.  
  39.   // initialize Button pin
  40.   pinMode(button_pin, INPUT_PULLUP);
  41. } // END setup()
  42.  
  43.  
  44. //*****************************************************//
  45. //****       Main loop                        
  46. void loop() {
  47.   // put your main code here, to run repeatedly:
  48.  
  49.   button_state = digitalRead(button_pin);
  50.  
  51.   if(button_state == LOW){
  52.     Blink_LED();
  53.   }else{
  54.     digitalWrite(blink_pin, LOW);    // set the LED off
  55.     reset_blink = true;   // next time the LED_blink is called, it will reset its internal values, as starting from scratch
  56.   }
  57. } // END loop()
  58.  
  59.  
  60. //*****************************************************//
  61. //****       Blink with no delay
  62. void Blink_LED() {
  63.   static unsigned long blink_millis = 0;
  64.   static unsigned long blink_interval = 0;
  65.   static boolean do_on = true;
  66.   unsigned long currentMillis = 0;
  67.  
  68.   currentMillis = millis();
  69.  
  70.   if(reset_blink) {
  71.     blink_millis = 0;
  72.     blink_interval = 0;
  73.     blink_count = 0;
  74.     reset_blink = false;
  75.   }
  76.  
  77.   if ( currentMillis - blink_millis >  blink_interval )  { // if its time to change the blink
  78.     if (do_on) { //use a flag to determine wether to turn on or off the Blink LED
  79.       digitalWrite(blink_pin, HIGH);   // set the LED on, if okay to use power for it
  80.       blink_millis = currentMillis;    // ready for next action after an interval has passed
  81.       blink_interval = on_delay;       // set the interval to wait for the next action
  82.       do_on = false;
  83.      
  84.       blink_count++;
  85.       if(blink_count > number_of_short_blinks){
  86.         blink_interval = long_delay;   // after a few blinks, a long ON time is set
  87.         blink_count = 0;               // ready to count a new sequense of short blinks
  88.       }
  89.     }else{
  90.       digitalWrite(blink_pin, LOW);    // set the LED off
  91.       // set the time to do next blink
  92.       blink_millis = currentMillis;    // ready for next action after an interval has passed
  93.       blink_interval = off_delay;      // set the interval to wait for the next action
  94.       do_on = true;
  95.     }
  96.   }
  97. } // END Blink_LED()
Advertisement
Add Comment
Please, Sign In to add comment