animations on 4x4 LED matrix using shift register.

Hi,

about three weeks ago I bought me an arduino to play around. I had no pogramming skills at all, so it's quite an effort to teach myself the simplest things, but it's fun :).
But now I'm stuck. I soldered a little 4x4 LED matrix. (never soldered before, worked at first try yeah) Directly driven I had no problems figuring out how to light it up and display some animations. Now I plugged a 74hc595n shift register between. Shouldn't have done it..... :wink: After hours and hours researching I managed to play simple animations like that:

//animation for 4x4 LED matrix using 74hc595n shift register.
//shows diagonal lines for every half second.

int latchPin = 8;
int clockPin = 12;
int dataPin = 11;

// diagonal line from top left to bottom right.
int  diaglr[] = {    //first 4 bits are columns (anodes, HIGH/1 activates). last 4 bits are rows (cathodes, LOW/0 activates).
  B10000111,
  B01001011,
  B00101101,
  B00011110
};

// diagonal line from bottom left to top right.
int diagrl[] = {
  B00010111,
  B00101011,
  B01001101,
  B10001110
};

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  
  unsigned long start1 = millis();

  while (millis() - start1 <= 500){
    for (int i = 0; i < 4; i++){
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, diaglr[i]);  
      digitalWrite(latchPin, HIGH);
    }
  }
  
  unsigned long start2 = millis();
  
  while (millis() - start2 <= 500){
   for (int i = 0; i < 4; i++){
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, diagrl[i]);  
      digitalWrite(latchPin, HIGH);
    }
  }
}

Or some other steady "images", who alternate. Now i want to make a snake-like animation: snake starts in the top left corner, goes to top right, bottom right, bottom left, up, and then in a spiral to the middle. At the end the LEDs should all be on and it should start again. I found the "Arduino Frame Animator" here in the forum. Unfortunately I don't know how to parse it in my code. I tried, but miserably failed... I used two for-loops like I did, when I directly drove the matrix:

//just a snippet. Imagine some setup and loop stuff like above...
int snake[16][4] = {
  { 0x3, 0x0, 0x0, 0x0 },
  { 0xf, 0x0, 0x0, 0x0 },
  { 0x3f, 0x0, 0x0, 0x0 },
  { 0xff, 0x0, 0x0, 0x0 },
  { 0xff, 0xc0, 0x0, 0x0 },
  { 0xff, 0xc0, 0xc0, 0x0 },
  { 0xff, 0xc0, 0xc0, 0xc0 },
  { 0xff, 0xc0, 0xc0, 0xf0 },
  { 0xff, 0xc0, 0xc0, 0xfc },
  { 0xff, 0xc0, 0xc0, 0xff },
  { 0xff, 0xc0, 0xc3, 0xff },
  { 0xff, 0xc3, 0xc3, 0xff },
  { 0xff, 0xcf, 0xc3, 0xff },
  { 0xff, 0xff, 0xc3, 0xff },
  { 0xff, 0xff, 0xf3, 0xff },
  { 0xff, 0xff, 0xff, 0xff }
};

void loop(){

    for (int i = 0; i < 16; i++){      
      for (int j = 0; j < 4; j++){
       digitalWrite(latchPin, LOW);
       shiftOut(dataPin, clockPin, LSBFIRST, snake[i][j]);  
       digitalWrite(latchPin, HIGH);
      delay(250);
    }
    delay(250);
  }
}

But it just blinks randomly. Due to my very poor coding skills, I have no clue how to do it. Can you help me?

samsa

edit: I looked at the example provided by the maker of the animation creator. But there's just too much stuff I don't understand or (at the moment) don't need (like PWM)....

That's the pattern I want do display. But all LEDs that are lit before should stay on, so that the snake gets longer until it bites itself...

int latchPin = 8;
int clockPin = 12;
int dataPin = 11;

int snake[] = {
  B10000111,
  B01000111,
  B00100111,
  B00010111,
  B00011011,
  B00011101,
  B00011110,
  B00101110,
  B01001110,
  B10001110,
  B10001101,
  B10001011,
  B01001011,
  B00101011,
  B00101101,
  B01001101,
};

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
    for (int i = 0; i < 16; i++){
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, snake[i]);  
      digitalWrite(latchPin, HIGH);
      delay(100);
    
  }
}

I just built a 20 x 5 LED matrix using the 74HC595 shift register.

I started with http://bildr.org/2011/02/74hc595/

If this is your 4 x 4 LED matrix,

LED0    LED1    LED2    LED4
LED5    LED6    LED7    LED8
LED9    LED10   LED11   LED12
LED13   LED14   LED15   LED15

LED0 IC1 pin 15
LED1 IC1 pin 1
...
LED7 IC1 pin 7
LED8 IC2 pin 15
LED9 IC2 pin 1
...
LED15 IC2 pin 7

To create the snake,
LED0 is on
then LED0 + LED1 are on
then LED0 + LED1 + LED2 are on
then LED0 + LED1 + LED2 + LED3 are on
then LED0 + LED1 + LED2 + LED3 + LED7 are on
...
LED0 + LED1 + LED2 + LED3 + LED7 + LED11 + LED15 + LED14 + LED13 + LED12 + LED8 + LED4 + LED5 + LED6 + LED10 are on
LED0 + LED1 + LED2 + LED3 + LED7 + LED11 + LED15 + LED14 + LED13 + LED12 + LED8 + LED4 + LED5 + LED6 + LED10 + LED9 are on

int snake[16] = 
{
  B1000000000000000,
  B1100000000000000,
...
  B1111111110011111,   //LED9 and LED10 are off
  B1111111110111111,   //LED9 is off
  B1111111111111111,
}

In your code inside loop( ) function, you would have a FOR loop that runs 16 times.

Thank you for your answer ieee488, but that doesn't work. I'm sending bytes to the shift register and those can only be 8 digits. That's why I tried it with that [16]x[4] array. But you understood my problem. I have to use 16 different frames:


frame1: LED1 on
delay(sometime);
frame2: LED 1 + LED2 on
delay(sometime);
.
.
.
frame5: LED1 + LED2 + LED3 + LED4 + LED8 on
delay(sometime)
frame6: LED1 + LED2 + LED3 + LED4 + LED8 + LED12 on
.
.
.
frame16: all LEDs on


like you said.

But that's a hell of a lot frames i'd have to define. Maybe you know how i could write a code that does something like that (beginning with LED1, top left):


LED1 on
.
.
.
multiplex LEDs(1-4, 8 )
multiplex LEDs(1-4, 8, 12)
multiplex LEDs(1-4, 8, 12, 16)
.
.
.
multiplex LEDs(1-4, 8, 12, 13-16)
.
.
.
multiplex allLEDs


I think my problem is how to address that array which is given me by the animation creator in a for loop, so that my shift register can read it.

I have no idea how you have your shift registers wired.

The article is how my circuit is wired.