mathiasbk

Untitled

May 30th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. /*
  2.  * Example Code for an 8 x 8 LED Matrix
  3.  * For More Details Visit http://www.tinyurl.com/yhwxv6h
  4.  *
  5.  * Displays a test pattern lighting one LED after another
  6.  * To Play around with modifying the bitmap, un comment Example #2
  7.  */
  8.  
  9.  
  10. int speed = 5; //the delay time in milliseconds
  11.  
  12. int pauseDelay = 1;    //the number of milliseconds to display each scanned line
  13.  
  14. //Pin Definitions
  15. int rowA[] = {9,8,7,6,5,4,3,2};          //An Array defining which pin each row is attached to
  16.                                          //(rows are common anode (drive HIGH))
  17. int colA[] = {38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22};  //An Array defining which pin each column is attached to
  18.                                          //(columns are common cathode (drive LOW))
  19.  
  20. //The array used to hold a bitmap of the display
  21. //(if you wish to do something other than scrolling marque change the data in this
  22. //variable then display)
  23. byte data[] = {0,0,0,0,0,0,0,0};    
  24.  
  25.  
  26. //Setup runs once when power is applied
  27. void setup()
  28. {
  29.   Serial.begin(9600);         //Open the Serial port for debugging
  30.   for(int i = 0; i <8; i++){  //Set the 16 pins used to control the array as OUTPUTs
  31.     pinMode(rowA[i], OUTPUT);
  32.     pinMode(colA[i], OUTPUT);
  33.   }
  34. }
  35.  
  36. //repeats  
  37. void loop()
  38. {
  39.  //Example #1 - test pattern
  40.  //Run a small test program which lights up each light in time
  41.   test();
  42.  
  43.  //Example #2 - static image
  44.  //Display a defined bitmap
  45. /*
  46.   data[0] = B10101010; //row 1s bit mask (1 LED is on 0 LED is off)
  47.   data[1] = B01010101; //row 1s bit mask (1 LED is on 0 LED is off)
  48.   data[2] = B10101010; //row 1s bit mask (1 LED is on 0 LED is off)
  49.   data[3] = B01010101; //row 1s bit mask (1 LED is on 0 LED is off)
  50.   data[4] = B10101010; //row 1s bit mask (1 LED is on 0 LED is off)
  51.   data[5] = B01010101; //row 1s bit mask (1 LED is on 0 LED is off)
  52.   data[6] = B10101010; //row 1s bit mask (1 LED is on 0 LED is off)  
  53.   data[7] = B01010101; //row 1s bit mask (1 LED is on 0 LED is off)  
  54.   showSprite(speed);
  55. */
  56. }
  57.  
  58. //An array to store power values to act as bit masks
  59. const int powers[] = {1,2,4,8,16,32,64,128};
  60.  
  61. //Runs a pattern where each LED is lit one after another
  62. void test(){
  63.   for(int i = 0; i < 16; i++){
  64.     for(int ii = 0; ii< 8; ii++){
  65.     data[i] = data[i]+ powers[ii];   //Goes through each row of lights lighting each column one after another
  66.     showSprite(speed);
  67.     }
  68.   }
  69.  
  70.   for(int i = 0; i < 8; i++){
  71.     for(int ii = 0; ii< 8; ii++){
  72.     data[i] = data[i] - powers[ii];   //Goes through each row of lights turning off each column one after another
  73.     showSprite(speed);
  74.     }
  75.   }  
  76. }
  77.  
  78. void showSprite(int speed2){
  79.  for(int iii = 0; iii < speed2; iii++){                 //show the current frame speed2 times
  80.   for(int column = 0; column < 8; column++){            //iterate through each column
  81.    for(int i = 0; i < 8; i++){                          
  82.        digitalWrite(rowA[i], LOW);                      //turn off all row pins  
  83.    }
  84.    for(int i = 0; i < 8; i++){ //Set only the one pin
  85.      if(i == column){     digitalWrite(colA[i], LOW);}  //turns the current row on
  86.      else{                digitalWrite(colA[i], HIGH); }//turns the rest of the rows off
  87.    }
  88.  
  89.    for(int row = 0; row < 8; row++){                    //iterate through each pixel in the current column
  90.     int bit = (data[column] >> row) & 1;
  91.     if(bit == 1){
  92.        digitalWrite(rowA[row], HIGH);                   //if the bit in the data array is set turn the LED on
  93.     }
  94.  
  95.    }
  96.    delay(pauseDelay);                       //leave the column on for pauseDelay microseconds (too high a delay causes flicker)
  97.   }
  98.  }
  99. }
Add Comment
Please, Sign In to add comment