Monday, March 23, 2015

Controlling 8 X 8 LED Matrix With the MAX7219

At first it was annoyingly difficult to get this LED matrix working properly but after putting it down for a month and coming back to it I suddenly have it working. We have a spiral staircase leading up to a Mac lab and my office and the student and teacher traffic always leads to an annoying jam when people
start up and down the staircase at the same time. So I have it in mind to use two of these, running off 2 MAX 7219's, to let people know if the coast is clear or not at either end, probably using a couple sharp distance sensors.
The LED matrix came from Tinkersphere, the MAX 7219 from Sparkfun. This tutorial was the ticket this time around, whereas many other tutorials I tried when I was just getting started never seemed to work. This reference page for the ledControl library is helpful, too. What still confuses me is this LED matrix is supposedly common-anode and the MAX7219 is supposed to drive common-cathode matrices. It's possible, from one forum source, that this only matters in the case of less of more than one color, though I swear what I finally got hung up on when I first had to put this project down was the leds were doing the opposite of what I was programming them to do, such as lighting up when set to false and turning off when set to true, so it seemed they were off when high, on when low. Anyway, all is good now.
Here is the code for the beginnings of a program for displaying alphanumeric characters.

#include "LedControl.h" //  need the library
LedControl lc=LedControl(12,11,10,1); // lc is our object
// pin 12 is connected to the MAX7219 pin 1
// pin 11 is connected to the CLK pin 13
// pin 10 is connected to LOAD pin 12
// 1 as we are only using 1 MAX7219
int col = 0;
void setup()
{
  // the zero refers to the MAX7219 number, it is zero for 1 chip
  lc.shutdown(0,false);// turn off power saving, enables display
  lc.setIntensity(0,4);// sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
}
void loop()
{
  letter('a',500);
  letter('b',500);
}

void letter(char alpha, int duration)
{
  lc.clearDisplay(0);
  switch(alpha) {
    case 'a':
    lc.setColumn(0,4,B00011000);
    lc.setColumn(0,5,B00100100);
    lc.setColumn(0,6,B00100100);
    lc.setColumn(0,7,B00011100);
    break;
    case 'b':
    lc.setColumn(0,1,B00100000);
    lc.setColumn(0,2,B00100000);
    lc.setColumn(0,3,B00100000);
    lc.setColumn(0,4,B00111000);
    lc.setColumn(0,5,B00100100);
    lc.setColumn(0,6,B00100100);
    lc.setColumn(0,7,B00111000);
    break;
  }
  delay(duration);
}
And here are potential stop and go animations for the above-mentioned project:

#include "LedControl.h" //  need the library
LedControl lc=LedControl(12,11,10,1); // lc is our object
// pin 12 is connected to the MAX7219 pin 1
// pin 11 is connected to the CLK pin 13
// pin 10 is connected to LOAD pin 12
// 1 as we are only using 1 MAX7219
int col = 0;
void setup()
{
  // the zero refers to the MAX7219 number, it is zero for 1 chip
  lc.shutdown(0,false);// turn off power saving, enables display
  lc.setIntensity(0,4);// sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
}
void loop()
{
  arrow();
  x();
}

void arrow()
{
  for(int i = 0; i<16;i++)
  {
    setOne();
    setTwo();
    setThree();
    setFour();
    if(col < 15)
    {
      col++;
    }
    else
    {
      col = 0;
    }
    delay(80);
  }
}

void setOne()
{
  lc.setLed(0,col,3,true);
  lc.setLed(0,col,4,true);
  if(col > 3)
  {
    lc.setLed(0,col-8,3,false);
    lc.setLed(0,col-8,4,false);
  }
}

void setTwo()
{
  if(col > 0)
  {
    lc.setLed(0,col-1,2,true);
    lc.setLed(0,col-1,5,true);
    if(col > 1)
    {
      lc.setLed(0,col-2,2,false);
      lc.setLed(0,col-2,5,false);
    }
  }
}

void setThree()
{
  if(col > 1)
  {
    lc.setLed(0,col-2,1,true);
    lc.setLed(0,col-2,6,true);
    if(col > 2)
    {
      lc.setLed(0,col-3,1,false);
      lc.setLed(0,col-3,6,false);
    }
  }
}

void setFour()
{
  if(col > 2)
  {
    lc.setLed(0,col-3,0,true);
    lc.setLed(0,col-3,7,true);
    if(col > 3)
    {
      lc.setLed(0,col-4,0,false);
      lc.setLed(0,col-4,7,false);
    }
  }
}

void x()
{
  for(int t = 0; t< 5;t++)
  {
    for(int i = 0; i<8; i++)
    {
      lc.setLed(0,i,i,true);
    }
    for(int h = 7; h>=0;h--)
    {
      lc.setLed(0,h,7-h,true);
    }
    delay(200);
    for(int j = 0; j<8; j++)
    {
      lc.setLed(0,j,j,false);
    }
    for(int h = 7; h>=0;h--)
    {
      lc.setLed(0,h,7-h,false);
    }
    delay(200);
  }
  delay(500);
}
And again

No comments :