#include <MD_MAX72xx.h>
#include <MD_Parola.h>
// #include <SPI.h>
#include "Font_Data.h"
// #include <LibPrintf.h> // if using arduino
// change this pins to work with your board
#define MAX_DEVICES 4
#define CLK_PIN 13 // or SCK
#define DATA_PIN 11 // or MOSI
#define CS_PIN 12 // or SS
// Change this to work with your matrix - see video 1 you have 4 choices
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
int hh=0, mm=0, ss=0, ms=0;
bool timerStart = false;
char Buffer[3] = " "; // create a buffer to hold the numbers
#define bt_start 2
#define bt_stop 3
void setup() { // put your setup code here, to run once
pinMode(bt_start, INPUT_PULLUP);
pinMode(bt_stop, INPUT_PULLUP);
P.begin();
P.setIntensity(1); // keep it 3 or below as we are powering off the chip/usb
P.setFont(F4x7straight); // If you use the standard built in font the numbers on the matrix will jump around as the number 1 not as wide
// as the other numbers. Comment out this line to see what happens. I will have a video in the future to show
// how to create your own fonts or adjust the ones you find online.
noInterrupts(); // disable all interrupts
TCCR1A = 0; // set entire TCCR1A register to 0 //set timer1 interrupt at 1kHz // 1 ms
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // set timer count for 1khz increments
OCR1A = 1999; // = (16*10^6) / (1000*8) - 1
//had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler
// turn on CTC mode
TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11); // enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
interrupts(); // enable
}
void loop() {
if(digitalRead (bt_start) == 0){
ms=0, ss=0, mm=0, hh=0; // Reset stopwatch
timerStart = true; // Start stopwatch
delay(100);
}
if(digitalRead (bt_stop) == 0){
timerStart = false; // Stop stopwatch
delay(100);
}
if (P.displayAnimate()) {
sprintf(Buffer, "%02d%c%02d", mm, (':'), ss); // the number "1" is the number of digits that you want always shown on the screen for this font we can have 5
P.displayText(Buffer, PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
}
}
ISR(TIMER1_COMPA_vect){
if(timerStart == true)
{ms=ms+1;}
if(ms>999)
{ms=0;ss=ss+1;}
if(ss>59)
{ss=0; mm=mm+1;}
if(mm>59)
{mm=0; hh=hh+1;}
}