Skip to content

Commit f3c19d4

Browse files
remove button library with manual coding
1 parent 78aef2e commit f3c19d4

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ Overview
33
--------------------------------------------
44
* Name: timer_arduino
55
* Title : Countdown Timer, arduino based.
6-
* Description: Countdown Timer: 1-99 minutes. Output(mm:ss) to seven segment display(TM1637) and Buzzer.
7-
Push button to start. 10K Pot used for time select input.
6+
* Description: Countdown Timer: Range 1-99 minutes.
7+
Output time (mm:ss) to seven segment display(TM1637) and Audio to Buzzer.
8+
Input: Push button to start. 10K Pot used for time select.
89
* Author: Gavin Lyons
910

1011
libraries
1112
--------------------------
1213
* https://github.com/avishorp/TM1637
1314
* Version 1.1.0
14-
* TM1637Display.h // Tm1637 module
15+
* TM1637Display.h // to drive the Tm1637 module
1516

16-
* https://github.com/JChristensen/JC_Button
17-
* Version 2.0-1
18-
* JC_Button.h // push button lib
1917

2018
Software Used
2119
------------------

timer_arduino.ino

+29-15
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
// Version 1.1.0
1515
#include <TM1637Display.h> // Tm1637 module
1616

17-
// https://github.com/JChristensen/JC_Button
18-
// Version 2.0-1
19-
#include <JC_Button.h> // push button lib
2017

2118
//*************************** GLOBALS ********************
2219

@@ -29,8 +26,13 @@ TM1637Display display(CLK, DIO);
2926
int buzzer = 9;//the pin of the active buzzer
3027

3128
// 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
3436
int count_start = 0;
3537

3638

@@ -60,8 +62,7 @@ void setup() {
6062

6163
// button setup
6264
// Setup pins for button enable internal pull-up resistors
63-
digitalWrite(btn_start_pin, HIGH);
64-
btn_start.begin();
65+
digitalWrite(buttonPin, HIGH);
6566

6667
//display setup
6768
display.setBrightness(0x0c);
@@ -78,18 +79,33 @@ void setup() {
7879

7980
//******************* MAIN LOOP *****************
8081
void loop() {
82+
// read the pot
8183
if (count_start == 0)
8284
{
8385
potRead();
8486
}
8587

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+
}
91104
}
92105
}
106+
107+
// save the reading.
108+
lastButtonState = reading;
93109
delay(5);
94110
}
95111

@@ -112,7 +128,7 @@ void TestButton()
112128
// includes smoothing the analog sample by taking avergae of 10 readings
113129
void potRead()
114130
{
115-
unsigned long average = 0;
131+
unsigned long average = 0;
116132
// subtract the last reading:
117133
total = total - readings[readIndex];
118134
readings[readIndex] = map(analogRead(potPin), 0, 1023, 1, 99);
@@ -125,7 +141,6 @@ void potRead()
125141
}
126142
average = total / numReadings;
127143
Serial.println(average);
128-
Serial.println(total);
129144
timeLimit = ((average * 60) * 1000);
130145
average = average * 100 ;
131146
display.showNumberDecEx(average, 0x40, true);
@@ -148,7 +163,6 @@ void my_buzzer (void)
148163
// run for 250*1400ms = 3.5 minutes
149164
for (j = 0; j < 250; j++)
150165
{
151-
Serial.println(j);
152166
//output an frequency
153167
for (i = 0; i < 100; i++)
154168
{

0 commit comments

Comments
 (0)