Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- April 6, 2019
- Button demonstration:
- 1. The button sends messages only when it changes state, holding it down does nothing.
- 2. Pin #13 LED lights up to reflect the button state.
- 3. The button uses non-blocking button debouncing on both the press and the release events.
- Board = any
- Turn on the serial monitor to use this sketch. After the "Exiting setup()" message, the button sends a serial message every time it is pressed down, and every time it is released.
- */
- int button_1_pin = 7; // This is where the button is connected.
- // The above pin is using INPUT_PULLUP, so it should connect to a ground pin when pressed.
- int button_debounce_time = 100; // This is the time in milliseconds to wait, to prevent the buttons from bouncing.
- // The variables above you can change the values of.
- // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- // The variables below do not change the values of.
- int button_1_previous_state = 0;
- int button_1_current_state = 0;
- bool buttons_enabled = true;
- unsigned long button_press_begin_time = 0;
- unsigned long button_press_current_time = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(button_1_pin, INPUT_PULLUP);
- Serial.println("Exiting setup()");
- }
- void loop() {
- if (buttons_enabled == true) {
- button_1_current_state = 0;
- if (digitalRead(button_1_pin) == LOW) {
- button_1_current_state = 1;
- }
- // The if() condditions below make sure that you only get the "button pressed" message when the button state changes from "off" to "on".
- if (button_1_previous_state == 0) { // If the button was previously not pressed...
- if (button_1_current_state == 1) { // ...but it is pressed now...
- Serial.println("Button pressed.");
- digitalWrite(13, HIGH);
- button_pressed();
- }
- }
- // The two lines below place a de-bounce delay when the button is released, to make sure that it does not trigger a positive pulse at that time.
- // This part can be placed inside the "if (buttons_enabled == true)" condition because the only time that digitalRead() is called on the button pin is read is when buttons_enabled == true.
- if (button_1_previous_state == 1) { // If the button was previously being pressed...
- if (button_1_current_state == 0) { // ...but it is not pressed now...
- Serial.println("Button released.");
- digitalWrite(13, LOW);
- button_pressed();
- }
- }
- button_1_previous_state = button_1_current_state;
- }
- else { // If the buttons are not enabled, then that means they are de-bouncing and the elapsed time since the press must be checked.
- button_press_current_time = millis();
- if (button_press_current_time > button_press_begin_time) {
- if (button_press_current_time >= (button_press_begin_time + button_debounce_time)) {
- buttons_enabled = true;
- }
- }
- else {
- button_press_begin_time = millis();
- }
- }
- } // end of main loop()
- void button_pressed() {
- buttons_enabled = false;
- button_press_begin_time = millis();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement