Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //***** Basic Blink example, but without the use of delay()
- //***** and only blink if a button is pressed
- /*
- * Hardware:
- * - a LED on 'blink_pin'
- * - a button connected to 'button_pin'
- *
- * Function
- * When button is pressed
- * - the LED will blink a few times ('number_of_short_blinks' times)
- * - then the LED will be left on, for some time ('long_delay' time in ms (milli seconds)
- * - and the LED will blink a fewtimes againb.
- *
- * When button is released
- * - blinking will stop
- * - and blink sequence will start from fresh, upon next button press
- *
- */
- const int blink_pin = 13; // the pin number the LED is connected to
- const int button_pin = 2; // the pin number the Button is connected to
- int on_delay = 75; // sets ON time for the LED in milli seconds (ms)
- int off_delay = 75; // sets OFF time for the LED in milli seconds (ms)
- int long_delay = 1500; // sets ON time for the LED in milli seconds (ms)
- int number_of_short_blinks = 5;
- bool button_state = HIGH; // default state of the button pin
- bool reset_blink = true; // raise flag in order to reset blink sequences
- int blink_count = 0; // counts the number of times a short blink has occured
- //*****************************************************//
- //**** setup()
- void setup() {
- // put your setup code here, to run once:
- // initialize LED pin as an output.
- pinMode(blink_pin, OUTPUT);
- // initialize Button pin
- pinMode(button_pin, INPUT_PULLUP);
- } // END setup()
- //*****************************************************//
- //**** Main loop
- void loop() {
- // put your main code here, to run repeatedly:
- button_state = digitalRead(button_pin);
- if(button_state == LOW){
- Blink_LED();
- }else{
- digitalWrite(blink_pin, LOW); // set the LED off
- reset_blink = true; // next time the LED_blink is called, it will reset its internal values, as starting from scratch
- }
- } // END loop()
- //*****************************************************//
- //**** Blink with no delay
- void Blink_LED() {
- static unsigned long blink_millis = 0;
- static unsigned long blink_interval = 0;
- static boolean do_on = true;
- unsigned long currentMillis = 0;
- currentMillis = millis();
- if(reset_blink) {
- blink_millis = 0;
- blink_interval = 0;
- blink_count = 0;
- reset_blink = false;
- }
- 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 = on_delay; // set the interval to wait for the next action
- do_on = false;
- blink_count++;
- if(blink_count > number_of_short_blinks){
- blink_interval = long_delay; // after a few blinks, a long ON time is set
- blink_count = 0; // ready to count a new sequense of short blinks
- }
- }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 = off_delay; // set the interval to wait for the next action
- do_on = true;
- }
- }
- } // END Blink_LED()
Advertisement
Add Comment
Please, Sign In to add comment