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