Modifying code to count up using eight digit, 7-seg displays, a MAX7219 and Uno

I’m having issues with a MAX7219 eight digit, 7- segment display paired with the Arduino.

My plan is to display an 8 digit “clock” of sorts. it can begin as (00:00:00:00), and count up as (HH:MM:SS:FF). HH=hours MM=minutes SS=seconds and FF=Frames. for frames, i would like the a sequence of 0 to 29, every second.

See this video for an example. timecode 3h - YouTube

I have an Arduino Uno, MAX7219 [(Purchased here http://embedded-lab.com/blog/?p=6862) ](http://(Purchased here http://embedded-lab.com/blog/?p=6862))

The code below was used perfectly with what I prototyped, but it runs in the wrong direction. I tried changing it, but the modulo (%) were giving me some issues.

#include <LedControl.h>    //We cant do anything without importing a suitable library!

// Lets start with the pinouts to the Max7219 led driver

int DIN = 12;              // Pin 1  on the Max72xx
int CLK = 11;              // Pin 13 on the Max72xx
int LOADCS = 10;           // Pin 12 on the Max72xx

int flashDelay = 100;      // delay in MS (100=1/10th second) 
int ledBrightness = 5;    // range is 0-15.  0=lowest, 15 = full power

// define the LedControl instance - if I want more I can setup lc2, lc3 etc
// ************************************************************************

LedControl lc=LedControl(DIN,CLK,LOADCS,1);    // DIN, CLK, Load/CS, 1 = only one chip MAX chip attached.

void setup()                    
{ 
  pinMode(DIN, OUTPUT); 
  pinMode(CLK, OUTPUT); 
  pinMode(LOADCS, OUTPUT); 

  // take pins out of power save mode.  No updates otherwise. 
  for(int index=0;index<lc.getDeviceCount();index++) { 
    lc.shutdown(index,false); 
  }

  lc.setIntensity(0,ledBrightness  ); //set the brightness 
}

void loop()                     // here comes the good stuff, the main loop!
{
  int row = 0;                  //Set the starting position
  int chipId = 0;               //This is not strictly reqd, but if using more than one display this will be needed

  // First lets display the hours

  for (int hourcountup=01; hourcountup>0; hourcountup--){
    int x=hourcountup;
    int hourones;
    int hourtens;
    hourones=x%10;
    hourtens=x/10%10;                                     // %10 divides by ten and extracts the remainder
    lc.setDigit(chipId, row+6, (byte) hourones, true);   // These two lines add the numerical hours 
    lc.setDigit(chipId, row+7, (byte) hourtens, false);   // This technique is repeated several times!

    //  Now the minutes
    for (int minutecountup=60; minutecountup>0; minutecountup--){
      int y=minutecountup;
      int minuteones;
      int minutetens;
      minuteones=y%10;
      minutetens=y/10%10;                                   // %10 divides by ten and extracts the remainder
      lc.setDigit(chipId, row+4, minuteones, true);         // These two lines add the numerical hours
      lc.setDigit(chipId, row+5, minutetens, false);        // Deja Vu?


      //  And the seconds/tenths
      for (int seccountup=60; seccountup>0; seccountup--){
        int v=seccountup;
        int secones;
        int sectens;
        secones=v%10;                                      
        sectens=v/10%10;
    //    thousands=v/1000%10;
    //    lc.setDigit(chipId, row+1, (byte) tenths, false);       
        lc.setDigit(chipId, row+2, (byte) secones, true);    // True in the arguments activates the dot...
        lc.setDigit(chipId, row+3, (byte) sectens, false);
        delay (flashDelay);
      }
    }
  }
}
// *** That's All Folks!!! Jack Bauer eat your heart out! ***
// code provided by Blair Thompson

Any help would be appreciated!

I just got the same LED control and was playing around with it. I’m not sure I understand exactly what you’re trying to do, but it seems like you want a “count up” clock. The code below is an approximation, but is not going to be accurate since I used delay() and just guessed at the delay period. It would work better using interrupts and an RTC, but this might help you with the code part of it. Note that I changed the pin assignments to match my setup. You’ll need to change those for yours.

#include <LedControl.h>    //We cant do anything without importing a suitable library!

// Lets start with the pinouts to the Max7219 led driver

int DIN = 10;              // Pin 1  on the Max72xx
int CLK = 13;              // Pin 13 on the Max72xx
int LOADCS = 11;           // Pin 12 on the Max72xx

int flashDelay = 100;      // delay in MS (100=1/10th second)
int ledBrightness = 5;    // range is 0-15.  0=lowest, 15 = full power

// define the LedControl instance - if I want more I can setup lc2, lc3 etc
// ************************************************************************

LedControl lc = LedControl(DIN, CLK, LOADCS, 8); // DIN, CLK, Load/CS, 1 = only one chip MAX chip attached.
int frame0, frame1;
int seconds0, seconds1;
int minutes0, minutes1;
int hours0, hours1;

void setup()
{
  pinMode(DIN, OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(LOADCS, OUTPUT);

  // take pins out of power save mode.  No updates otherwise.
  //for (int index = 0; index < lc.getDeviceCount(); index++) {
    lc.shutdown(0, false);
  //}
  Reset();
  lc.setDigit(0, 0, 0, false);       // These two lines add the numerical hours
  for (int i = 1; i < lc.getDeviceCount(); i++) {
    lc.setDigit(0, i, 0, (i % 2 == 0) ? true : false);       // These two lines add the numerical hours
  }

  lc.setIntensity(0, ledBrightness  ); //set the brightness
}

void loop()                     // here comes the good stuff, the main loop!
{

  BumpFrame();
  // First lets display the hours
}
// *** That's All Folks!!! Jack Bauer eat your heart out! ***
// code provided by Blair Thompson

void  BumpFrame()
{
  delay(30);    // Count to 29 from 0 every second
  
  lc.setDigit(0, 0, frame0++, false);       // These two lines add the numerical hours
  if (frame0 == 10) {
    frame1++;
    frame0 = 0;
  }
  if (frame1 < 3) {
    lc.setDigit(0, 1, frame1, false);       // These two lines add the numerical hours
  } else {
    frame1 = 0;
    lc.setDigit(0, 1, frame1, false);       // These two lines add the numerical hours
    BumpSeconds();
  }
}

void  BumpSeconds()
{
  seconds0++;
  if (seconds0 == 10) {
    seconds1++;
    seconds0 = 0;
  }
  lc.setDigit(0, 2, seconds0, true);       // These two lines add the numerical hours

  if (seconds1 < 6) {
    lc.setDigit(0, 3, seconds1, false);       // These two lines add the numerical hours
  } else {
    seconds1 = 0;
    seconds0 = 0;
    lc.setDigit(0, 3, seconds1, false);       // These two lines add the numerical hours
    BumpMinutes();
  }
}

void  BumpMinutes()
{
  minutes0++;
  if (minutes0 == 10) {
    minutes1++;
    minutes0 = 0;
  }
  lc.setDigit(0, 4, minutes0, true);       // These two lines add the numerical hours

  if (minutes1 < 6) {
    lc.setDigit(0, 5, minutes1, false);       // These two lines add the numerical hours
  } else {
    minutes1 = 0;
    minutes0 = 0;
    lc.setDigit(0, 5, minutes1, false);       // These two lines add the numerical hours
   BumpHours();
  }
}

void  BumpHours()
{
  hours0++;
  if (hours0 == 10) {
    hours1++;
    hours0 = 0;
  }
  lc.setDigit(0, 6, hours0, true);       // These two lines add the numerical hours

  if (hours1 < 6) {
    lc.setDigit(0, 7, hours1, false);       // These two lines add the numerical hours
  } else {
    hours1 = 0;
    hours0 = 0;
    Reset();
  }
}

void Reset()
{
  lc.setDigit(0, 0, 0, false);       // These two lines add the numerical hours
  for (int i = 1; i < lc.getDeviceCount(); i++) {
    lc.setDigit(0, i, 0, (i % 2 == 0) ? true : false);       // These two lines add the numerical hours
  }
}

Hey, look at that! You did it! thanks a bunch.

i may be asking to much, but would there be a way to add a pause button? like a momentary switch that could stop and start the time at the same place?

Thanks for the help BTW!!! :slight_smile:

That wouldn't be too hard. In loop(), you'd need to monitor the pin that the switch is connected to and when it is pressed, call my Reset() function. When you get a little more comfortable with programming the Arduino, you could use an interrupt to sense the button push.

Added:

I just reread what you asked and you didn't want to reset the clock to zero. In that case, when your code senses a button press, call a function that simply goes into a while loop looking at the switch pin. When it sees the pin is released, it exits your function and returns to loop(). Since none of the variables have changed, it should pick up where it left off. You may need to use interrupts to do this properly.