|
| 1 | +// Adafruit Trinket React Counter Sketch - 14-segment quad alpha display |
| 2 | +// |
| 3 | +// Use a 14-segment quad alphanumeric LED backpack to display the |
| 4 | +// number of times a button has been pressed. Great for building |
| 5 | +// a physical 'like' or react button. The value will be stored |
| 6 | +// in EEPROM so it will persist between power down/up. |
| 7 | +// |
| 8 | +// NOTE: As-is this sketch needs to run on a Trinket because it |
| 9 | +// assumes the switch on pin #1 has a pull-down resistor to ground. |
| 10 | +// If using another board without this pull-down you can explicitly |
| 11 | +// add a ~10kohm resistor from digital #1 to ground. |
| 12 | +// |
| 13 | +// Author: Tony DiCola |
| 14 | +// License: MIT (https://opensource.org/licenses/MIT) |
| 15 | +#include <EEPROM.h> |
| 16 | +#include <Wire.h> |
| 17 | +#include "Adafruit_LEDBackpack.h" |
| 18 | +#include "Adafruit_GFX.h" |
| 19 | + |
| 20 | + |
| 21 | +// Uncomment the line below to reset the counter value in EEPROM to zero. |
| 22 | +// After uncommenting reload the sketch and during the setup the counter |
| 23 | +// will be reset. Then comment the line again and reload to start again |
| 24 | +// (if you don't comment it out then every time the board powers on it |
| 25 | +// will reset back to zero!). |
| 26 | +//#define RESET_COUNT |
| 27 | + |
| 28 | +// OR just hold the button for longer than the RESET_HOLD_SECOND below |
| 29 | +// to reset the count! |
| 30 | + |
| 31 | +// Configuration: |
| 32 | +#define LED_BACKPACK_ADDRESS 0x70 // I2C address of the backpack display. |
| 33 | + // Keep the default 0x70 unless you |
| 34 | + // change the backpack's address bridges. |
| 35 | + |
| 36 | +#define COUNT_BUTTON_PIN 1 // Digital input connected to the button that |
| 37 | + // will increase the count. This line should |
| 38 | + // have a pull-down resistor to ground. The |
| 39 | + // opposite side of the button should be |
| 40 | + // connected to a high level like 5V or 3.3V. |
| 41 | + |
| 42 | +#define COUNT_ADDRESS 0 // Address in EEPROM to store the counter. |
| 43 | + // This will take 2 bytes (16-bit value). |
| 44 | + // You don't need to change this unless you |
| 45 | + // want to play with different EEPROM locations. |
| 46 | + |
| 47 | +#define RESET_HOLD_SECONDS 5 // Number of seconds to hold the button down to |
| 48 | + // force a reset of the count to zero. Set to 0 |
| 49 | + // to disable this functionality. |
| 50 | + |
| 51 | +// 14-segment quad alphanumeric display |
| 52 | +Adafruit_AlphaNum4 backpack = Adafruit_AlphaNum4(); |
| 53 | +uint32_t holdStart = 0; |
| 54 | + |
| 55 | +void update_display() { |
| 56 | + // Get the count value from EEPROM and print it to the display. |
| 57 | + uint16_t count; |
| 58 | + EEPROM.get(COUNT_ADDRESS, count); |
| 59 | + // Use writeDigitRaw function to write out each digit for the thousands, |
| 60 | + // hundreds, tens, and ones place. |
| 61 | + int thousands = count / 1000; |
| 62 | + int remainder = count % 1000; |
| 63 | + int hundreds = remainder / 100; |
| 64 | + remainder = remainder % 100; |
| 65 | + int tens = remainder / 10; |
| 66 | + remainder = remainder % 10; |
| 67 | + // Check if the value is too large to display and just print dashes. |
| 68 | + if (thousands >= 10) { |
| 69 | + backpack.writeDigitAscii(0, '-'); |
| 70 | + backpack.writeDigitAscii(1, '-'); |
| 71 | + backpack.writeDigitAscii(2, '-'); |
| 72 | + backpack.writeDigitAscii(3, '-'); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + // Print out the number starting from the first non-zero digit. |
| 77 | + if (thousands > 0) { |
| 78 | + backpack.writeDigitAscii(0, '0'+thousands); |
| 79 | + backpack.writeDigitAscii(1, '0'+hundreds); |
| 80 | + backpack.writeDigitAscii(2, '0'+tens); |
| 81 | + backpack.writeDigitAscii(3, '0'+remainder); |
| 82 | + } |
| 83 | + else if (hundreds > 0) { |
| 84 | + backpack.writeDigitAscii(1, '0'+hundreds); |
| 85 | + backpack.writeDigitAscii(2, '0'+tens); |
| 86 | + backpack.writeDigitAscii(3, '0'+remainder); |
| 87 | + } |
| 88 | + else if (tens > 0) { |
| 89 | + backpack.writeDigitAscii(2, '0'+tens); |
| 90 | + backpack.writeDigitAscii(3, '0'+remainder); |
| 91 | + } |
| 92 | + else { |
| 93 | + backpack.writeDigitAscii(3, '0'+remainder); |
| 94 | + } |
| 95 | + } |
| 96 | + backpack.writeDisplay(); |
| 97 | +} |
| 98 | + |
| 99 | +void setup() { |
| 100 | + // Setup button inputs. |
| 101 | + pinMode(COUNT_BUTTON_PIN, INPUT); |
| 102 | + |
| 103 | + // Initialize the LED backpack display. |
| 104 | + backpack.begin(LED_BACKPACK_ADDRESS); |
| 105 | + |
| 106 | + // Clear the count in EEPROM if desired. |
| 107 | + #ifdef RESET_COUNT |
| 108 | + uint16_t count = 0; |
| 109 | + EEPROM.put(COUNT_ADDRESS, count); |
| 110 | + #endif |
| 111 | + |
| 112 | + // Update the display with the current count value. |
| 113 | + update_display(); |
| 114 | + |
| 115 | + // Reset the last known time the button wasn't being held. |
| 116 | + holdStart = millis(); |
| 117 | +} |
| 118 | + |
| 119 | +void loop() { |
| 120 | + // Take a couple button readings with a small delay in between to detect when |
| 121 | + // the signal changes from high to low, i.e. the button was released. |
| 122 | + int firstCount = digitalRead(COUNT_BUTTON_PIN); |
| 123 | + delay(20); |
| 124 | + int secondCount = digitalRead(COUNT_BUTTON_PIN); |
| 125 | + |
| 126 | + // Check for count button release. |
| 127 | + if (firstCount == HIGH && secondCount == LOW) { |
| 128 | + // Button was released! |
| 129 | + // Increment the count value stored in EEPROM. |
| 130 | + uint16_t count; |
| 131 | + EEPROM.get(COUNT_ADDRESS, count); |
| 132 | + count += 1; |
| 133 | + EEPROM.put(COUNT_ADDRESS, count); |
| 134 | + // Update the display with the latest count value. |
| 135 | + update_display(); |
| 136 | + } |
| 137 | + |
| 138 | + // Reset the hold start if the button wasn't pressed (i.e. first and last were not both high levels). |
| 139 | + if ((firstCount != HIGH) || (secondCount != HIGH)) { |
| 140 | + holdStart = millis(); |
| 141 | + } |
| 142 | + |
| 143 | + // Check if the button has been held for the amount of reset time. |
| 144 | + if ((RESET_HOLD_SECONDS > 0) && ((millis() - holdStart) >= (RESET_HOLD_SECONDS*1000))) { |
| 145 | + // Reset to zero and update the display! |
| 146 | + uint16_t count = 0; |
| 147 | + EEPROM.put(COUNT_ADDRESS, count); |
| 148 | + update_display(); |
| 149 | + // Reset the hold time to start over again. |
| 150 | + holdStart = millis(); |
| 151 | + // Flash the display a few times to give time to remove finger. |
| 152 | + for (int i=0; i<5; ++i) { |
| 153 | + delay(200); |
| 154 | + backpack.clear(); |
| 155 | + backpack.writeDisplay(); |
| 156 | + delay(200); |
| 157 | + update_display(); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments