3x3 LED matrix

Hello, I am reading book „C Programming for Arduino” and I am not sure am I understand something wrong, or is there a mistake in the book.

So here is the code:

int CLOCK_595 = 4; // first 595 clock pin connecting to pin 4
int LATCH_595 = 3; // first 595 latch pin connecting to pin 3
int DATA_595 = 2; // first 595 serial data pin connecting to pin 2
int counter = 0;
boolean LED_states[9] ;

void setup() {
pinMode(LATCH_595, OUTPUT);
pinMode(CLOCK_595, OUTPUT);
 pinMode(DATA_595, OUTPUT);
 // use a seed coming from the electronic noise of the ADC
 randomSeed(analogRead(0));
}

void loop() {
 // generate random state for each 9 LEDs
 for (int i = 0 ; i < 9 ; i++)
 {
 LED_states[i] = random(2) ;
 }
 // initialize data at each loop()
 byte data = 0;
 byte dataRow = 0;
 byte dataColumn = 0;
 int currentLed = 0;
 // cycling columns
 for (int c = 0 ; c < 3 ; c++)
 {
 // write the 1 at the correct bit place (= current column)
 dataColumn = 1 << (4 - c);
 // cycling rows
 for (int r = 0 ; r < 3 ; r++)
 {
 // IF that LED has to be up, according to LED_states array
 // write the 1 at the correct bit place (= current row)
 if (LED_states[currentLed]) dataRow = 1 << (4 - c);
 // sum the two half-bytes results in the data to be sent
 data = dataRow | dataColumn;
 // Put latch pin to LOW (ground) while transmitting data to 595
 digitalWrite(LATCH_595, LOW);
 // Shifting Out bits
 shiftOut(DATA_595, CLOCK_595, MSBFIRST, data);
// Put latch pin to HIGH (5V) & all data are pushed to outputs
 digitalWrite(LATCH_595, HIGH);
 dataRow = 0; // resetting row bits for next turn
 currentLed++;// incrementing to next LED to process
 }
 dataColumn = 0;// resetting column bits for next turn
 }
 // each 5000 loop() execution, grab a new seed for the random function
 if (counter < 5000) counter++;
 else
 {
 randomSeed(analogRead(0)); // read a new value from analog pin 0
 counter = 0; // reset the counter
 }
 // pause a bit to provide a cuter fx
 delay(150);
}

I do not have shift registers so I am unable to test out this sketch by my own.
Confusion starts in line ” if (LED_states[currentLed]) dataRow = 1 << (4 - c);”.
Isn't there suppose to be (8-c) instead of (4-c) , due to sum of two half bytes in continue?

Thanks in advance :wink:

Oring rows and cols in the same spot is an error.

How are the connections set up (between the shift register and the leds)?

Whandall:
Oring rows and cols in the same spot is an error.

How are the connections set up (between the shift register and the leds)?

Something like this should work then

  for (int c = 0 ; c < 3 ; c++)
  {
    // write the 1 at the correct bit place (= current column)
    dataColumn = 1 << (3 + c);
    // cycling rows
    for (int r = 0 ; r < 3 ; r++)
    {
      // IF that LED has to be up, according to LED_states array
      // write the 1 at the correct bit place (= current row)
      if (LED_states[currentLed]) dataRow = 1 << r;

Whandall:
Something like this should work then

  for (int c = 0 ; c < 3 ; c++)

{
    // write the 1 at the correct bit place (= current column)
    dataColumn = 1 << (3 + c);
    // cycling rows
    for (int r = 0 ; r < 3 ; r++)
    {
      // IF that LED has to be up, according to LED_states array
      // write the 1 at the correct bit place (= current row)
      if (LED_states[currentLed]) dataRow = 1 << r;

sorry, but your code make less sense to me that code before. :smiley:

  • if there is (4-c) and (8-r), then first 3 bits will control rows, and 4,5,6th will control columns and two right bits always stays empty a.k.a. 0,

mixxx2005:

  • if there is (4-c) and (8-r), then first 3 bits will control rows, and 4,5,6th will control columns and two right bits always stays empty a.k.a. 0,

(4-c) and (8-r) has to be wrong,
because the row and column bits end up not adjacent (2 x three bits but 4 bits away), but are adjacent in the hardware.

The connections (shown in your picture) are:

Q0 -> row1
Q1 -> row2
Q2 -> row3
Q3 -> col1
Q4 -> col2
Q5 -> col3

My code builds a byte (or tries to) to reflect that.

I do not know whether it has to be shifted out MSB first or LSB first to be positioned correctly.