Unable to display consecutive scores on 8 digit 7 segment MAX7219 LED display

Hi, I am trying to create a game with my Arduino Uno, with an 8 digit 7 segment MAX7219 display that shows the score at the end of the game, followed by the high score. I have created a function called updateShotCounter, and call it twice within the same subroutine. The problem, is that it only updates the display the once, and even after a delay, when function is called again to display the highscore, it doesn't update.

Subroutine where I display game score and high score.

  updateShotCounter(shotcounter);
      delay(5000);
      updateShotCounter(highscore);

This is my function to update scores to 8 digit display

  void updateShotCounter(int shotcounter) {
       
        int dig2 = 1;
        int digCnt2 = 0;
      
        // if number is greater than zero
        if (shotcounter > 0) {
         
          // count # of digits
          while (shotcounter >= dig2) {
            dig2 *= 10;
            digCnt2++;
      
          }  // while
      
      for (int d2 = 0; d2 < digCnt2; d2++) {
            // wite this digit
            lc.setDigit(0, d2+4, byte(shotcounter % 10), false);
      
            // writing from right to left
            shotcounter /= 10;
      
          }  // while
      }
      }

hard to answer without seeing all of the code

i'll guess that there isn't a delay after the call updateShotCounter(highscore); so it it quickly overwritten with "shotcounter"

Here's all the code, thanks in advance!


#include <LedControl.h>
#define BUTTON_PIN 3
#define BUTTON_PIN2 10
#define Buzzer 8


int counter = 0;  // initial value for countdown
int shotcounter = 0;
int currentState = 0;
int previousState = 0;
int highscore = 0;

//variables for long button press reset
const int LONG_PRESS_TIME = 1000; // 1000 ms
int lastState = LOW; // the previous state from input pin
int currentState2;   // the current reading from input pin
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;


unsigned long nextTick;  // time to update counter

// Arduino Pins:          DIN, Clk, CS, no.of devices is 1
LedControl lc = LedControl(4, 6, 5, 1);

void setup() {

  Serial.begin(9600);

  // Initialize the MAX7219 device
  lc.shutdown(0, false);  // Enable display
  lc.setIntensity(0, 8);  // Set brightness level (0 is min, 15 is max)
  lc.clearDisplay(0);     // Clear display register


  // first tick will happen right away
  nextTick = millis();

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(Buzzer, OUTPUT);
}

void loop() {

  // read state of the button:
  currentState2 = digitalRead(BUTTON_PIN);

  if(lastState == HIGH && currentState2 == LOW)
    pressedTime = millis();
  else if(lastState == LOW && currentState2 == HIGH) 
    releasedTime = millis();

  long pressDuration = releasedTime - pressedTime;

  if(pressDuration > LONG_PRESS_TIME) {
    Serial.println("A long press is detected");
   // highscore = 0; //reset highscore

}

// save the last state
  lastState = currentState2;
  
  // tick timer
  updateTimer();
  updateShotCounter(shotcounter);
  byte buttonState = digitalRead(BUTTON_PIN);
  byte buttonState2 = digitalRead(BUTTON_PIN2);

//Serial.println(buttonState2);

  if (buttonState == LOW) {
    counter = 60;  //reset counter
    shotcounter = 0; //reset shot counter
  }

  if (buttonState2 == LOW) {
  currentState = 1;
  }
  else {
  currentState = 0;
  }  
  delay(300);
  if(currentState != previousState) {
 if(currentState == 1){
   shotcounter = shotcounter + 1;
   Serial.println(shotcounter);
   
 }
 }   

   if (shotcounter > highscore) {

    highscore = shotcounter;

  }

}

// updateTimer
//
// Call to tick timer
//
void updateShotCounter(int shotcounter) {
 
  int dig2 = 1;
  int digCnt2 = 0;

  // if number is greater than zero
  if (shotcounter > 0) {
   
    // count # of digits
    while (shotcounter >= dig2) {
      dig2 *= 10;
      digCnt2++;

    }  // while

for (int d2 = 0; d2 < digCnt2; d2++) {
      // wite this digit
      lc.setDigit(0, d2+4, byte(shotcounter % 10), false);

      // writing from right to left
      shotcounter /= 10;

    }  // while


}

}


void updateTimer() {
  // if it's time to update counter
  if (millis() >= nextTick) {

    // if there are any seconds left
    if (counter >= 0) { //was >= 0
      // clear display
      lc.clearDisplay(0);

      // write next number
      printNum(counter);

      // decrement counter
      counter--;

    }  // if


    // set next update time
    nextTick = millis() + 1000;

  }  //if
}

// printNum
//
// Print passed number to display (only works with positve numbers)
//
void printNum(int num) {
  int dig = 1;
  int digCnt = 0;

  // if number is greater than zero
  if (num > 0) {

    // count # of digits
    while (num >= dig) {
      dig *= 10;
      digCnt++;

    }  // while

    // while more digits
    for (int d = 0, dig = 10; d < digCnt; d++) {
      // wite this digit
      lc.setDigit(0, d, byte(num % 10), false);

      // writing from right to left
      num /= 10;

    }  // while
    
  } else {

    // just write a zero
    
   
    
  lc.setDigit(0, 0, (byte)0, false);
  updateShotCounter(shotcounter);
  Serial.print("My score is: ");
  Serial.println(shotcounter);
 

  digitalWrite (Buzzer, HIGH);
  delay(100);
  digitalWrite (Buzzer, LOW);
  delay(100);
  digitalWrite (Buzzer, HIGH);
  delay(100);
  digitalWrite (Buzzer, LOW);
  delay(100);
  digitalWrite (Buzzer, HIGH);
  delay(100);
  digitalWrite (Buzzer, LOW);
  delay(5000);
 
 lc.setDigit(0, 0, (byte)0, false);
 lc.setDigit(0, 1, (byte)6, false);
 

Serial.print("My highscore is: ");
Serial.println(highscore);

updateShotCounter(highscore);


  }  // else
}

looks like updateTimer() attempts to call printNum() only every second, but it looks like printNum() has several delays and take > 5 seconds to complete so that printNum() is called every time updateTimer() is called

try adding a delay after displaying highScore to see if that is the problem and take another look at the time flow of your code

Thanks, that did the trick! I went into my void loop(), and added an if statement to only update the score via updateShotCounter() if game hasn't ended.
Cheers!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.