Code as promised:
[code]/////////////////////////////////
// 4-DIGIT TIMER USING MAX7219 //
// By BulletMagnet83 //
/////////////////////////////////
#include "LedControl.h"
LedControl lc=LedControl(12, 11, 10, 1); //data, clock, load, number of devices
int minutes_t = 0;
int minutes_u = 0;
int seconds_t = 0;
int seconds_u = 0;
int tenths_u = 0;
void setup () {
lc.shutdown(0, false); //wake-up MAX7219
lc.setIntensity(0, 1); //medium display intensity
lc.clearDisplay(0); //blank display
pinMode(A5, INPUT); //minutes button
pinMode(A4, INPUT); //seconds button
pinMode(A3, INPUT); //start
digitalWrite(A5, HIGH); //set pull-up
digitalWrite(A4, HIGH); //set pull-up
digitalWrite(A3, HIGH); //set pull-up
pinMode(A2, INPUT); // not really necessary, pins default to INPUT anyway
digitalWrite(A2, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to ground.
}
void delaySet () { //allow user to preset delay time
while(1) { //infinite-loop this block while delay is being set
lc.setChar(0, 0, minutes_t, false); //display tens of minutes
lc.setChar(0, 1, minutes_u, true); //display ones of minutes, with decimal point
lc.setChar(0, 2, seconds_t, true); //display tens of seconds
lc.setChar(0, 3, seconds_u, false); //display ones of seconds
if(digitalRead(A5) == LOW) { //read input from minutes button
minutes_u++; //increment value by 1
delay(150); //short delay to debounce input, can hold button without it going mad
if(minutes_u > 9) { //this block of statements controls counting
minutes_t++;
minutes_u = 0;
}
if(minutes_t > 5) {
minutes_t = 0;
minutes_u = 0;
}
}
if(digitalRead(A4) == LOW) { //same here but for seconds
seconds_u++;
delay(150);
if(seconds_u > 9) {
seconds_t++;
seconds_u = 0;
}
if(seconds_t > 5) {
seconds_t = 0;
seconds_u = 0;
minutes_u++;
}
}
if(digitalRead(A3) == LOW) {
countDown(); //begin countdown
}
}
}
void error() {
lc.clearDisplay(0); //blank display
lc.setChar(0, 1, 'E', false); //this block writes "Err" to display
lc.setRow(0, 2, 0x05);
lc.setRow(0, 3, 0x05);
tone(7, 700, 10);
delay(50);
tone(7, 700, 10);
delay(50);
tone(7, 700, 10);
delay(300);
delaySet(); //return to delay set function, previous entry is remembered
}
void updateDisplay () { //displays minutes and seconds in 00:00 format
lc.setChar(0, 0, minutes_t, false); //display tens of minutes
lc.setChar(0, 1, minutes_u, true); //display units of minutes and lower decimal point
lc.setChar(0, 2, seconds_t, true); //display tens of seconds and upper decimal point
lc.setChar(0, 3, seconds_u, false); //display units of seconds
}
void udateDisplayUnderTenMinutes(){ // displays minutes and seconds 0:00 format
lc.setChar(0, 0, ' ', false);
lc.setChar(0, 1, minutes_u, true); //display units of minutes and lower decimal point
lc.setChar(0, 2, seconds_t, true); //display tens of second and upper decimal point
lc.setChar(0, 3, seconds_u, false); //display units of seconds
}
void updateDisplayUnderOneMinute(){ // displays seconds and tenths of seconds in 00:0 format
lc.setChar(0, 0, seconds_t, false);
lc.setChar(0, 1, seconds_u, true); //display units of seconds and lower decimal point
lc.setChar(0, 2, tenths_u, false); //display tenths of second
lc.setChar(0, 3, ' ', false);
}
void updateDisplayUnderTen() { // displays seconds and tenths of second in 0.0 format
lc.setChar(0, 0, ' ', false);
lc.setChar(0, 1, seconds_u, true); //display ones of seconds and lower decimal point
lc.setChar(0, 2, tenths_u, false); //display tenths of second
lc.setChar(0, 3, ' ', false);
}
void timeUp () {
for(int i = 0; i < 6; i++) {
lc.clearDisplay(0);
delay(100);
lc.setChar(0, 0, 0, false);
lc.setChar(0, 1, 0, true);
lc.setChar(0, 2, 0, true);
lc.setChar(0, 3, 0, false);
}
seconds_u = 0; //initialise all variables back to zero
seconds_t = 0;
minutes_u = 0;
minutes_t = 0;
delaySet(); //return to delay set function
}
void countDown() {
if(seconds_t < 1 && minutes_u == 0 && minutes_t == 0) { //user cannot select a delay of less than 10 seconds
error();
}
else {
while(1) { //infinite-loop this block while timer is running unless all digits equal to zero
static unsigned long lastTick = 0;
if(millis() - lastTick >= 100) {
lastTick = millis();
tenths_u--;
}
if(tenths_u < 0) {
seconds_u--;
tenths_u = 9;
updateDisplay();
}
if(seconds_u < 0) {
seconds_t--;
seconds_u = 9;
updateDisplay();
}
if(seconds_t < 0) {
minutes_u--;
seconds_t = 5;
updateDisplay();
}
if(minutes_u < 0 && minutes_t > 0) {
minutes_t--;
minutes_u = 9;
updateDisplay();
}
if(tenths_u == 9 && seconds_u == 9 && seconds_t == 5 && minutes_u == 9 && minutes_t == 0) { // display shifts to 9.59 minutes
udateDisplayUnderTenMinutes();
while(1) {
if(millis() - lastTick >= 100) {
lastTick = millis();
tenths_u--;
}
if(tenths_u < 0) {
seconds_u--;
tenths_u = 9;
udateDisplayUnderTenMinutes();
}
if(seconds_u < 0) {
seconds_t--;
seconds_u = 9;
udateDisplayUnderTenMinutes();
}
if(seconds_t < 0) {
minutes_u--;
seconds_t = 5;
udateDisplayUnderTenMinutes();
}
if(tenths_u == 9 && seconds_u == 9 && seconds_t == 5 && minutes_u == 0 && minutes_t == 0) { // display shifts to 59.99 seconds
// At this point the first digit flashes 0 -- !@#%?
updateDisplayUnderOneMinute();
while(1) {
if(millis() - lastTick >= 100) {
lastTick = millis();
tenths_u--;
updateDisplayUnderOneMinute();
}
if(tenths_u < 0) {
seconds_u--;
tenths_u = 9;
updateDisplayUnderOneMinute();
}
if(seconds_u < 0) {
seconds_t--;
seconds_u = 9;
updateDisplayUnderOneMinute();
}
if(tenths_u == 9 && seconds_u == 9 && seconds_t == 0 && minutes_u == 0 && minutes_t == 0) { //display shifts to 9.99 seconds
updateDisplayUnderTen();
while(1) {
if(millis() - lastTick >= 100) {
lastTick = millis();
tenths_u--;
updateDisplayUnderTen();
}
if(tenths_u < 0) {
seconds_u--;
tenths_u = 9;
updateDisplayUnderTen();
}
if (digitalRead(A2) == LOW){ //Button to stop clock - delay it actually
delay(10000);
}
if(tenths_u == 0 && seconds_u == 0 && seconds_t == 0 && minutes_u == 0 && minutes_t == 0) { //time up operation - flashing 00:00
timeUp();
} // time up if bracket
} //while bracket
} // display shifts to 9.99 if bracket
} // 59.99 while bracket
} // display shifts to 59.99 seconds if bracket
} // 9.59 while bracket
} // display shifts to 9.59 minutes if bracket
} ////infinite-loop this block while timer is running unless all digits equal to zero bracket
} //else bracket
} //void countdown bracket
void loop () {
delaySet ();
}
[/code]