I have adapted a sketch for a stopwatch using a 32 x 8 LED Matrix. If the fractional part is less than 10, it puts a zero before the digit. This works fine until roughly 5 minutes and 27 seconds after which a zero appears before the two digit number and does not go away. I know the code is a bit untidy as I have been tweaking it as I go in order to get it to work. Can anyone see why after 5 minutes and 27 seconds it prefixes the fractional part with a zero even when the fractional part is greater than 10? I am tearing my hair out trying to find the error! Any help would be very much appreciated.
Thank you in advance
Steve
/*
* Adapted Sketch October 2021
*/
// include the library code:
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include "SmallDigits.h"
#include <SafeString.h>
createSafeString(strOne, 20);
createSafeString(strTwo, 20);
createSafeString(strThree, 20);
createSafeString(strFour, 20);
createSafeString(strFive, 20);
createSafeString(strSix, 20);
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define DATA_PIN 11 // Pin for Arduino Nano 11
#define CS_PIN 3 // Pin for Arduino Nano 3
#define CLK_PIN 13 // Pin for Arduino Nano 13
// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Arbitrary output pins
// MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
int ledPin = 8; // LED connected to digital pin 8
int buttonPin = 2; // button on pin 2
int value = LOW; // previous value of the LED
int buttonState; // variable to store button state
int lastButtonState; // variable to store last button state
int blinking; // condition for blinking - timer is timing
int frameRate = 100; // the frame rate (frames per second) at which the stopwatch runs - Change to suit
long interval = (1000/frameRate); // blink interval
long previousMillis = 0; // variable to store last time LED was updated
long startTime; // start time for stop watch
long elapsedTime; // elapsed time for stop watch
int fractional; // variable used to store fractional part of Frames
int fractionalSecs; // variable used to store fractional part of Seconds
int fractionalMins; // variable used to store fractional part of Minutes
int elapsedFrames; // elapsed frames for stop watch
int elapsedSeconds; // elapsed seconds for stop watch
int elapsedMinutes; // elapsed Minutes for stop watch
void setup()
{
Serial.begin(57600);
P.begin(); // intialise the LED Matrix.
P.setIntensity(5); // Set LED Matrix Brightness
P.print("Start!"); // print opening message
pinMode(ledPin, OUTPUT); // sets the digital pin as output
pinMode(buttonPin, INPUT); // not really necessary, pins default to INPUT anyway
digitalWrite(buttonPin, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to ground.
digitalWrite(ledPin, HIGH); // LED Check
delay(1000);
P.setFont(SmallDigits);
}
void loop(){
digitalWrite(ledPin, LOW); // Initiate LED and Step Pin States
buttonState = digitalRead(buttonPin); // Check for button press, read the button state and store
// check for a high to low transition if true then found a new button press while clock is not running - start the clock
if (buttonState == LOW && lastButtonState == HIGH && blinking == false){
startTime = millis(); // store the start time
blinking = true; // turn on blinking while timing
delay(10); // short delay to debounce switch
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
}
// check for a high to low transition if true then found a new button press while clock is running - stop the clock and report
else if (buttonState == LOW && lastButtonState == HIGH && blinking == true){
blinking = false; // turn off blinking, all done timing
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
// Routine to report elapsed time
elapsedTime = millis() - startTime; // store elapsed time
elapsedMinutes = (elapsedTime / 60000L); // divide by 60000 to convert to minutes - then cast to an int to print
elapsedSeconds = (elapsedTime / 1000L); // divide by 1000 to convert to seconds - then cast to an int to print
elapsedFrames = (elapsedTime / interval); // divide by 100 to convert to 1/100 of a second - then cast to an int to print
fractional = (int)(elapsedFrames % frameRate); // use modulo operator to get fractional part of 100 Seconds
fractionalSecs = (int)(elapsedSeconds % 60L); // use modulo operator to get fractional part of 60 Seconds
fractionalMins = (int)(elapsedMinutes % 60L); // use modulo operator to get fractional part of 60 Minutes
strOne = fractionalSecs;
strFour = 0;
strSix = 0;
if(fractionalSecs < 10){
strOne = strFour += fractionalSecs;
}
strFive = " ";
strTwo = fractionalMins;
strTwo = strFive += strTwo;
strThree = fractional;
if(fractional < 10){
strThree = strSix += strThree;
}
strTwo = strTwo += ":";
strTwo = strTwo += strOne;
strTwo = strTwo += ":";
strTwo = strTwo += strThree;
Serial.println(strTwo);
P.print(strTwo);
delay(10);
}
else{
lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time
}
// run commands at the specified time interval
// blink routine - blink the LED while timing
// check to see if it's time to blink the LED; that is, the difference
// between the current time and last time we blinked the LED is larger than
// the interval at which we want to blink the LED.
if ( (millis() - previousMillis > interval) ) {
if (blinking == true){
previousMillis = millis(); // remember the last time we blinked the LED
digitalWrite(ledPin, HIGH); // Pulse the LED for Visual Feedback
elapsedTime = millis() - startTime; // store elapsed time
elapsedMinutes = (elapsedTime / 60000L); // divide by 60000 to convert to minutes - then cast to an int to print
elapsedSeconds = (elapsedTime / 1000L); // divide by 1000 to convert to seconds - then cast to an int to print
elapsedFrames = (elapsedTime / interval); // divide by 40 to convert to 1/25 of a second - then cast to an int to print
fractional = (int)(elapsedFrames % frameRate);// use modulo operator to get fractional part of 25 Frames
fractionalSecs = (int)(elapsedSeconds % 60L); // use modulo operator to get fractional part of 60 Seconds
fractionalMins = (int)(elapsedMinutes % 60L); // use modulo operator to get fractional part of 60 Minutes
strOne = fractionalSecs;
strFour = 0;
strSix = 0;
if(fractionalSecs < 10){
strOne = strFour += fractionalSecs;
}
strFive = " ";
strTwo = fractionalMins;
strTwo = strFive += strTwo;
strThree = fractional;
if(fractional < 10){
strThree = strSix += strThree;
}
strTwo = strTwo += ":";
strTwo = strTwo += strOne;
strTwo = strTwo += ":";
strTwo = strTwo += strThree;
Serial.println(strTwo);
P.print(strTwo);
delay(10);
}
else{
digitalWrite(ledPin, LOW); // turn off LED when not blinking
}
}
}