Arduino

Testing the MAX7221 - 24 Hour Countdown Sketch

Arduino - MAX7221 - 24 Hour Countdown ClockI recently built a Serially Interfaced, 8-Digit, 7-Segment, LED Display module to use with my Arduino. Of course once you have built such a module you need something to run on it to test it functions correctly. What was needed was an Arduino sketch that wrote values to each of the 8 digits of the display and also that would amuse me.

Think 7-Segment, LED Displays and you cannot help but think of the standard displays of heinous bombs in action films or that frantic TV series 24. I could think of nothing more fitting to test my new display than a countdown timer! Here is the code and a short video of said timer in action.

Hopefully the commenting in the code is sufficient to explain it’s function, if you have any questions please leave them in the comments…



// ********************************************************************
// *                       24 Hour Coundown                           *
// *  Because if you have just built a MAX7221/MAX7219 Based 8 Digit, *
// *  7 Segment, LED Display then you will want to test it somehow.   *
// *  Nothing says LED Segment display more than a TV/Film style      *
// *  countdown timer.                                                *
// *                                                                  *
// *  A silly test routine by Blair Thompson - www.justblair.co.uk    *
// ********************************************************************

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

// Lets start with the pinouts to the Max72xx 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 = 15;    // 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);        // once only, lets make the pins outputs 
  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 hourcountdown=24; hourcountdown>0; hourcountdown--){
    int x=hourcountdown;
    int hourones;
    int hourtens;
    hourones=x%10;
    hourtens=x/10%10;                                     // %10 divides by ten and extracts the remainder
    lc.setChar(chipId, row+5, 'h', false);                // Adds the letter H
    lc.setDigit(chipId, row+6, (byte) hourones, false);   // 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 minutecountdown=60; minutecountdown >0; minutecountdown--){
      int y=minutecountdown;
      int minuteones;
      int minutetens;
      minuteones=y%10;
      minutetens=y/10%10;                                   // %10 divides by ten and extracts the remainder
      lc.setDigit(chipId, row+3, minuteones, true);         // These two lines add the numerical hours
      lc.setDigit(chipId, row+4, minutetens, false);        // Deja Vu?

      //  And the seconds/tenths just to make it look exciting!
      for (int seccountdown=600; seccountdown>0; seccountdown--){
        int v=seccountdown;
        int tenths;
        int secones;
        int sectens;
        int thousands;
        tenths=v%10;                                         // Again divide by 10 and calculate the remainder
        secones=v/10%10;                                      
        sectens=v/100%10;
        thousands=v/1000%10;
        lc.setDigit(chipId, row, (byte) tenths, false);       
        lc.setDigit(chipId, row+1, (byte) secones, true);    // True in the arguments activates the dot...
        lc.setDigit(chipId, row+2, (byte) sectens, false);
        delay (flashDelay);
      }
    }
  }
}
// *** That's All Folks!!! Jack Bauer eat your heart out! ***

Click the image below to see it in action... Apologies, Spielburg I am not!

{youtube}3HeWLXY200Q|600|450|1{/youtube}