Skip to content

Commit b2d6d3b

Browse files
authored
Create TrinketReactCounter_7segment.ino
1 parent a8d631d commit b2d6d3b

1 file changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Adafruit Trinket React Counter Sketch - 7-segment display
2+
//
3+
// Use a 7-segment LED backpack to display the number of times a
4+
// button has been pressed. Great for building a physical 'like'
5+
// or react button. The value will be stored in EEPROM so it
6+
// 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+
// 7-segment display
52+
Adafruit_7segment backpack = Adafruit_7segment();
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+
backpack.print(count, DEC);
60+
backpack.writeDisplay();
61+
}
62+
63+
void setup() {
64+
// Setup button inputs.
65+
pinMode(COUNT_BUTTON_PIN, INPUT);
66+
67+
// Initialize the LED backpack display.
68+
backpack.begin(LED_BACKPACK_ADDRESS);
69+
70+
// Clear the count in EEPROM if desired.
71+
#ifdef RESET_COUNT
72+
uint16_t count = 0;
73+
EEPROM.put(COUNT_ADDRESS, count);
74+
#endif
75+
76+
// Update the display with the current count value.
77+
update_display();
78+
79+
// Reset the last known time the button wasn't being held.
80+
holdStart = millis();
81+
}
82+
83+
void loop() {
84+
// Take a couple button readings with a small delay in between to detect when
85+
// the signal changes from high to low, i.e. the button was released.
86+
int firstCount = digitalRead(COUNT_BUTTON_PIN);
87+
delay(20);
88+
int secondCount = digitalRead(COUNT_BUTTON_PIN);
89+
90+
// Check for count button release.
91+
if (firstCount == HIGH && secondCount == LOW) {
92+
// Button was released!
93+
// Increment the count value stored in EEPROM.
94+
uint16_t count;
95+
EEPROM.get(COUNT_ADDRESS, count);
96+
count += 1;
97+
EEPROM.put(COUNT_ADDRESS, count);
98+
// Update the display with the latest count value.
99+
update_display();
100+
}
101+
102+
// Reset the hold start if the button wasn't pressed (i.e. first and last were not both high levels).
103+
if ((firstCount != HIGH) || (secondCount != HIGH)) {
104+
holdStart = millis();
105+
}
106+
107+
// Check if the button has been held for the amount of reset time.
108+
if ((RESET_HOLD_SECONDS > 0) && ((millis() - holdStart) >= (RESET_HOLD_SECONDS*1000))) {
109+
// Reset to zero and update the display!
110+
uint16_t count = 0;
111+
EEPROM.put(COUNT_ADDRESS, count);
112+
update_display();
113+
// Reset the hold time to start over again.
114+
holdStart = millis();
115+
// Flash the display a few times to give time to remove finger.
116+
for (int i=0; i<5; ++i) {
117+
delay(200);
118+
backpack.clear();
119+
backpack.writeDisplay();
120+
delay(200);
121+
update_display();
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)