Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- August 12, 2017
- This is an example of one way to debounce buttons in code.
- This sketch doesn't produce any functional output, it only shows how to debounce buttons in code.
- Below is defining two different groups of buttons.
- Each group will have its own separate button debounce timer.
- Buttons on pins #2, #3 and #4 are in group #1.
- The button delay (debounce) time for group 1 is set to 1000 milliseconds (the bg1_button_delay_time variable).
- Buttons on pins #5, #6 and #7 are in group #2.
- The button delay (debounce) time for group 2 is set to 5000 milliseconds (the bg2_button_delay_time variable).
- In this example, neither groups of buttons interferes with the other. The delay/debounce times can overlap each other, and both will still run for the correct total time.
- If a button in one group is pressed, no more buttons {in that same group} can be pressed until the time delay {for that group} is passed.
- When this program is checked for the Uno board:
- Sketch uses 1,174 bytes (3%) of program storage space. Maximum is 32,256 bytes.
- Global variables use 27 bytes (1%) of dynamic memory, leaving 2,021 bytes for local variables. Maximum is 2,048 bytes.
- */
- // Button group #1:
- int buttonPin2 = 2; // These are defining the physical input pins for the button connections.
- int buttonPin3 = 3;
- int buttonPin4 = 4;
- // Button group #2:
- int buttonPin5 = 5;
- int buttonPin6 = 6;
- int buttonPin7 = 7;
- boolean bg1_buttonPressed = false; // "bg" here means "button group"
- unsigned long bg1_button_press_start_time = 0; // This stores the system time that the button was pushed.
- unsigned long bg1_button_press_current_time = 0; // This stores the current time, to compare to the value above.
- int bg1_button_delay_time = 1000; // This is the length of time (in milliseconds) to delay button inputs for any of the buttons in button group #1.
- boolean bg2_buttonPressed = false;
- unsigned long bg2_button_press_start_time = 0;
- unsigned long bg2_button_press_current_time = 0;
- int bg2_button_delay_time = 5000; // This is the length of time (in milliseconds) to delay button inputs for any of the buttons in button group #2.
- // ***************************************************
- void setup() {
- // All that is done here is setting the input pin states.
- // This sketch doesn't actually *do* anything with the button inputs, but if it did, you would need these here.
- pinMode(buttonPin2, INPUT_PULLUP);
- pinMode(buttonPin3, INPUT_PULLUP);
- pinMode(buttonPin4, INPUT_PULLUP);
- pinMode(buttonPin5, INPUT_PULLUP);
- pinMode(buttonPin6, INPUT_PULLUP);
- pinMode(buttonPin7, INPUT_PULLUP);
- }
- // ***************************************************
- void loop() {
- if (bg1_buttonPressed == false) {
- if (digitalRead(buttonPin2) == LOW) {
- doButton2stuff();
- bg1_buttonPressDelay();
- }
- if (digitalRead(buttonPin3) == LOW) {
- doButton3stuff();
- bg1_buttonPressDelay();
- }
- if (digitalRead(buttonPin4) == LOW) {
- doButton4stuff();
- bg1_buttonPressDelay();
- }
- }
- if (bg2_buttonPressed == false) {
- if (digitalRead(buttonPin5) == LOW) {
- doButton5stuff();
- bg2_buttonPressDelay();
- }
- if (digitalRead(buttonPin6) == LOW) {
- doButton6stuff();
- bg2_buttonPressDelay();
- }
- if (digitalRead(buttonPin7) == LOW) {
- doButton7stuff();
- bg2_buttonPressDelay();
- }
- }
- // The section below re-enables button group #1 if the timer time span has elapsed since the button was pushed.
- if (bg1_buttonPressed == true) {
- bg1_button_press_current_time = millis();
- if (bg1_button_press_current_time > bg1_button_press_start_time) {
- if (bg1_button_press_current_time >= (bg1_button_press_start_time + bg1_button_delay_time)) {
- bg1_buttonPressed = false;
- }
- }
- else {
- bg1_button_press_start_time = millis();
- }
- }
- // The section below re-enables button group #2 if the timer time span has elapsed since the button was pushed.
- if (bg2_buttonPressed == true) {
- bg2_button_press_current_time = millis();
- if (bg2_button_press_current_time > bg2_button_press_start_time) {
- if (bg2_button_press_current_time >= (bg2_button_press_start_time + bg2_button_delay_time)) {
- bg2_buttonPressed = false;
- }
- }
- else {
- bg2_button_press_start_time = millis();
- }
- }
- } // This is the end of the main sketch loop.
- // ***************************************************
- void bg1_buttonPressDelay() {
- // This gets called when any button in group #1 gets pushed.
- bg1_button_press_start_time = millis();
- bg1_buttonPressed = true;
- }
- // ***************************************************
- void bg2_buttonPressDelay() {
- // This gets called when any button in group #2 gets pushed.
- bg2_button_press_start_time = millis();
- bg2_buttonPressed = true;
- }
- // ***************************************************
- /*
- The individual functions for what each button does are below.
- */
- void doButton2stuff() {
- // Whatever button 2 should do, goes in here
- }
- void doButton3stuff() {
- // Whatever button 3 should do, goes in here
- }
- void doButton4stuff() {
- // Whatever button 4 should do, goes in here
- }
- void doButton5stuff() {
- // Whatever button 5 should do, goes in here
- }
- void doButton6stuff() {
- // Whatever button 6 should do, goes in here
- }
- void doButton7stuff() {
- // Whatever button 7 should do, goes in here
- }
- // ~~~~~~~~ fini ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement