Snake game Update #1

Yesterday got a new Arduino Mega 2560 board in my hands, so decided to make something interesting. I do not have a lot of components, but I have 8×8 LED matrix and a couple of 74HC595 shift registers, so my plan is to make a Snake game. After a little bit reading about Arduino and how the shift registers work I came up with design how to wire an LED matrix.

matrix

I was unable to find a specification for this led matrix, so the only way to figure out which pins are anodes and which are cathodes I had to use a multimeter and test them all. …. I have played a little bit with code I wrote a class to control a led matrix. I don’t know is it a good design, but it works for me and I can change it in future.

Matrix class


class MatrixClass
{
  private:
    bool leds[8][8];

    int dataPin;
    int clockPin;
    int latchPin;

  public:
    void init(int data, int clock, int latch);
    void clear();
    void setHigh(int row, int col);
    void display();
};

void MatrixClass::init(int data, int clock, int latch)
{
  dataPin = data;
  clockPin = clock;
  latchPin = latch;

  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);
}

void MatrixClass::clear()
{
  for (int i = 0; i < 8; i++)
  {
    for (int j = 0; j < 8; j++)
    {
      leds[i][j] = 0;
    }
  }
}

void MatrixClass::setHigh(int row, int col)
{
  leds[row - 1][col - 1] = 1;
}

void MatrixClass::display()
{
  for (int i = 0; i < 8; i++)
  {
    byte row = B00000001 << (7 - i);
    byte col = B00000000;

    for (int j = 0; j < 8; j++)
    {
      if (leds[i][j] == 1)
      {
        col |= B00000001 << (7 - j);
      }
   }

   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, col);
   shiftOut(dataPin, clockPin, LSBFIRST, row);
   digitalWrite(latchPin, HIGH);
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s