int dataIn = 2;
int load = 3;
int clock = 4;
int maxInUse = 5; //change this variable to set how many MAX7219's you'll use
int e = 0; // just a varialble
// define max7219 registers
byte max7219_reg_noop = 0x00;
byte max7219_reg_digit0 = 0x01;
byte max7219_reg_digit1 = 0x02;
byte max7219_reg_digit2 = 0x03;
byte max7219_reg_digit3 = 0x04;
byte max7219_reg_digit4 = 0x05;
byte max7219_reg_digit5 = 0x06;
byte max7219_reg_digit6 = 0x07;
byte max7219_reg_digit7 = 0x08;
byte max7219_reg_decodeMode = 0x09;
byte max7219_reg_intensity = 0x0a;
byte max7219_reg_scanLimit = 0x0b;
byte max7219_reg_shutdown = 0x0c;
byte max7219_reg_displayTest = 0x0f;
void putByte(byte data) {
byte i = 8;
byte mask;
while(i > 0) {
mask = 0x01 << (i - 1); // get bitmask
digitalWrite( clock, LOW); // tick
if (data & mask){ // choose bit
digitalWrite(dataIn, HIGH);// send 1
}else{
digitalWrite(dataIn, LOW); // send 0
}
digitalWrite(clock, HIGH); // tock
--i; // move to lesser bit
}
}
void maxSingle( byte reg, byte col) {
//maxSingle is the "easy" function to use for a //single max7219
digitalWrite(load, LOW); // begin
putByte(reg); // specify register
putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
digitalWrite(load, LOW); // and load da shit
digitalWrite(load,HIGH);
}
void maxAll (byte reg, byte col) { // initialize all MAX7219's in the system
int c = 0;
digitalWrite(load, LOW); // begin
for ( c =1; c<= maxInUse; c++) {
putByte(reg); // specify register
putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
}
digitalWrite(load, LOW);
digitalWrite(load,HIGH);
}
void maxOne(byte maxNr, byte reg, byte col) {
//maxOne is for adressing different MAX7219's,
//whilele having a couple of them cascaded
int c = 0;
digitalWrite(load, LOW); // begin
for ( c = maxInUse; c > maxNr; c--) {
putByte(0); // means no operation
putByte(0); // means no operation
}
putByte(reg); // specify register
putByte(col);//((data & 0x01) * 256) + data >> 1); // put data
for ( c =maxNr-1; c >= 1; c--) {
putByte(0); // means no operation
putByte(0); // means no operation
}
digitalWrite(load, LOW); // and load da shit
digitalWrite(load,HIGH);
}
void setup () {
pinMode(dataIn, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(load, OUTPUT);
//beginSerial(9600);
digitalWrite(13, HIGH);
//initiation of the max 7219
maxAll(max7219_reg_scanLimit, 0x07);
maxAll(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode
maxAll(max7219_reg_displayTest, 0x00); // no display test
for (e=1; e<=8; e++) { // empty registers, turn all LEDs off
maxAll(e,0);
}
maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set
// range: 0x00 to 0x0f
}
void loop () {
//Animation 1
maxOne(1,1,B00010000);
delay(30);
maxOne(1,2,B00010000);
delay(30);
maxOne(1,3,B00010000);
delay(30);
maxOne(1,4,B00010000);
delay(30);
maxOne(1,5,B00010000);
delay(30);
maxOne(1,6,B00010000);
delay(30);
maxOne(1,7,B00010000);
delay(30);
maxOne(1,8,B00010000);
delay(30);
delay(30);
maxOne(2,1,B00010000);
delay(30);
maxOne(2,2,B00010000);
delay(30);
maxOne(2,3,B00001000);
delay(30);
maxOne(2,4,B00000100);
delay(30);
maxOne(2,5,B00000010);
delay(30);
maxOne(2,6,B00000100);
delay(30);
maxOne(2,7,B00001000);
delay(30);
maxOne(2,8,B00010000);
delay(30);
delay(30);
maxOne(3,1,B00100000);
delay(30);
maxOne(3,2,B01000000);
delay(30);
maxOne(3,3,B00100000);
delay(30);
maxOne(3,4,B00010000);
delay(30);
maxOne(3,5,B00010000);
delay(30);
maxOne(3,6,B00010000);
delay(30);
maxOne(3,7,B00010000);
delay(30);
maxOne(3,8,B00010000);
delay(30);
delay(30);
maxOne(4,1,B00010000);
delay(30);
maxOne(4,2,B00010000);
delay(30);
maxOne(4,3,B00010000);
delay(30);
maxOne(4,4,B00001000);
delay(30);
maxOne(4,5,B00000100);
delay(30);
maxOne(4,6,B00000010);
delay(30);
maxOne(4,7,B00000100);
delay(30);
maxOne(4,8,B00001000);
delay(30);
delay(30);
maxOne(5,1,B00010000);
delay(30);
maxOne(5,2,B00100000);
delay(30);
maxOne(5,3,B01000000);
delay(30);
maxOne(5,4,B00100000);
delay(30);
maxOne(5,5,B00010000);
delay(30);
maxOne(5,6,B00010000);
delay(30);
maxOne(5,7,B00010000);
delay(30);
maxOne(5,8,B00010000);
delay(30);
//Animation 2
delay(30);
maxOne(1,1,B00011000);
delay(30);
maxOne(1,2,B00011000);
delay(30);
maxOne(1,1,B00000000);
delay(30);
maxOne(1,3,B00011000);
delay(30);
maxOne(1,2,B00000000);
delay(30);
maxOne(1,4,B00011000);
delay(30);
maxOne(1,3,B00000000);
delay(30);
maxOne(1,5,B00011000);
delay(30);
maxOne(1,4,B00000000);
delay(30);
maxOne(1,6,B00011000);
delay(30);
maxOne(1,5,B00000000);
delay(30);
maxOne(1,7,B00011000);
delay(30);
maxOne(1,6,B00000000);
delay(30);
maxOne(1,8,B00011000);
delay(30);
maxOne(1,7,B00000000);
delay(30);
maxOne(2,1,B00011000);
delay(30);
maxOne(1,8,B00000000);
delay(30);
maxOne(2,2,B00011000);
delay(30);
maxOne(2,1,B00000000);
delay(30);
maxOne(2,3,B00011000);
delay(30);
maxOne(2,2,B00000000);
delay(30);
maxOne(2,4,B00011000);
delay(30);
maxOne(2,3,B00000000);
delay(30);
maxOne(2,5,B00011000);
delay(30);
maxOne(2,4,B00000000);
delay(30);
maxOne(2,6,B00011000);
delay(30);
maxOne(2,5,B00000000);
delay(30);
maxOne(2,7,B00011000);
delay(30);
maxOne(2,6,B00000000);
delay(30);
maxOne(2,8,B00011000);
delay(30);
maxOne(2,7,B00000000);
delay(30);
maxOne(3,1,B00011000);
delay(30);
maxOne(2,8,B00000000);
delay(30);
maxOne(3,2,B00011000);
delay(30);
maxOne(3,1,B00000000);
delay(30);
maxOne(3,3,B00011000);
delay(30);
maxOne(3,2,B00000000);
delay(30);
maxOne(3,4,B00011000);
delay(30);
maxOne(3,3,B00000000);
delay(30);
maxOne(3,5,B00011000);
delay(30);
maxOne(3,4,B00000000);
delay(30);
maxOne(3,6,B00011000);
delay(30);
maxOne(3,5,B00000000);
delay(30);
maxOne(3,7,B00011000);
delay(30);
maxOne(3,6,B00000000);
delay(30);
maxOne(3,8,B00011000);
delay(30);
maxOne(3,7,B00000000);
delay(30);
maxOne(4,1,B00011000);
delay(30);
maxOne(3,8,B00000000);
delay(30);
maxOne(4,2,B00011000);
delay(30);
maxOne(4,1,B00000000);
delay(30);
maxOne(4,3,B00011000);
delay(30);
maxOne(4,2,B00000000);
delay(30);
maxOne(4,4,B00011000);
delay(30);
maxOne(4,3,B00000000);
delay(30);
maxOne(4,5,B00011000);
delay(30);
maxOne(4,4,B00000000);
delay(30);
maxOne(4,6,B00011000);
delay(30);
maxOne(4,5,B00000000);
delay(30);
maxOne(4,7,B00011000);
delay(30);
maxOne(4,6,B00000000);
delay(30);
maxOne(4,8,B00011000);
delay(30);
maxOne(4,7,B00000000);
delay(30);
maxOne(5,1,B00011000);
delay(30);
maxOne(4,8,B00000000);
delay(30);
maxOne(5,2,B00011000);
delay(30);
maxOne(5,1,B00000000);
delay(30);
maxOne(5,3,B00011000);
delay(30);
maxOne(5,2,B00000000);
delay(30);
maxOne(5,4,B00011000);
delay(30);
maxOne(5,3,B00000000);
delay(30);
maxOne(5,5,B00011000);
delay(30);
maxOne(5,4,B00000000);
delay(30);
maxOne(5,6,B00011000);
delay(30);
maxOne(5,5,B00000000);
delay(30);
maxOne(5,7,B00011000);
delay(30);
maxOne(5,6,B00000000);
delay(30);
maxOne(5,8,B00011000);
delay(30);
maxOne(5,7,B00000000);
delay(30);
//Animation 3
maxOne(1,1,B11111111);
delay(50);
maxOne(1,2,B11111111);
delay(50);
maxOne(1,1,B00000000);
maxOne(1,3,B11111111);
delay(50);
maxOne(1,2,B00000000);
maxOne(1,4,B11111111);
delay(50);
maxOne(1,3,B00000000);
maxOne(1,5,B11111111);
delay(50);
maxOne(1,4,B00000000);
maxOne(1,6,B11111111);
delay(50);
maxOne(1,5,B00000000);
maxOne(1,7,B11111111);
delay(50);
maxOne(1,6,B00000000);
maxOne(1,8,B11111111);
delay(50);
maxOne(1,1,B11111111);
maxOne(1,7,B00000000);
maxOne(2,1,B11111111);
delay(50);
maxOne(1,2,B11111111);
maxOne(1,8,B00000000);
maxOne(2,2,B11111111);
delay(50);
//
delay(30);
}