Advertisement
atuline

Arduino 3x3 Row Column Scan LED matrix with PWM

Aug 21st, 2014
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. /* Arduino UNO with 3x3 LED Array using row column scanning with PWM and only using built in ports.
  2.  *
  3.  * Author: Andrew Tuline
  4.  *
  5.  * Date: July 14, 2012
  6.  *
  7.  * This does not use any 3rd party chips, thus easy to use in a Lilypad.
  8.  *
  9.  * Uses synchronized PWM to support LED brightness.
  10.  *
  11.  * Neither the Arduino's analogWrite, nor the softPWM library worked for me with row/column scanning.
  12.  * The LED's blinked, so I created a synchronized PWM capability in order to minimize blinking.
  13.  *
  14.  * The default for cathodes is HIGH and LOW for anodes. We will be applying PWM to the cathodes,
  15.  * which lends itself nicely to a common anode configuration for RGB LED's later on.
  16.  *
  17.  * Seems to crash every so often, so I'm trying to be careful with usage of variables.
  18.  *
  19.  */
  20.  
  21.  
  22. int mydelay = 500;                  // used to determine how often to change LED values
  23. int oldmillis = 0;                  // a counter to support the delay value above
  24. const int row[] = {3, 5, 6};        // rows are anodes. Consider them 'common' anodes
  25. const int col[] = {11, 10, 9};      // columns are cathodes and will have PWM applied to them
  26. int pixels[3][3];                   // 2-dimensional array of pixels;
  27. int mycounter = 0;                 // used for PWM on each LED
  28.  
  29. void setup () {
  30.   for (int i=0; i<3; i++) {
  31.     pinMode(row[i], OUTPUT);                   // initialize Arduino ports as OUTPUT for the rows and columns
  32.     pinMode(col[i], OUTPUT);
  33.     digitalWrite(col[i], HIGH);                // turn off pixel to start by setting the cathode HIGH
  34.   } // for
  35.  
  36.   randomSeed(analogRead(0));                  // randomizer
  37.  
  38.   Serial.begin(9600);                        // console setup, only used for debugging
  39.  
  40. } // setup()
  41.  
  42. void loop() {
  43.   readSensors();
  44.   refreshScreen();
  45. } // loop()
  46.  
  47.  
  48. void readSensors() {                           // this routine changes the value of the LED's, but only after a delay time
  49.  
  50.   if (int(millis()/mydelay) == oldmillis) {    // only change values if we've not done so in 'mydelay' milliseconds
  51.       ++oldmillis;                             // increase the counter used to count the time between changing LED values
  52.       mycounter = 0;                           // reset the refresh counter that's used for PWM so it doesn't cause the 9999 value below to strobe occasionally
  53.  
  54.     for (int x=0; x<3; x++) {                  // change the values for ALL of the LED's
  55.       for (int y=0; y<3; y++) {
  56.         pixels[x][y] = int(random(0, 5)*20);        // this is a PWM value, the higher the number, the fewer the times the LED will be pulsed
  57.         if (pixels[x][y] == 80) {              // even a long duration of PWM is > 0 pulses, so let's represent OFF somehow
  58.           pixels[x][y] = 9999;                 // this value represents an LED that is turned off. Testing has shown that 'mycounter' doesn't go this high
  59.         }
  60.       }
  61.     }
  62.   }
  63. } // readSensors()
  64.  
  65.  
  66. void refreshScreen() {
  67.   mycounter++;                                    // use a counter that will be bitwise AND'ed with the pixel value to determine if a LED is turned on or not
  68.  
  69.   for (int thisRow = 0; thisRow<3; thisRow++) {   // enable a row by setting the anode to high
  70.     digitalWrite(row[thisRow], HIGH);
  71.     for(int thisCol = 0; thisCol<3; thisCol++) {  // now we go column by column
  72.       int thisPixel = pixels[thisRow][thisCol];
  73.  
  74.       if ((mycounter & thisPixel) == thisPixel) {  // depending on the PWM value we will turn on the LED . .
  75.         digitalWrite(col[thisCol], LOW);           // by setting the cathode LOW
  76.         digitalWrite(col[thisCol], HIGH);          // now, set the column high again to turn if off
  77.         }
  78.     } // for thisCol
  79.    digitalWrite(row[thisRow], LOW);                // once the columns are done, turn off the row (anode)
  80.   } // for thisRow
  81. } // refreshScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement