Last active
August 29, 2015 14:10
-
-
Save mpflaga/4755b88540a6fa7b25a0 to your computer and use it in GitHub Desktop.
Arduino sketch to directly drive the pins of a CSM58261EG 8x5 Red/Green LED Matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Show messages on an 8x5 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. | |
* | |
* This sketch was based on DirectDriveLED.pde | |
* it has been improved to be more general | |
* and self descriptive about the X and Y. Better | |
* lending itself to different sizes of matrixs. | |
*/ | |
#include <FrequencyTimer2.h> | |
#define POST_DELAY 125/8 | |
#define SPACE { \ | |
{' ', ' ', ' ', ' ', ' '}, \ | |
{' ', ' ', ' ', ' ', ' '}, \ | |
{' ', ' ', ' ', ' ', ' '}, \ | |
{' ', ' ', ' ', ' ', ' '}, \ | |
{' ', ' ', ' ', ' ', ' '}, \ | |
{' ', ' ', ' ', ' ', ' '}, \ | |
{' ', ' ', ' ', ' ', ' '}, \ | |
{' ', ' ', ' ', ' ', ' '} \ | |
} | |
#define H { \ | |
{'R', ' ', ' ', 'R', ' '}, \ | |
{'R', ' ', ' ', 'R', ' '}, \ | |
{'R', ' ', ' ', 'R', ' '}, \ | |
{'R', 'R', 'R', 'R', ' '}, \ | |
{'R', ' ', ' ', 'R', ' '}, \ | |
{'R', ' ', ' ', 'R', ' '}, \ | |
{'R', ' ', ' ', 'R', ' '}, \ | |
{'R', ' ', ' ', 'R', ' '} \ | |
} | |
#define E { \ | |
{'G', 'G', 'G', 'G', ' '}, \ | |
{'G', ' ', ' ', ' ', ' '}, \ | |
{'G', ' ', ' ', ' ', ' '}, \ | |
{'G', 'G', 'G', 'G', ' '}, \ | |
{'G', ' ', ' ', ' ', ' '}, \ | |
{'G', ' ', ' ', ' ', ' '}, \ | |
{'G', ' ', ' ', ' ', ' '}, \ | |
{'G', 'G', 'G', 'G', ' '} \ | |
} | |
#define L { \ | |
{'Y', ' ', ' ', ' ', ' '}, \ | |
{'Y', ' ', ' ', ' ', ' '}, \ | |
{'Y', ' ', ' ', ' ', ' '}, \ | |
{'Y', ' ', ' ', ' ', ' '}, \ | |
{'Y', ' ', ' ', ' ', ' '}, \ | |
{'Y', ' ', ' ', ' ', ' '}, \ | |
{'Y', ' ', ' ', ' ', ' '}, \ | |
{'Y', 'Y', 'Y', 'Y', ' '} \ | |
} | |
#define O { \ | |
{' ', 'R', 'G', ' ', ' '}, \ | |
{'G', ' ', ' ', 'Y', ' '}, \ | |
{'Y', ' ', ' ', 'R', ' '}, \ | |
{'G', ' ', ' ', 'G', ' '}, \ | |
{'R', ' ', ' ', 'Y', ' '}, \ | |
{'Y', ' ', ' ', 'R', ' '}, \ | |
{'G', ' ', ' ', 'G', ' '}, \ | |
{' ', 'R', 'Y', ' ', ' '} \ | |
} | |
#define size_of_array(X) (sizeof(X)/sizeof(X[0])) //total length divided by length of one element | |
//col[xx] of leds = pin yy on led matrix | |
//Red pins on CSM58261EG: {18, 3, 4, 7, 11}; | |
const int col_red[] = {A5, 4, 5, 8, 12}; | |
//Grn pins on CSM58261EG: { 1, 2, 13, 12, 10}; | |
const int col_grn[] = { 2, 3, A0, 13, 11}; | |
//row[xx] of leds = pin yy on led matrix | |
//row pins on CSM58261EG: {16, 15, 17, 14, 8, 5, 6, 9}; | |
const int rows[] = {A3, A2, A4, A1, 9, 6, 7, 10}; | |
byte current_row = 0; | |
byte leds[size_of_array(rows)][size_of_array(col_red)]; | |
byte patterns[][size_of_array(rows)][size_of_array(col_red)] = { | |
H,E,L,L,O,SPACE | |
}; | |
int pattern = 0; | |
void setup() { | |
// preset the pins as off and out output | |
// first the column pins | |
for (int col = 0; col < size_of_array(col_red); col++) { | |
digitalWrite(col_red[col], HIGH); | |
pinMode(col_red[col], OUTPUT); | |
digitalWrite(col_grn[col], HIGH); | |
pinMode(col_grn[col], OUTPUT); | |
} | |
// then the row pins | |
for (int row = 0; row < size_of_array(rows); row++) { | |
digitalWrite(rows[row], LOW); | |
pinMode(rows[row], OUTPUT); | |
} | |
for (int row = 0; row < size_of_array(rows); row++) { | |
digitalWrite(rows[row], HIGH); // Turn on this led | |
for (int col = 0; col < size_of_array(col_red); col++) { | |
digitalWrite(col_red[col], LOW); // Turn whole column on at once (for equal lighting times) | |
delay(POST_DELAY); | |
digitalWrite(col_grn[col], LOW); // Turn whole column on at once (for equal lighting times) | |
delay(POST_DELAY); | |
digitalWrite(col_red[col], HIGH); // Turn whole previous column off | |
delay(POST_DELAY); | |
digitalWrite(col_grn[col], HIGH); // Turn whole previous column off | |
} | |
digitalWrite(rows[row], LOW); // Turn off this led | |
} | |
clearLeds(); // clear the buffer of LEDs | |
// initialize the timer to update the pins driving the LEDs | |
// Turn off toggling of pin 11 | |
FrequencyTimer2::disable(); | |
// Set refresh rate (interrupt timeout period) | |
FrequencyTimer2::setPeriod(2000); // MicroSeconds | |
// Set interrupt routine to be called on overflow to draw next column | |
FrequencyTimer2::setOnOverflow(display); | |
// set first pattern into LED buffer | |
setPattern(pattern); | |
delay(1000); | |
} | |
void loop() { | |
pattern = ++pattern % size_of_array(patterns); | |
slidePattern(pattern, 500); | |
} | |
void clearLeds() { | |
// Clear display array | |
for (int col = 0; col < size_of_array(col_red); col++) { | |
for (int row = 0; row < size_of_array(rows); row++) { | |
leds[row][col] = 0; | |
} | |
} | |
} | |
void setPattern(int pattern) { | |
for (int col = 0; col < size_of_array(col_red); col++) { | |
for (int row = 0; row < size_of_array(rows); row++) { | |
leds[row][col] = patterns[pattern][row][col]; | |
} | |
} | |
} | |
void slidePattern(int next_pattern, int delay_period) { | |
for (int col = 0; col < size_of_array(col_red); col++) { | |
for (int col_minusFirst = 1; col_minusFirst < size_of_array(col_red); col_minusFirst++) { | |
for (int row = 0; row < size_of_array(rows); row++) { | |
leds[row][col_minusFirst - 1] = leds[row][col_minusFirst]; | |
} | |
} | |
for (int row = 0; row < size_of_array(rows); row++) { | |
leds[row][(size_of_array(col_red) - 1)] = patterns[next_pattern][row][col]; | |
} | |
delay(delay_period); | |
} | |
} | |
// Interrupt routine | |
void display() { | |
digitalWrite(rows[current_row], LOW); // Turn whole previous row off | |
//current_row = ++current_row % size_of_array(rows); | |
current_row++; | |
if (current_row >= size_of_array(rows)) { | |
current_row = 0; | |
} | |
for (int col = 0; col < size_of_array(col_red); col++) { | |
if ((leds[current_row][col] == 'R') || (leds[current_row][col] == 'Y')) { | |
digitalWrite(col_red[col], LOW); // Turn on this led | |
} | |
else { | |
digitalWrite(col_red[col], HIGH); // Turn off this led | |
} | |
if ((leds[current_row][col] == 'G') || (leds[current_row][col] == 'Y')) { | |
digitalWrite(col_grn[col], LOW); // Turn on this led | |
} | |
else { | |
digitalWrite(col_grn[col], HIGH); // Turn off this led | |
} | |
} | |
digitalWrite(rows[current_row], HIGH); // Turn whole column on at once (for equal lighting times) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment