Need help with DMD stopwatch code

Hi all

Im very new to this coding business so apologies if this is a simpleton question but my problem is as follows.

I am trying to display a stopwatch with minuets and seconds on a 512-LED DMD. Being new i have done a lot of going trough code change a bit and see if it works and then try to mash code together.

so the following code is what i have so far. it seems to work correctly in the serial monitor minus a glitch here and there however when the timer is started it displays 00:60 on the DMD and this does not change at all. if anyones knows how to get this to display the elapsed time that would be greatly appreciated.

stopwatch_works.ino (7.88 KB)

Incase you cant open the above folder here is the code:

#include <SPI.h> //SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <DMD.h> //
#include <TimerOne.h> //
#include “SystemFont5x7.h”

#define StopButton 4 // button on pin 4
#define StartButton 2 // button on pin 2

int buttonStateStart; // variable to store button state
int lastButtonStateStart; // variable to store last button state
int buttonStateStop; // variable to store button state
int lastButtonStateStop; // variable to store last button state
int fractional;
int fractionalOld;
byte StopWatchRun = LOW;
unsigned long start, finished, elapsed;
//Fire up the DMD library as dmd
DMD dmd(1,1);
unsigned int ui;
int fontNr=0;
int minute=0;
int sec=0;
byte secOver = HIGH;
int i=0;

/--------------------------------------------------------------------------------------
Interrupt handler for Timer1 (TimerOne) driven DMD refresh scanning, this gets
called at the period set in Timer1.initialize();
--------------------------------------------------------------------------------------
/
void ScanDMD()
{
dmd.scanDisplayBySPI();
}

/--------------------------------------------------------------------------------------
setup
Called by the Arduino architecture before the main loop begins
--------------------------------------------------------------------------------------
/
void setup(void)
{
Serial.begin(9600);

pinMode(StartButton, INPUT); // the start button
pinMode(StopButton, INPUT); // the stop button
Serial.println(“Press 1 for Start/reset, 2 for elapsed time”);

//initialize TimerOne’s interrupt/CPU usage used to scan and refresh the display
Timer1.initialize( 5000 ); //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
Timer1.attachInterrupt( ScanDMD ); //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()

//clear/init the DMD pixels held in RAM
dmd.clearScreen( true ); //true is normal (all pixels off), false is negative (all pixels on)
dmd.selectFont(System5x7);
}

/--------------------------------------------------------------------------------------
loop
Arduino architecture main loop
--------------------------------------------------------------------------------------
/
void loop(void)
{
buttonStateStart = digitalRead(StartButton);
buttonStateStop = digitalRead(StopButton);

if (buttonStateStart == LOW && lastButtonStateStart == HIGH)
{
start = millis();
delay(5); // for debounce
Serial.println(“Started…”);
lastButtonStateStart = buttonStateStart;
StopWatchRun = HIGH;
}
else if (buttonStateStop == LOW && lastButtonStateStop == HIGH)
{
finished = millis();
delay(5); // for debounce
displayResult();
lastButtonStateStop = buttonStateStop;
StopWatchRun = LOW;
}
else
{
if (StopWatchRun == HIGH)
{
fractional = (int)(millis() % 1000L);
if (fractional < fractionalOld)
{
finished = millis();
displayResult();

if (secOver == 1)
{
minute=minute–;
sec=60;
secOver = 0;
}
sec=sec–;
ui = minute * 100 + sec;
Blink(ui);
if(sec==0)
{
secOver = 1;
}
}
}

lastButtonStateStart = buttonStateStart;
lastButtonStateStop = buttonStateStop;
fractionalOld = fractional;
}
}

/--------------------------------------------------------------------------------------
Display result in serial monitor
--------------------------------------------------------------------------------------
/
void displayResult()
{
float h, m, s, ms;
unsigned long over;

elapsed = finished - start;
h = int(elapsed / 3600000);
over = elapsed % 3600000;
m = int(over / 60000);
over = over % 60000;
s = int(over / 1000);
ms = over % 1000;
Serial.print("Raw elapsed time: ");
Serial.println(elapsed);
Serial.print("Elapsed time: ");
Serial.print(h, 0);
Serial.print("h ");
Serial.print(m, 0);
Serial.print("m ");
Serial.print(s, 0);
Serial.print("s ");
Serial.print(ms, 0);
Serial.println(“ms”);
Serial.println();
}

/--------------------------------------------------------------------------------------
Show clock colon overlay on and off
--------------------------------------------------------------------------------------
/
void Blink(unsigned int uiTime)
{
if(i)
{
ShowClockNumbers( uiTime, false );
i = 0;
}
else
{
ShowClockNumbers( uiTime, true );
i=1;
}
}

/--------------------------------------------------------------------------------------
Show clock numerals on the screen from a 4 digit time value, and select whether the
flashing colon is on or off
--------------------------------------------------------------------------------------
/
void ShowClockNumbers( unsigned int uiTime, byte bColonOn )
{
dmd.clearScreen(true);
dmd.drawChar( 3, 3,‘0’+((uiTime%10000)/1000), GRAPHICS_NORMAL ); // thousands
dmd.drawChar( 9, 3, ‘0’+((uiTime%1000) /100), GRAPHICS_NORMAL ); // hundreds
dmd.drawChar( 18, 3, ‘0’+((uiTime%100) /10), GRAPHICS_NORMAL ); // tens
dmd.drawChar( 24, 3, ‘0’+ (uiTime%10), GRAPHICS_NORMAL ); // units
if( bColonOn )
dmd.drawChar( 14, 3, ‘:’, GRAPHICS_OR ); // clock colon overlay on
else
dmd.drawChar( 14, 3, ‘:’, GRAPHICS_NOR ); // clock colon overlay off
}

    pinMode(StartButton, INPUT); // the start button
    pinMode(StopButton, INPUT); // the stop button

How ARE the switches wired? Using INPUT_PULLUP makes for much simpler wiring.

byte StopWatchRun = LOW;

A boolean, with values of true or false, would make a lot more sense.

float h, m, s, ms;

So that you can show that it is 3.14.76654 seconds after 3451.56:-26628.8673?

  h = int(elapsed / 3600000);

Why are you casting the result to an int, so you can store the value in a float? Use the proper type! Here's a hint. It isn't float!

  Serial.print(h, 0);

Stupid. There is no other word for this.

            minute=minute--;

This is undefined behavior. Learn what the -- operator does, and study examples of how to PROPERLY use it.