//www.elegoo.com
//2016.12.9
//We always have to include the library
#include "LedControl.h"
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 12 is connected to the DataIn
pin 11 is connected to LOAD(CS)
pin 10 is connected to the CLK
We have only a single MAX72XX.
*/
//LedControl lc=LedControl(12,10,11,1);
LedControl lc=LedControl(11,13,10,1);
volatile unsigned long customMillisValue1 = 0;
volatile unsigned long customMillisValue2 = 0;
unsigned long delaytime2=50;
unsigned long delaytime1 = 500;
void setup() {
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,8);
/* and clear the display */
lc.clearDisplay(0);
Serial.begin(115200);
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 31250;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A);
OCR1B = 3125;
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1B); // Enable Timer 1B compare interrupt
sei();
}
/*
This method will display the characters for the
word "Arduino" one after the other on the matrix.
(you need at least 5x7 leds to see the whole chars)
*/
unsigned long previousMillis1 = 0;
int currentCharIndex = 0;
byte characters[][5] = {
{B01111110,B10001000,B10001000,B10001000,B01111110}, // 'a'
{B00010000,B00100000,B00100000,B00010000,B00111110}, // 'r'
{B11111110,B00010010,B00100010,B00100010,B00011100}, // 'd'
{B00111110,B00000100,B00000010,B00000010,B00111100}, // 'u'
{B00000000,B00000010,B10111110,B00100010,B00000000}, // 'i'
{B00011110,B00100000,B00100000,B00010000,B00111110}, // 'n'
{B00011100,B00100010,B00100010,B00100010,B00011100} // 'o'
};
void writeArduinoOnMatrix() {
unsigned long currentMillis = customMillis1();
if (currentCharIndex < 7) {
if (currentMillis - previousMillis1 >= delaytime1) {
lc.setRow(0, 0, characters[currentCharIndex][0]);
lc.setRow(0, 1, characters[currentCharIndex][1]);
lc.setRow(0, 2, characters[currentCharIndex][2]);
lc.setRow(0, 3, characters[currentCharIndex][3]);
lc.setRow(0, 4, characters[currentCharIndex][4]);
previousMillis1 = currentMillis;
currentCharIndex++;
}
} else {
lc.setRow(0, 0, 0);
lc.setRow(0, 1, 0);
lc.setRow(0, 2, 0);
lc.setRow(0, 3, 0);
lc.setRow(0, 4, 0);
currentCharIndex = 0;
}
}
/*
This function lights up a some Leds in a row.
The pattern will be repeated on every row.
The pattern will blink along with the row-number.
row number 4 (index==3) will blink 4 times etc.
*/
// // Define variables to keep track of the current row and timing
// // Define variables to keep track of the current row and timing
int currentRow = 0;
unsigned long previousMillis2 = 0;
void rows() {
unsigned long currentMillis = customMillis2();
if (currentMillis - previousMillis2 >= delaytime2) {
lc.setRow(0, currentRow, B10100000);
previousMillis2 = currentMillis;
// Clear the previous row
int previousRow = currentRow - 1;
if (previousRow < 0) {
previousRow = 7;
}
lc.setRow(0, previousRow, (byte)0);
currentRow++;
if (currentRow >= 8) {
currentRow = 0;
}
}
}
/*
This function lights up a some Leds in a column.
The pattern will be repeated on every column.
The pattern will blink along with the column-number.
column number 4 (index==3) will blink 4 times etc.
*/
unsigned long previousMillis3 = 0;
void columns() {
static int col = 0;
unsigned long currentMillis = customMillis2();
if (currentMillis - previousMillis3 >= delaytime2) {
previousMillis3 = currentMillis;
// Clear the previous column
for (int clearCol = 0; clearCol < 8; clearCol++) {
lc.setColumn(0, clearCol, (byte)0);
}
// Set the current column
lc.setColumn(0, col, B10100000);
col++;
if (col >= 8) {
col = 0;
}
}
}
/*
This function will light up every Led on the matrix.
The led will blink along with the row-number.
row number 4 (index==3) will blink 4 times etc.
*/
unsigned long previousMillis4 = 0;
int row1 = 0;
int col1 = 0;
int state = 0;
bool ledStates[8][8] = {false};
void single() {
unsigned long currentMillis = customMillis2();
if (currentMillis - previousMillis4 >= delaytime2) {
previousMillis4 = currentMillis;
lc.setLed(0, row1, col1, false);
ledStates[row1][col1] = !ledStates[row1][col1];
lc.setLed(0, row1, col1, ledStates[row1][col1]);
col1++;
if (col1 >= 8) {
col1 = 0;
row1++;
if (row1 >= 8) {
row1 = 0;
}
}
}
}
unsigned long dotMillis = 0;
void printdot()
{
unsigned long currentMillis = customMillis1();
if (currentMillis - dotMillis >= delaytime1) {
dotMillis = currentMillis;
Serial.print(".");
}
}
void loop() {
writeArduinoOnMatrix();
rows();
columns();
single();
printdot();
}
ISR(TIMER1_COMPA_vect) {
customMillisValue1 += 500;
}
ISR(TIMER1_COMPB_vect) {
customMillisValue2 +=50;
}
unsigned long customMillis1() {
return customMillisValue1;
}
unsigned long customMillis2(){
return customMillisValue2;
}