Programming a LED Cube with Arduino Mega 1280 using Shift Register CAT4016(W).

/*
 Hardware design based on:
 http://picprojects.org.uk/projects/lc/index.htm#Schematic  This is a PIC based project but the Arduino 
 has all it takes to drive the display. After building the basic driver board using jumpers to an Arduino
 board, I later added a AVR 328 socket to the driver board and moved the processor to it for standalone operation.
 I choose to build the LED cube and driver as seperate assemblies, coupled with a 40 pin IDC ribbon cable. That way 
 I can rebuild the cube someday if I wish, using different support/soldering method.
 
Software:
The MsTimer2 libaray (http://www.arduino.cc/playground/Main/MsTimer2) is used to generate a 2msec timer interrupt, 
so the five row cube is completly refreshed every 10msec. faster then the eye can detect flickering. I have yet to test
using a pattern script from EEPROM memory as of this time. I know how to load a hex file to the AVR chip, but I haven't
figured out how I might create the hex file! A cube editor would be very useful, but well beyond my software skills.

The two column driver chips connected in series are Allegro A6276. I got mine from Newark Electronics. It combines the function
of a 32 bit serial in, parallel output shift register, plus 32 constant current drive output pins.  The data output pin's 
constant current sink value is programmable (set by a fixed or variable resistor), so 125 external current limiting resistors are not
required, and the lamp brightness can be easily adjustable, nice! I used two 650 ohm resistors I had on hand. The two series 
connected 16 bit shift registers allows for a total of 32 bits of column data. For a 5X5X5 cube only 25 bits (0-24) are wired
and used, bits 25-31 could be used for future additions. Maybe drive a 5x7 numeric display at same time as the cube?
The five NPN row driver transistors (I used TIP29C) allow the Arduino to update each 25 light row (or level) for 2msec after
the 25 column data bits have been shifted out to the column driver chips and latched into the current driver outputs.

http://www.allegromicro.com/en/Products/Part_Numbers/6276/6276.pdf


                           April 14, 2010 by retrolefty @ http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl

*/

#include <MsTimer2.h>     // used to set scanning rate
#include <avr/eeprom.h>   // used to read pattern data from on chip eeprom


const byte potInput = 10;    // used to get optional variable cube update interval time via analog input pin 0, not implemented of tested.
const byte clockPin = 5;   // shift reg clock pin
const byte dataPin = 6;    // shift reg data pin
const byte latchPin = 13;   // shift reg latch pin, note that the Arduino built in pin 13 LED will show this latch activity, but very dim.
const byte row0Pin =0;      // 5 cube row driver output pins to drive 5 row driver transistors
const byte row1Pin =1;
const byte row2Pin =2;
const byte row3Pin =3;
const byte row4Pin =4;
const int  timedelay = 200;  // how fast to update cube with a new data pattern (milliseconds)
const byte updatetype = 0;   // 0 = fixed update interval using timedelay constant value 
                             // 1 = variable update interval using analog input pin 0 voltage
                             // 2 = time interval obtained by high 7 hits in active_display[4], from EEPROM script
                             
                          
const unsigned long all_off_display[5] = {0,0,0,0,0};  // all LEDs off pattern
const unsigned long all_on_display[5]  = {0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff}; // all LEDs on pattern
const unsigned long alt_on_display[5]  = {0xaaaaaaaa,0xaaaaaaaa,0xaaaaaaaa,0xaaaaaaaa,0xaaaaaaaa}; // alternating LED pattern
const unsigned long alt_off_display[5] = {0x55555555,0x55555555,0x55555555,0x55555555,0x55555555}; // opposite alt LED pattern

unsigned long patternDelay;
volatile byte active_row = 0;  // represents the active row of five vertical rows of the cube, 0-4 = bottom to top rows
volatile unsigned long active_display[5] = {0,0,0,0,0}; // holds 5 rows of 25 leds (125 LEDs/bits) to update cube
unsigned int address = 0;      // EEPROM starting address, mega168 holds 128 patterns, mega328 holds 256, mega1280 holds 1,024 patterns
byte Pattern = 2;              // data pattern to send to cube, 0= blink all LEDs on/off, 1 = alternating LEDs on/off,
                               // 2 = random LEDs pattern, 3 = run a script pattern from preloaded EEPROM data (not tested yet)
                        
                       void setup() {
  
  // setup 5 output pins to drive cube row driver transistors
  pinMode(row0Pin, OUTPUT);   // setup and turn off all 5 row driver transistors
  digitalWrite(row0Pin, LOW);
  pinMode(row1Pin, OUTPUT);
  digitalWrite(row1Pin, LOW);
  pinMode(row2Pin, OUTPUT);
  digitalWrite(row2Pin, LOW);
  pinMode(row3Pin, OUTPUT);
  digitalWrite(row3Pin, LOW);
  pinMode(row4Pin, OUTPUT);
  digitalWrite(row4Pin, LOW);
  
  // setup and turn off 3 shift register control pins
  pinMode(clockPin, OUTPUT);  
  digitalWrite(clockPin, LOW);
  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin,  LOW);
  pinMode(latchPin, OUTPUT);
  digitalWrite(latchPin, LOW);
  
  // four input pins used to select pattern update type by jumper ground clip on driver/processor board
  pinMode(7, INPUT);                  
  digitalWrite(7, HIGH);  //jumper will select pattern 0 on/off
  pinMode(8, INPUT);           
  digitalWrite(8, HIGH);  //jumper will select pattern 1 alternating on/off
  pinMode(9, INPUT);           
  digitalWrite(9, HIGH);  //jumper will select pattern 2 random pattern
  pinMode(10, INPUT);           
  digitalWrite(10, HIGH); //jumper will select pattern 3 EEPROM script pattern


 
 
  for (byte j = 0; j < 32; j++) {          // set shift reg to all columns outputs off, in case shift reg doesn't power-up cleared 
       digitalWrite (dataPin, LOW);        // probably not needed, but why not start in a known all off condition.
       digitalWrite(clockPin, HIGH);       // clock 32 low bits to shift regester
       digitalWrite(clockPin, LOW);
     } 
  digitalWrite(latchPin, HIGH);            //latch all zeros to 25 column output pins
  digitalWrite(latchPin, LOW);
  
  randomSeed(analogRead(1));               // used for random LED pattern mode for cube

  
  Serial.begin(57600);
  Serial.println ("5X5X5 Cube Ready");     // signal initalization done
 
 

  MsTimer2::set(2, refresh_display);      // 2ms period, generates a 2msec interrupt to the ISR routine

  MsTimer2::start(); 
   
}  // End of setup