// TM1637TinyDisplay TEST Sketch for 4 Digit Displays
// This is a test countdown sketch for the Arduino TM1637TinyDisplay LED Display library
//
// 4-Digit Display: [>1hr] hh:mm then [<1hr] mm:ss
//
#include <pitches.h>
/* Set Display Digits - 4
#define DIGITS 4
/* Digital Pins to TM1637 */
#define CLK 3
#define DIO 2
/* Set up 4-Digit Display */
#include <TM1637TinyDisplay.h>
TM1637TinyDisplay display(CLK, DIO);
uint8_t dots = 0b01000000; // Add dots or colons (depends on display module)
/* Useful Constants */
#define SECS_PER_MIN (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY (SECS_PER_HOUR * 24L)
/* Useful Macros for time (s) */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)
#define hmsToMillis(_h_, _m_, _s_) ((_h_ * SECS_PER_HOUR) + (_m_ * SECS_PER_MIN) + _s_ ) * 1000ul;
/* Global Variables in milliseconds */
unsigned long startTime;
unsigned long lastLoopTime;
unsigned long countDown;
#define BUZZER_PIN 4
// https://github.com/hibit-dev/buzzer/tree/master/src
int melody[] = {
NOTE_E5, NOTE_D5, NOTE_FS4, NOTE_GS4,
NOTE_CS5, NOTE_B4, NOTE_D4, NOTE_E4,
NOTE_B4, NOTE_A4, NOTE_CS4, NOTE_E4,
NOTE_A4
};
int durations[] = {
8, 8, 4, 4,
8, 8, 4, 4,
8, 8, 4, 4,
2
};
void setup()
{
pinMode(BUZZER_PIN, OUTPUT);
display.begin();
// Record Epoch - Same as Timer Reset
startTime = millis();
// Set countdown timer in h, m, s
int Hour = 0;
int Min = 0;
int Sec = 3;
countDown = hmsToMillis(Hour, Min, Sec);
}
void loop()
{
unsigned long timeNow = millis();
unsigned long timeElapsed = timeNow - startTime; // amount of time since start
unsigned long counter = countDown - timeElapsed; // current state of countdown
// Update Display - every 10ms
if (timeNow - lastLoopTime >= 10) {
lastLoopTime = timeNow; // remember last time we displayed
if (timeElapsed >= countDown) {
// If we hit zero - flash every second
int size = sizeof(durations) / sizeof(int);
for (int note = 0; note < size; note++) {
if (note % 3) {
display.clear();
}
else {
display.showNumberDec(000000, dots, true);
}
//to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int duration = 1000 / durations[note];
tone(BUZZER_PIN, melody[note], duration);
//to distinguish the notes, set a minimum time between them.
//the note's duration + 30% seems to work well:
int pauseBetweenNotes = duration * 1.30;
delay(pauseBetweenNotes);
//stop the tone playing:
noTone(BUZZER_PIN);
}
delay(2000);
}
else {
// Compute the values
unsigned long HOURS = numberOfHours(counter / 1000);
unsigned long MINUTES = numberOfMinutes(counter / 1000);
unsigned long SECONDS = numberOfSeconds(counter / 1000);
unsigned long MILLISECONDS = (counter % 1000) / 10;
// Convert time values to integer to display
if (HOURS <= 0) {
// Display M:S.hs if timer is below one hour
display.showNumberDec(MINUTES, dots, true, 2, 0);
display.showNumberDec(SECONDS, dots, true, 2, 2);
} else {
// Display H:M:S if timer is above one hour
display.showNumberDec(HOURS, dots, true, 2, 0);
display.showNumberDec(MINUTES, dots, true, 2, 2);
}
}
}
}