用于5x7 LED矩阵的Arduino代码无法正常工作

时间:2012-10-09 22:35:52

标签: matrix arduino led

我想知道我在这个项目中做错了什么。我有一个8x8矩阵码,并尝试将其改编为我的5x7 LED矩阵。

因此,重要的是要记住我正在使用FrequencyTimer2库,并且由于Arduino Uno's端口的数量,我只能使用橙色LED。

LED矩阵5x7映射:

Enter image description here

Original code(第一个示例代码)

改编代码:

/*
 * Show messages on an 8x8 LED matrix,
 * scrolling from right to left.
 *
 * Uses FrequencyTimer2 library to
 * constantly run an interrupt routine
 * at a specified frequency. This
 * refreshes the display without the
 * main loop having to do anything.
 *
 */

#include <FrequencyTimer2.h>

#define SPACE { \
    {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}, \
}

#define H { \
    {1, 0, 0, 1, 0}, \
    {1, 0, 0, 1, 0}, \
    {1, 0, 0, 1, 0}, \
    {1, 1, 1, 1, 0}, \
    {1, 0, 0, 1, 0}, \
    {1, 0, 0, 1, 0}, \
    {1, 0, 0, 1, 0}, \
 }

#define E  { \
    {1, 1, 1, 1, 0}, \
    {1, 0, 0, 0, 0}, \
    {1, 0, 0, 0, 0}, \
    {1, 1, 1, 1, 0}, \
    {1, 0, 0, 0, 0}, \
    {1, 0, 0, 0, 0}, \
    {1, 1, 1, 1, 0}  \
}

#define L { \
    {1, 0, 0, 0, 0}, \
    {1, 0, 0, 0, 0}, \
    {1, 0, 0, 0, 0}, \
    {1, 0, 0, 0, 0}, \
    {1, 0, 0, 0, 0}, \
    {1, 0, 0, 0, 0}, \
    {1, 1, 1, 1, 0}  \
}

#define O { \
    {1, 1, 1, 1, 0}, \
    {1, 0, 0, 1, 0}, \
    {1, 0, 0, 1, 0}, \
    {1, 0, 0, 1, 0}, \
    {1, 0, 0, 1, 0}, \
    {1, 0, 0, 1, 0}, \
    {1, 1, 1, 1, 0}  \
}

byte col = 0;
byte leds[5][7];

// pin[xx] on LED matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1)
int pins[14]= {-1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

// col[xx] of LEDs = pin yy on LED matrix
int cols[5] = {pins[13], pins[11], pins[10], pins[4], pins[6]};

// row[xx] of LEDs = pin yy on LED matrix
int rows[7] = {pins[5], pins[2], pins[3], pins[9], pins[8], pins[7], pins[12]};
const int numPatterns = 6;
byte patterns[numPatterns][8][8] = {
  H,E,L,L,O,SPACE
};

int pattern = 0;

void setup() {
  // Sets the pins as output
  for (int i = 1; i <= 16; i++) {
    pinMode(pins[i], OUTPUT);
  }

  // Set up cols and rows
  for (int i = 1; i <= 8; i++) {
    digitalWrite(cols[i - 1], LOW);
  }

  for (int i = 1; i <= 8; i++) {
    digitalWrite(rows[i - 1], LOW);
  }

  clearLeds();

  // Turn off toggling of pin 11
  FrequencyTimer2::disable();
  // Set refresh rate (interrupt timeout period)
  FrequencyTimer2::setPeriod(2000);
  // Set interrupt routine to be called
  FrequencyTimer2::setOnOverflow(display);

  setPattern(pattern);
}

void loop() {
    pattern = ++pattern % numPatterns;
    slidePattern(pattern, 60);
}

void clearLeds() {
  // Clear display array
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      leds[i][j] = 0;
    }
  }
}

void setPattern(int pattern) {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      leds[i][j] = patterns[pattern][i][j];
    }
  }
}

void slidePattern(int pattern, int del) {
  for (int l = 0; l < 8; l++) {
    for (int i = 0; i < 7; i++) {
      for (int j = 0; j < 8; j++) {
        leds[j][i] = leds[j][i+1];
      }
    }
    for (int j = 0; j < 8; j++) {
      leds[j][7] = patterns[pattern][j][0 + l];
    }
    delay(del);
  }
}

// Interrupt routine
void display() {
  digitalWrite(cols[col], LOW);  // Turn whole previous column off
  col++;
  if (col == 8) {
    col = 0;
  }
  for (int row = 0; row < 8; row++) {
    if (leds[col][7 - row] == 1) {
      digitalWrite(rows[row], LOW);  // Turn on this LED
    }
    else {
      digitalWrite(rows[row], HIGH); // Turn off this LED
    }
  }
  digitalWrite(cols[col], HIGH); // Turn whole column on at once (for equal lighting times)
}

0 个答案:

没有答案