## Arduino Program to Design Running Diagonal LEDs in an 8*8 LED Matrix

by realfinetime  |  in LED Matrix at  09:34

We had already seen the circuit to connect 8*8 LED matrix to arduino through 8 bit shift register IC 74595 in previous blog. Next is a simple program to make running diagonal LEDs (1, 10, 19, 28, 37, 46, 55, 64) in an 8*8 LED matrix. LED matrix will look like as shown in the following image.

```int latchPin = 12;  //Pin connected to ST_CP of 1st 74595
int clockPin = 13;  //Pin connected to SH_CP of 1st 74595
int dataPin = 11;   //Pin connected to DS of 1st 74595

int latchPin2 = 6;  //Pin connected to ST_CP of 2nd 74595
int clockPin2 = 7;  //Pin connected to SH_CP of 2nd 74595
int dataPin2 = 5;   //Pin connected to DS of 2nd 74595

int j=0;
int anode_decimal={1, 2, 4, 8, 16, 32, 64, 128};
int cathode_decimal={254, 253, 251, 247, 239, 223, 191, 127};

void setup() {
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

pinMode(latchPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
}

void loop() {
for(j=0;j<8;j++)
{
/************To increase the ON time of LEDs five times more than
OFF time to increase the brightness of LEDs*************/

/*************************  TURN ON DIAGONAL LEDs ONLY  ***************************/

// take the latchPin low so the LEDs don't change while you're sending in bits:
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, anode_decimal[j]);
// shift out the bits:
digitalWrite(latchPin, HIGH);

// take the latchPin low so the LEDs don't change while you're sending in bits:
digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, cathode_decimal[j]);
// shift out the bits:
digitalWrite(latchPin2, HIGH);

delay(100);

/**************************  TURN OFF ALL LEDs  ***************************/

/*** Send LOW to all Anode pins (16, 15, 11, 3, 10, 5, 6, 13) of LED matrix ***/

// take the latchPin low so the LEDs don't change while you're sending in bits:
digitalWrite(latchPin, LOW);
//Send 0 0 0 0 0 0 0 0 (1) to Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595
shiftOut(dataPin, clockPin, MSBFIRST, 0);
// shift out the bits:
digitalWrite(latchPin, HIGH);

/*** Send HIGH to all cathode pins (4, 7, 2, 8, 12, 1, 14 and 9) of LED matrix ***/

// take the latchPin low so the LEDs don't change while you're sending in bits:
digitalWrite(latchPin2, LOW);
//Send 1 1 1 1 1 1 1 1 (255) to Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595
shiftOut(dataPin2, clockPin2, MSBFIRST, 255);
// shift out the bits:
digitalWrite(latchPin2, HIGH);
}
}
```

Output will be running diagonal LEDs. We have defined two arrays in program. One is 'anode_decimal' and the other is 'cathode_decimal'. 'anode_decimal' array has numbers that should be shifted out to the anode pins of 8*8 LED matrix to turn on the diagonal LEDs only. 'cathode_decimal' array also has numbers that should be shifted out to the cathode pins of 8*8 LED matrix to turn on diagonal LEDs only.

When j=0

anode_decimal[j]=1.
cathode_decimal[j]=254.

When anode_decimal[j] (1) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 0 0 0 0 1. When cathode_decimal[j] (254) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 1 1 1 1 0. This will turn on the 1st LED.

Delay for 100 milliseconds.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.

When j=1

anode_decimal[j]=2.
cathode_decimal[j]=253.

When anode_decimal[j] (2) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 0 0 0 1 0. When cathode_decimal[j] (253) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 1 1 1 0 1. This will turn on the 10th LED.

Delay for 100 milliseconds.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=2

anode_decimal[j]=4.
cathode_decimal[j]=251.

When anode_decimal[j] (4) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 0 0 1 0 0. When cathode_decimal[j] (251) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 1 1 0 1 1. This will turn on the 19th LED.

Delay for 100 milliseconds.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=3

anode_decimal[j]=8.
cathode_decimal[j]=247.

When anode_decimal[j] (8) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 0 1 0 0 0. When cathode_decimal[j] (247) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 1 0 1 1 1. This will turn on the 28th LED.

Delay for 100 milliseconds.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=4

anode_decimal[j]=16.
cathode_decimal[j]=239.

When anode_decimal[j] (16) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 0 1 0 0 0 0. When cathode_decimal[j] (239) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 1 0 1 1 1 1. This will turn on the 37th LED.

Delay for 100 milliseconds.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=5

anode_decimal[j]=32.
cathode_decimal[j]=223.

When anode_decimal[j] (32) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 0 1 0 0 0 0 0. When cathode_decimal[j] (223) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 1 0 1 1 1 1 1. This will turn on the 46th LED.

Delay for 100 milliseconds.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=6

anode_decimal[j]=64.
cathode_decimal[j]=191.

When anode_decimal[j] (64) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 0 1 0 0 0 0 0 0. When cathode_decimal[j] (191) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 1 0 1 1 1 1 1 1. This will turn on the 55th LED.

Delay for 100 milliseconds.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
When j=7

anode_decimal[j]=128.
cathode_decimal[j]=127.

When anode_decimal[j] (128) is shifted out through 1st 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595 becomes 1 0 0 0 0 0 0 0. When cathode_decimal[j] (127) is shifted out through 2nd 74595, Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595 becomes 0 1 1 1 1 1 1 1. This will turn on the 64th LED.

Delay for 100 milliseconds.

Turn off all LEDs of LED matrix. For that, shift out 0 to 1st 74595 and 255 to 2nd 74595.
Only one LED glows at a time. Each LED glows at each iteration of 'j' loop. Since iteration is very fast, due to persistence of vision, our eyes will feel that all LEDs are glowing together. Hence eye will feel that all diagonal LEDs are glowing together.