Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Momentary on button switch or Latched button switch Demo
- //IF IN "LATCHED BUTTON MODE", WILL BLINK "Blink()" THE LED IF LATCHED BUTTON IS HELD DOWN kept "LOW" blinkLedPeriod = 300;
- //DECLARATIONS
- //CONSTANTS
- const byte Button_1 = 4; //pin 4
- const byte blinkLedPin = 13;
- //INT
- int buttonState = HIGH; //this variable tracks the state of the button, low if not pressed, high if pressed
- //MOST IMPORTANT HERE
- int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on
- int blinkLedPeriod = 300;
- //LONG
- long lastDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelay = 50; // the debounce time; increase if the output flickers
- //UNSIGNED LONG
- unsigned long currentTime;
- unsigned long blinkLedStartTime;
- //setup
- void setup()
- {
- Serial.begin(115200);
- //PINMODE
- pinMode( Button_1, INPUT_PULLUP);
- pinMode(blinkLedPin, OUTPUT);
- Serial.println(ledState);
- }
- void loop()
- {
- currentTime = millis();
- button_1_press();
- }//loop
- void button_1_press()
- {
- //sample the state of the button - is it pressed or not?
- buttonState = digitalRead( Button_1);
- //filter out any noise by setting a time buffer
- if ( (millis() - lastDebounceTime) > debounceDelay)
- {
- //if the button has been pressed, lets toggle the LED from "off to on" or "on to off"
- if ( (buttonState == LOW) && (ledState < 0) )
- {
- Blink(); //call the Blink() function
- Serial.println("buttonState == LOW");
- Serial.println(ledState);
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //BEGIN: MOMENTARY ON BUTTON OPTION ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //to make a "Momentary on Button Switch", "uncomment" the two lines below, digitalWrite(blinkLedPin, HIGH); and ledState = -ledState; below else it's a Latched switch
- //digitalWrite(blinkLedPin, HIGH); //turn an LED on /////////////////////////////////////
- //ledState = - ledState; //now the LED is on, we need to change the state
- lastDebounceTime = millis(); //set the current time
- }
- else if ( (buttonState == HIGH) && (ledState > 0) )
- {
- Serial.println("buttonState == HIGH");
- Serial.println(ledState);
- //to make a "Momentary on Button Switch", "uncomment" the two lines below, digitalWrite(blinkLedPin, LOW); and ledState = -ledState; below else it's a Latched switch
- //digitalWrite(blinkLedPin, LOW); //turn an LED off
- //ledState = - ledState; //now the LED is off, we need to change the state
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //END: MOMENTARY ON BUTTON OPTION ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- lastDebounceTime = millis(); //set the current time
- }//if button state
- }//millis
- }//button_1_presS
- void Blink() //function
- {
- //let's start with the single blinking LED
- if (currentTime - blinkLedStartTime >= blinkLedPeriod) //time to change the single LED state
- {
- digitalWrite(blinkLedPin, !digitalRead(blinkLedPin));
- blinkLedStartTime = currentTime;
- Serial.println(F("\tBLINK"));
- }
- }//BLINK
Advertisement
Add Comment
Please, Sign In to add comment