So I’m trying to build a timer using an I2C backpack and a 4-digit 7-segment LED display. I’ve managed to find/piece together a sketch that counts down in minutes/seconds, but currently it runs non-stop and then repeats, no way to start/pause it, and the only way to change the length of the countdown is to plug it in and edit the code.
So far though I haven’t been able to find any code from projects doing anything similar (with an I2C display, so I have no reference code to look at when trying to put my buttons in. If I were to define pins and buttons using the code below, how exactly would I tie them to the numbers displayed by the matrix commands?
/***********************************************
* Count down using millis()
*
* Adafruit 7-segment, I2C, LED backpack display
*
* pin A5 -> C (on 7-segment)
* pin A4 -> D (on 7-segment)
*
************************************************/
#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_LEDBackpack.h"
Adafruit_7segment matrix = Adafruit_7segment();
unsigned long previousSecondMillis = 0UL;
long oneSecond = 1000UL; // milliseconds per second
#define startMinute 10 // Modify these defines to
#define startSecond 00 // change the timer interval
int minutes = startMinute;
int seconds = startSecond;
void setup()
{
matrix.begin(0x70);
}
void loop() {
// --------- Run this every second ------------------
if (millis() - previousSecondMillis >= oneSecond) {
matrix.writeDigitNum(0, (minutes / 10));
matrix.writeDigitNum(1, (minutes % 10));
matrix.writeDigitNum(3, (seconds / 10));
matrix.writeDigitNum(4, (seconds % 10));
matrix.writeDisplay();
if (seconds-- == 0) {
if (minutes == 0) {
minutes = startMinute;
seconds = startSecond;
delay(1000);
} else {
minutes -= 1;
seconds = 59;
}
}
previousSecondMillis += oneSecond;
}
}