buttons.h
1 // SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 // Process button input and return button activity 6 // Retained button code from RGB Shades though just using one button 7 8 #define NUMBUTTONS 1 9 #define MODEBUTTON 2 //define the pin the button is connected to 10 11 #define BTNIDLE 0 12 #define BTNDEBOUNCING 1 13 #define BTNPRESSED 2 14 #define BTNRELEASED 3 15 #define BTNLONGPRESS 4 16 #define BTNLONGPRESSREAD 5 17 18 #define BTNDEBOUNCETIME 20 19 #define BTNLONGPRESSTIME 1000 20 21 unsigned long buttonEvents[NUMBUTTONS]; 22 byte buttonStatuses[NUMBUTTONS]; 23 byte buttonmap[NUMBUTTONS] = {MODEBUTTON}; 24 25 void updateButtons() { 26 for (byte i = 0; i < NUMBUTTONS; i++) { 27 switch (buttonStatuses[i]) { 28 case BTNIDLE: 29 if (digitalRead(buttonmap[i]) == LOW) { 30 buttonEvents[i] = currentMillis; 31 buttonStatuses[i] = BTNDEBOUNCING; 32 } 33 break; 34 35 case BTNDEBOUNCING: 36 if (currentMillis - buttonEvents[i] > BTNDEBOUNCETIME) { 37 if (digitalRead(buttonmap[i]) == LOW) { 38 buttonStatuses[i] = BTNPRESSED; 39 } 40 } 41 break; 42 43 case BTNPRESSED: 44 if (digitalRead(buttonmap[i]) == HIGH) { 45 buttonStatuses[i] = BTNRELEASED; 46 } else if (currentMillis - buttonEvents[i] > BTNLONGPRESSTIME) { 47 buttonStatuses[i] = BTNLONGPRESS; 48 } 49 break; 50 51 case BTNRELEASED: 52 break; 53 54 case BTNLONGPRESS: 55 break; 56 57 case BTNLONGPRESSREAD: 58 if (digitalRead(buttonmap[i]) == HIGH) { 59 buttonStatuses[i] = BTNIDLE; 60 } 61 break; 62 } 63 } 64 } 65 66 byte buttonStatus(byte buttonNum) { 67 68 byte tempStatus = buttonStatuses[buttonNum]; 69 if (tempStatus == BTNRELEASED) { 70 buttonStatuses[buttonNum] = BTNIDLE; 71 } else if (tempStatus == BTNLONGPRESS) { 72 buttonStatuses[buttonNum] = BTNLONGPRESSREAD; 73 } 74 75 return tempStatus; 76 77 } 78 79 // Check the mode button (for switching between effects) 80 void doButtons() { 81 switch (buttonStatus(0)) { 82 83 case BTNRELEASED: // short button press 84 cycleMillis = currentMillis; 85 if (++currentEffect >= numEffects) currentEffect = 0; // loop to start of effect list 86 effectInit = false; // trigger effect initialization when new effect is selected 87 break; 88 89 case BTNLONGPRESS: // long button press 90 currentBrightness += 51; // increase the brightness (wraps to lowest) 91 FastLED.setBrightness(scale8(currentBrightness, MAXBRIGHTNESS)); 92 break; 93 } 94 }