/* Simple multiplexing of a matrix of 64 LEDs. For details, see: http://hblok.net http://hblok.net/blog/posts/tag/shift-register This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ // Digital pins for two independent shift registers. const int dataA = 5; const int latchA = 6; const int clockA = 7; const int dataB = 8; const int latchB = 9; const int clockB = 10; const int outputPinCount = 6; int outputPins[outputPinCount] = { dataA, clockA, latchA, dataB, clockB, latchB}; // Frame buffer const int lights = 64; int buf[lights]; void setupPins() { for (int i = 0; i < outputPinCount; i++) { pinMode(outputPins[i], OUTPUT); } clearAll(); } void setup() { Serial.begin(9600); Serial.println("reset"); setupPins(); setAll(1); } void loop() { display(); } void clearAll() { setAll(0); } void setAll(int v) { for (int i = 0; i < lights; i++) { buf[i] = v; } } void display() { for(int grp = 0; grp < 8; grp++) { digitalWrite(latchB, LOW); // Writes a single bit to the B register. // 0 means that group is active / on, 1 means off. // At the beginning of the loop, one 0 is shifted // onto the first bit, and later shifted upwards // as ones are turning the previous groups off. digitalWrite(clockB, LOW); digitalWrite(dataB, grp == 0 ? 0 : 1); digitalWrite(clockB, HIGH); digitalWrite(latchA, LOW); for(int i = 7; i > -1; i--) { digitalWrite(clockA, LOW); int bit = buf[grp * 8 + i]; digitalWrite(dataA, bit); digitalWrite(clockA, HIGH); } digitalWrite(latchA, HIGH); digitalWrite(latchB, HIGH); } }