Help With Recording Longest Time

Okay so I acknowledge that this one will be a bit unconventional. I'm working on a game to record how long someone can hang on a bar. To record the length of the hang time, I just set up a counter that goes off every 100ms while the switch for the hang bar is pressed. So far this works great for reporting the current time.

I'm displaying the scores on two Adafruit 64x32 matrix displays so I'm using a Metro M0 Express to report the scores to each display.

They're sharing the switch that starts the timer and when both are set up to display the current score, they're totally synced up. However, when I try to record the high score (longest hanging time), I get a longer time than the current time. The code for both is identical, except that the the high score is reset to zero in the current time display. For the high score, I just comment it out. Code is below:

#include <RGBmatrixPanel.h>

#define CLK  8 
#define OE   9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3

RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false, 64);

float highScore;

float milTime = 0;

int hitPin = 12;

int hangBar = 11;

int mode = 2;

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

  matrix.begin();

  pinMode(hangBar, INPUT_PULLUP);
  pinMode(hitPin, OUTPUT);

  digitalWrite(hitPin, HIGH);

  matrix.setTextColor(matrix.Color333(0,0,7));
  matrix.setTextSize(2);
  matrix.setCursor(3, 9);
  matrix.print(0);
  matrix.print(0);
  matrix.print(0);
  matrix.print(".");  
  matrix.println(0);

}

void loop() {

  switch (mode) {

  case 1:

    while (digitalRead(hangBar) == LOW) {
    
      if (milTime < 99) {
          milTime = (milTime + 1);
          digitalWrite(hitPin, LOW);
          delay(50);
          digitalWrite(hitPin, HIGH);
          Serial.println(milTime);
          delay(50);


          
          if (milTime > highScore) {
          highScore = (highScore + 1);
          matrix.setTextSize(2);
          matrix.fillScreen(0);
          matrix.setCursor(3, 9);
          matrix.print(0);
          matrix.print(0);
          matrix.println(highScore / 10, 1);
          }
          
      } else if (milTime >= 99) {
          milTime = (milTime + 1);
          digitalWrite(hitPin, LOW);
          delay(50);
          digitalWrite(hitPin, HIGH);
          Serial.println(milTime);
          delay(50);

          if (milTime > highScore) {
          highScore = (highScore + 1);
          matrix.setTextSize(2);
          matrix.fillScreen(0);
          matrix.setCursor(3, 9);
          matrix.print(0);
          matrix.println(highScore / 10, 1);
          }
          
      } else if (milTime >= 999) {
          milTime = (milTime + 1);
          digitalWrite(hitPin, LOW);
          delay(50);
          digitalWrite(hitPin, HIGH);
          Serial.println(milTime);
          delay(50);

          if (milTime > highScore) {
          highScore = (highScore + 1);
          matrix.setTextSize(2);
          matrix.fillScreen(0);
          matrix.setCursor(3, 9);
          matrix.println(highScore / 10, 1);
          }
          
  }

      if (digitalRead(hangBar) == HIGH) {
        mode = 2;
        break;
      }
    }

  case 2:
      if (digitalRead(hangBar) == LOW) {
        mode = 1;
        milTime = 0;
        highScore = 0;
        break;
      }
  }

}

What does "hitPin" do?

I was using "hitPin" to have one Metro trigger the other's input pin before I just connected both to the same switch. I moved away from it when I first encountered the different readings to see if that was causing the delay, but it didn't change anything.

Not certain if this answers your question. Are you trying to compare the values from two different Arduinos or the stored value in each?

#include <RGBmatrixPanel.h>

#define CLK  8 
#define OE   9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3

RGBmatrixPanel 
    matrix(A, B, C, D, CLK, LAT, OE, false, 64);

unsigned long 
    timeHang,
    timeHangTotal,
    highestHangtime;

const byte hitPin = 12;
const byte hangBar = 11;

#define HB_WAIT         0
#define HB_TIMING       1
#define HB_RELEASED     2

void setup() 
{
    Serial.begin(9600);
    Serial1.begin(9600);

    matrix.begin();

    pinMode(hangBar, INPUT_PULLUP);
    pinMode(hitPin, OUTPUT);

    digitalWrite(hitPin, HIGH);

    matrix.setTextColor(matrix.Color333(0,0,7));
    matrix.setTextSize(2);
    matrix.setCursor(3, 9);
    matrix.print(0);
    matrix.print(0);
    matrix.print(0);
    matrix.print(".");  
    matrix.println(0);
    Serial.println( "000.0" );

}//setup

void UpdateDisplay( unsigned long elapsed, bool bForce )
{
    char
        szStr[10];
    static int
        last_tenths = -1;
    int
        secs,
        tenths;

    //round the "hundredths" up so the tenths report accurately
    elapsed = elapsed + 50;
    
    //we can only display 999.9
    if( elapsed > 999900ul )
        elapsed = 999900ul;

    //compute seconds and tenths from the elapsed time
    secs = (int)(elapsed / 1000);
    tenths = (int)(elapsed - (secs * 1000)) / 100;

    //if we somehow manage to exceed 999.9 seconds, constrain it here
    if( secs > 999 )
    {
        secs = 999;
        tenths = 9;
        
    }//if

    //only update the display if
    //  - the tenths digit has changed, or
    //  - the force flag is set (which means we ignore the tenths being the same; used for flashing
    //    the high score at the end
    if( (tenths != last_tenths) || bForce )
    {
        last_tenths = tenths;
        sprintf( szStr, "%03d.%d", secs, tenths );
        
        Serial.println( szStr );    //debug
        
        matrix.setTextSize(2);      //send it to the screen
        matrix.fillScreen(0);
        matrix.setCursor(3, 9);
        matrix.println( szStr );        
        
    }//if
    
}//UpdateDisplay
    
void loop() 
{
    static bool
        bDispState;
    static unsigned long
        timeHangHighest;
    static unsigned long
        timeHang;
    static byte
        numFlashes;
    static byte
        stateHangBar = HB_WAIT;

    switch( stateHangBar )
    {
        case    HB_WAIT:
            //waiting for someone to hang on the bar
            if( digitalRead( hangBar ) == LOW )
            {
                //someone's one it now; log the start time and move to the timing state            
                timeHang = millis();
                stateHangBar = HB_TIMING;
                
            }//if
            
        break;
    
        case    HB_TIMING:
            //each time through calculate the elapsed time since switch closed
            timeHangTotal = millis() - timeHang;

            //check if it's higher than the current highest
            if( timeHangTotal > timeHangHighest )
                timeHangHighest = timeHangTotal;

            //if the person let go...
            if( digitalRead( hangBar ) == HIGH )
            {
                //set up for the released state    
                bDispState = true;                      //display is on now
                if( timeHangHighest == timeHangTotal )  //if the new value is the highest, set up to flash 5 times                    
                {
                    timeHang = millis();                    //this is used to time the flashes in state released
                    numFlashes = 5;
                    stateHangBar = HB_RELEASED;
                    
                }//if
                else
                    stateHangBar = HB_WAIT;             //if a new highest was not set, no flash, just go wait again
                                    
            }//if
            
            UpdateDisplay( timeHangTotal, false );      //otherwise, update the display each pass
            
        break;

        case    HB_RELEASED:
            //bar was released and we're flashing; 
            //if someone is hanging again, start timing the new session
            if( digitalRead( hangBar ) == LOW )
            {
                timeHang = millis();
                stateHangBar = HB_TIMING;
                
            }//if

            //if we're still flashing, handle that
            if( numFlashes )
            {
                if( millis() - timeHang > 400 )         //flashes every 400mS
                {
                    //set up next flash                
                    timeHang = millis();
                    //if display is on now...
                    if( bDispState == true )
                    {
                        //clear it and set up for next time                    
                        Serial.println( "" );   //prints a blank line to serial mon to simulate flashing
                        matrix.fillScreen(0);
                        bDispState = false;
                      
                    }//if
                    else
                    {
                        //display is off now; update display with force-flag set
                        UpdateDisplay( timeHangTotal, true );
                        bDispState = true;
                        numFlashes--;           //when this hits zero, we stop flashing and leave value on screen
                        
                    }//else
                   
                }//if
                
            }//if
            else           
                stateHangBar = HB_WAIT;         //when flashing is done, go wait for another go-round
            
        break;
        
    }//switch
    
}//loop