Advertisement
Guest User

Andrew

a guest
Feb 14th, 2008
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.15 KB | None | 0 0
  1. /*
  2.  * Show messages on an 8x8 led matrix,
  3.  * scrolling from right to left.
  4.  *
  5.  * Uses FrequencyTimer2 library to
  6.  * constantly run an interrupt routine
  7.  * at a specified frequency. This
  8.  * refreshes the display without the
  9.  * main loop having to do anything.
  10.  *
  11.  */
  12.  
  13. #include <FrequencyTimer2.h>
  14.  
  15. #define SPACE { \
  16.     {0, 0, 0, 0, 0, 0, 0, 0},  \
  17.     {0, 0, 0, 0, 0, 0, 0, 0}, \
  18.     {0, 0, 0, 0, 0, 0, 0, 0}, \
  19.     {0, 0, 0, 0, 0, 0, 0, 0}, \
  20.     {0, 0, 0, 0, 0, 0, 0, 0}, \
  21.     {0, 0, 0, 0, 0, 0, 0, 0}, \
  22.     {0, 0, 0, 0, 0, 0, 0, 0}, \
  23.     {0, 0, 0, 0, 0, 0, 0, 0} \
  24. }
  25.  
  26. #define H { \
  27.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  28.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  29.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  30.     {0, 1, 1, 1, 1, 1, 1, 0}, \
  31.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  32.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  33.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  34.     {0, 1, 0, 0, 0, 0, 1, 0}  \
  35. }
  36.  
  37. #define E  { \
  38.     {0, 1, 1, 1, 1, 1, 1, 0}, \
  39.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  40.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  41.     {0, 1, 1, 1, 1, 1, 1, 0}, \
  42.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  43.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  44.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  45.     {0, 1, 1, 1, 1, 1, 1, 0}  \
  46. }
  47.    
  48. #define L { \
  49.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  50.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  51.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  52.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  53.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  54.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  55.     {0, 1, 0, 0, 0, 0, 0, 0}, \
  56.     {0, 1, 1, 1, 1, 1, 1, 0}  \
  57. }
  58.  
  59. #define O { \
  60.     {0, 0, 0, 1, 1, 0, 0, 0}, \
  61.     {0, 0, 1, 0, 0, 1, 0, 0}, \
  62.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  63.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  64.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  65.     {0, 1, 0, 0, 0, 0, 1, 0}, \
  66.     {0, 0, 1, 0, 0, 1, 0, 0}, \
  67.     {0, 0, 0, 1, 1, 0, 0, 0}  \
  68. }
  69.  
  70. byte analogPin = 5;
  71. byte col = 0;
  72. byte leds[8][8];
  73.  
  74. // pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1)
  75. int pins[17]= {-1, 5, 4, 3, 2, 14, 15, 16, 17, 13, 12, 11, 10, 9, 8, 7, 6};
  76.  
  77. // col[xx] of leds = pin yy on led matrix
  78. int cols[8] = {pins[13], pins[3], pins[4], pins[10], pins[06], pins[11], pins[15], pins[16]};
  79.  
  80. // row[xx] of leds = pin yy on led matrix
  81. int rows[8] = {pins[9], pins[14], pins[8], pins[12], pins[1], pins[7], pins[2], pins[5]};
  82.  
  83. const int numPatterns = 6;
  84. byte patterns[numPatterns][8][8] = {
  85.   H,E,L,L,O,SPACE
  86. };
  87.  
  88. int pattern = 0;
  89.  
  90. void setup() {
  91.   // sets the pins as output
  92.   for (int i = 1; i <= 16; i++) {
  93.     pinMode(pins[i], OUTPUT);
  94.   }
  95.  
  96.   // set up cols and rows
  97.   for (int i = 1; i <= 8; i++) {
  98.     digitalWrite(cols[i - 1], LOW);
  99.   }
  100.  
  101.   for (int i = 1; i <= 8; i++) {
  102.     digitalWrite(rows[i - 1], LOW);
  103.   }
  104.  
  105.   clearLeds();
  106.  
  107.   // Turn off toggling of pin 11
  108.   FrequencyTimer2::disable();
  109.   // Set refresh rate (interrupt timeout period)
  110.   FrequencyTimer2::setPeriod(2000);
  111.   // Set interrupt routine to be called
  112.   FrequencyTimer2::setOnOverflow(display);
  113.  
  114.   setPattern(pattern);
  115. }
  116.  
  117. void loop() {
  118.     pattern = ++pattern % numPatterns;
  119.     slidePattern(pattern, 60);
  120. }
  121.  
  122. void clearLeds() {
  123.   // Clear display array
  124.   for (int i = 0; i < 8; i++) {
  125.     for (int j = 0; j < 8; j++) {
  126.       leds[i][j] = 0;
  127.     }
  128.   }
  129. }
  130.  
  131. void setPattern(int pattern) {
  132.   for (int i = 0; i < 8; i++) {
  133.     for (int j = 0; j < 8; j++) {
  134.       leds[i][j] = patterns[pattern][i][j];
  135.     }
  136.   }
  137. }
  138.  
  139. void slidePattern(int pattern, int del) {
  140.   for (int l = 0; l < 8; l++) {
  141.     for (int i = 0; i < 7; i++) {
  142.       for (int j = 0; j < 8; j++) {
  143.         leds[j][i] = leds[j][i+1];
  144.       }
  145.     }
  146.     for (int j = 0; j < 8; j++) {
  147.       leds[j][7] = patterns[pattern][j][0 + l];
  148.     }
  149.     delay(del);
  150.   }
  151. }
  152.  
  153. // Interrupt routine
  154. void display() {
  155.   digitalWrite(cols[col], LOW);  // Turn whole previous column off
  156.   col++;
  157.   if (col == 8) {
  158.     col = 0;
  159.   }
  160.   for (int row = 0; row < 8; row++) {
  161.     if (leds[col][7 - row] == 1) {
  162.       digitalWrite(rows[row], LOW);  // Turn on this led
  163.     }
  164.     else {
  165.       digitalWrite(rows[row], HIGH); // Turn off this led
  166.     }
  167.   }
  168.   digitalWrite(cols[col], HIGH); // Turn whole column on at once (for equal lighting times)
  169.   delayMicroseconds(900);  // Delay so that on times are longer than off time = brighter leds
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement