14
14
// Version 1.1.0
15
15
#include < TM1637Display.h> // Tm1637 module
16
16
17
- // https://github.com/JChristensen/JC_Button
18
- // Version 2.0-1
19
- #include < JC_Button.h> // push button lib
20
17
21
18
// *************************** GLOBALS ********************
22
19
@@ -29,8 +26,13 @@ TM1637Display display(CLK, DIO);
29
26
int buzzer = 9 ;// the pin of the active buzzer
30
27
31
28
// Button
32
- #define btn_start_pin 2
33
- Button btn_start (btn_start_pin);
29
+ #define buttonPin 2
30
+ int buttonState; // the current reading from the input pin
31
+ int lastButtonState = HIGH; // the previous reading from the input pin
32
+ // the following variables are unsigned longs because the time, measured in
33
+ // milliseconds, will quickly become a bigger number than can be stored in an int.
34
+ unsigned long lastDebounceTime = 0 ; // the last time the output pin was toggled
35
+ unsigned long debounceDelay = 50 ; // the debounce time; increase if the output flickers
34
36
int count_start = 0 ;
35
37
36
38
@@ -60,8 +62,7 @@ void setup() {
60
62
61
63
// button setup
62
64
// Setup pins for button enable internal pull-up resistors
63
- digitalWrite (btn_start_pin, HIGH);
64
- btn_start.begin ();
65
+ digitalWrite (buttonPin, HIGH);
65
66
66
67
// display setup
67
68
display.setBrightness (0x0c );
@@ -78,18 +79,33 @@ void setup() {
78
79
79
80
// ******************* MAIN LOOP *****************
80
81
void loop () {
82
+ // read the pot
81
83
if (count_start == 0 )
82
84
{
83
85
potRead ();
84
86
}
85
87
86
- // start button press
87
- if (btn_start.read ()) {
88
- if (btn_start.wasPressed ())
89
- {
90
- TestButton ();
88
+ // read and debounce push button.
89
+ int reading = digitalRead (buttonPin);
90
+ // If the switch changed?
91
+ if (reading != lastButtonState) {
92
+ // reset the debouncing timer
93
+ lastDebounceTime = millis ();
94
+ }
95
+
96
+ if ((millis () - lastDebounceTime) > debounceDelay) {
97
+ // if the button state has changed:
98
+ if (reading != buttonState) {
99
+ buttonState = reading;
100
+ // start timer if the new button state is low
101
+ if (buttonState == LOW) {
102
+ TestButton ();
103
+ }
91
104
}
92
105
}
106
+
107
+ // save the reading.
108
+ lastButtonState = reading;
93
109
delay (5 );
94
110
}
95
111
@@ -112,7 +128,7 @@ void TestButton()
112
128
// includes smoothing the analog sample by taking avergae of 10 readings
113
129
void potRead ()
114
130
{
115
- unsigned long average = 0 ;
131
+ unsigned long average = 0 ;
116
132
// subtract the last reading:
117
133
total = total - readings[readIndex];
118
134
readings[readIndex] = map (analogRead (potPin), 0 , 1023 , 1 , 99 );
@@ -125,7 +141,6 @@ void potRead()
125
141
}
126
142
average = total / numReadings;
127
143
Serial.println (average);
128
- Serial.println (total);
129
144
timeLimit = ((average * 60 ) * 1000 );
130
145
average = average * 100 ;
131
146
display.showNumberDecEx (average, 0x40 , true );
@@ -148,7 +163,6 @@ void my_buzzer (void)
148
163
// run for 250*1400ms = 3.5 minutes
149
164
for (j = 0 ; j < 250 ; j++)
150
165
{
151
- Serial.println (j);
152
166
// output an frequency
153
167
for (i = 0 ; i < 100 ; i++)
154
168
{
0 commit comments