Thursday, February 5, 2015

Beating Heart animation on 8x8 LED Matrix + Arduino Uno

This example run on Arduino uno, animate beating heart on 8x8 LED Matrix.



Example code:
// 2-dimensional array of row pin numbers:
const int row[8] = {
  2, 7, 19, 5, 13, 18, 12, 16
};

// 2-dimensional array of column pin numbers:
const int col[8] = {
  6, 11, 10, 3, 17, 4, 8, 9
};

// 2-dimensional array of pixels:
int pixels[8][8];

int count = 1000;

char str[] = "FABCDEDCBA";
int strLen = sizeof(str);
int ptrChar = 0;


typedef bool charMapType[8][8];

const charMapType charBlank = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType heart0 = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType heart1 = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 1, 1, 1, 1, 0, 0},
  {0, 0, 1, 1, 1, 1, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType heart2 = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 1, 0, 0, 1, 1, 0},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 0, 1, 1, 1, 1, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0}
};

const charMapType heart3 = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 1, 1, 0, 0, 1, 1, 0},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 0, 1, 1, 1, 1, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType heart4 = {
  {0, 1, 1, 0, 0, 1, 1, 0},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {0, 1, 1, 1, 1, 1, 1, 0},
  {0, 0, 1, 1, 1, 1, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0}
};

const charMapType *charMap[6] = {&heart0, &heart1, &heart2, &heart3, &heart4, &charBlank};

void setup() {
  // initialize the I/O pins as outputs
  // iterate over the pins:
  for (int thisPin = 0; thisPin < 8; thisPin++) {
    // initialize the output pins:
    pinMode(col[thisPin], OUTPUT);
    pinMode(row[thisPin], OUTPUT);
    // take the col pins (i.e. the cathodes) high to ensure that
    // the LEDS are off:
    digitalWrite(col[thisPin], HIGH);
  }

  //setupScreen();
  setupChar();

}

void loop() {

  // draw the screen:
  refreshScreen();
  
  if(count-- == 0){
    count = 1000;
    setupChar();
  }

}

void setupChar(){
  char c = str[ptrChar];
  int offset = c - 'A';
  
  const charMapType *cMap = charMap[offset];
  //charMapType *cMap = &charDummy;
  
  for (int x = 0; x < 8; x++) {
    for (int y = 0; y < 8; y++) {
      bool v = (*cMap)[x][y];
      
      if(v){
        pixels[x][y] = LOW;
      }else{
        pixels[x][y] = HIGH;
      }
    }
  }
  
  ptrChar++;
  if(ptrChar>=strLen-1){
    ptrChar = 0;
  }

}

void refreshScreen() {
  // iterate over the rows (anodes):
  for (int thisRow = 0; thisRow < 8; thisRow++) {
    // take the row pin (anode) high:
    digitalWrite(row[thisRow], HIGH);
    // iterate over the cols (cathodes):
    for (int thisCol = 0; thisCol < 8; thisCol++) {
      // get the state of the current pixel;
      int thisPixel = pixels[thisRow][thisCol];
      // when the row is HIGH and the col is LOW,
      // the LED where they meet turns on:
      digitalWrite(col[thisCol], thisPixel);
      // turn the pixel off:
      if (thisPixel == LOW) {
        digitalWrite(col[thisCol], HIGH);
      }
    }
    // take the row pin low to turn off the whole row:
    digitalWrite(row[thisRow], LOW);
  }
}


16 comments:

  1. Is it a common anode or common cathoda ....man?...rpy asap

    ReplyDelete
  2. common anode pin 1 on the bottom left

    ReplyDelete
  3. How would I set this up, if I had an LED SPI 8x8 matrix that i was hooking up to the UNO, instead of just hooking up a 8x8 to a breadboard, and then to the uno. Because with the SPI, it only has 10 total pins(5 on each side: VCC,GND,DIN,CS,CLK)

    ReplyDelete
  4. Hi! While I'm really happy to find correct array of row and column pins, I have to say you need to reconsider your wiring! You have your resistors placed partly wrong and because of that your 8x8 LED array works with different brightnesses. Trace your pins back to LED array with help of your row[] and col[]. Make sure all your row[] pins go to a 1k resistor and on to the LED array pin from there. Then you will have same brightness of all LED in array at all times. Cheers :)

    ReplyDelete
  5. Thank you. It works.... but... can you add an explanation of the code. I would like to lern the logic!

    ReplyDelete
  6. nice n cute... i will try sketcode... n put in my bloxspot

    ReplyDelete
  7. Can you explain a bit about what is happening in void setupChar() ? I don't understand the purpose of the char str[] = "FABCDEDCBA";

    ReplyDelete
    Replies
    1. As I remember:

      char str[] = "FABCDEDCBA";
      is the sequency to display the bit map:
      A = &heart0
      B = &heart1
      C = &heart2
      D = &heart3
      E = &heart4
      F = &charBlank
      The re-direct map is stored in:
      const charMapType *charMap[6] = {&heart0, &heart1, &heart2, &heart3, &heart4, &charBlank};

      such that I can easy re-arrange the mapping.

      The sequency of "FABCDEDCBA" =
      - charBlank
      - heart0
      - heart1
      - heart2
      - heart3
      - heart4
      - heart3
      - heart2
      - heart1

      Delete
  8. This resistor-array is faulty as it will cause dimm LEDs in certain spots while other LEDs are still getting full power from the Arduino. If you remove the bottom row of resistors and leave the connection open, you will still see certain LEDs lighting up. They are the ones that were brighter than the others before. I just ran everything as you pictured it WITHOUT the resistors and had a nice, even display. The LEDs are fine without any sign of them getting over current. This applies to the 1588BS Matrix from the China Starter Kit.

    ReplyDelete
  9. Are what are the header files needed for it?

    ReplyDelete