#include <MD_MAX72xx.h>
#include <MD_Parola.h>
#include "Font_Data.h"
// constants won't change. They're used here to set pin numbers:
const int BUTTON = 2; // the number of the pushbutton pin
const int PRESS_TIME = 1000; // Time for long press 1000 = 1 second
// change this pins to work with your board
#define MAX_DEVICES 4 //Number of LED Matrix
#define CLK_PIN 12 // or SCK
#define CS_PIN 11 // or SS
#define DATA_PIN 10 // or MOSI
// Change this to work with your matrix
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
int mm = 0, ss = 0, ms = 0;
bool timerStart = false;
char Buffer[10] = " "; // create a buffer to hold the numbers
// Variables will change:
int lastState = LOW; // the previous state from the input pin
int currentState; // the current reading from the input pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
bool isPressing = false;
bool isLongDetected = false;
void setup() {
pinMode(BUTTON, 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
unsigned long pressedTime = PRESS_TIME;
}
void loop() {
// read the state of the switch/button:
currentState = digitalRead(BUTTON);
if(lastState == HIGH && currentState == LOW) { // button is pressed
pressedTime = millis();
isPressing = true;
isLongDetected = false;
} else if(lastState == LOW && currentState == HIGH) { // button is released
isPressing = false;
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < PRESS_TIME && (timerStart == false)) {
timerStart = true; // Start stopwatch
delay(100);
}
else if( pressDuration < PRESS_TIME && (timerStart == true)) {
timerStart = false; // Stop stopwatch
delay(100);
}
}
if(isPressing == true && isLongDetected == false) {
long pressDuration = millis() - pressedTime;
if( pressDuration > PRESS_TIME ) {
timerStart = false; // Stop stopwatch
ms = 0, ss = 0, mm = 0; // Reset stopwatch
delay(100);
isLongDetected = true;
}
}
if (P.displayAnimate()) {
sprintf(Buffer, "%d:%02d:%02d", mm, ss, ms/10); // 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);
//Serial.println(Buffer);
}
// save the the last state
lastState = currentState;
}
ISR(TIMER1_COMPA_vect){
if(timerStart == true) ms ++;
if(ms > 999) {ms = 0; ss ++;}
if(ss > 59) {ss = 0; mm ++;}
if(mm > 9) timerStart = false;// Stop stopwatch
}