led dot matrix- 8 frames-

Hi everyone. I have a specific part of a question but i will post all of the code for completeness. Basically I have 8 frames, all of which are an image of a wheel with a different wheel spin for each frame. What is confusing me is that the bitwise rotation line of code, from what i gather, should shift each row in the first frame to the left by one bit. Then every element in the second image should shift by one bit. But this is not what is observed. Let me clarify myself after the code is shown

#include <TimerOne.h>
int latchPin = 8; //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)


int dataPin = 11; //Pin connected to Pin 14 of 74HC595 (Data)
byte frame = 0; // variable to store the current frame being displayed

byte led[8][8] = { {0, 56, 92, 158, 158, 130, 68, 56}, // 8 frames of an animation
{0, 56, 124, 186, 146, 130, 68, 56},
{0, 56, 116, 242, 242, 130, 68, 56},
{0, 56, 68, 226, 242, 226, 68, 56},
{0, 56, 68, 130, 242, 242, 116, 56},
{0, 56, 68, 130, 146, 186, 124, 56},
{0, 56, 68, 130, 158, 158, 92, 56},
{0, 56, 68, 142, 158, 142, 68, 56} };
void setup() {
pinMode(latchPin, OUTPUT); // set the 3 digital pins to outputs
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
Timer1.initialize(10000); // set a timer of length 10000 microseconds
Timer1.attachInterrupt(screenUpdate); // attach the screenUpdate function
}
void loop() {
//MY QUESTION IS TO DO WITH THIS CODE HERE!
for (int i=0; i<8; i++) { // loop through all 8 frames of the animation
for (int j=0; j<8; j++) { // loop through the 8 rows per frame
led[i][j]= led[i][j] << 1 | led[i][j] >> 7; // bitwise rotation
}
}
frame++; // go to the next frame in the animation
if (frame>7) { frame =0;} // make sure we go back to frame 0 once past 7
delay(100); // wait a bit between frames
}
void screenUpdate() { // function to display image
byte row = B10000000; // row 1
for (byte k = 0; k < 9; k++) {
digitalWrite(latchPin, LOW); // open latch ready to receive data
shiftIt(~led[frame][k] ); // LED array (inverted)
shiftIt(row); // row binary number
// Close the latch, sending the data in the registers out to the matrix
digitalWrite(latchPin, HIGH);
row = row >> 1; // bitshift right
}
}
void shiftIt(byte dataOut) {
// Shift out 8 bits LSB first, on rising edge of clock
boolean pinState;
//clear shift register read for sending data
digitalWrite(dataPin, LOW);
// for each bit in dataOut send out a bit
for (int i=0; i<8; i++) {
//set clockPin to LOW prior to sending bit
digitalWrite(clockPin, LOW);
// if the value of DataOut and (logical AND) a bitmask
// are true, set pinState to 1 (HIGH)
if ( dataOut & (1<<i) ) {
pinState = HIGH;
}
else {
pinState = LOW;
}
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
//send bit out on rising edge of clock
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
}
digitalWrite(clockPin, LOW); //stop shifting
}

I understand the bitwise rotation. For example, if I was dealing with only one frame, then all i would need is one for loop (i have written in the code what part im referring to in capital letters) followed by
led[0][j]= led[0][j] << 1 | led*[j] >> 7; the for loop would continue to shift each bit left and once the left bit reaches the end of the led dot matrix, it appears on the right side(I understand the reason why even though my explanation is poor).*
What Im totally confused about the following:
*Say that the for loop was led[0][1] and iterated through *
led [0][2] [0][3]...led[0][8]. All these values would be shifted by one and refer to the first frame. then the loop would go through led[1][0] led [1][1]...led [1][8]. But how is the bitwise operation taking effect because it would suggest that one frame gets shifted by one bit to the left and then the next frame all gets shifted one bit to the left.
What i am trying to say is that I don't get how one frame smoothly follows the next frame. From the line of code that im confused with, i see it as one frame moves to the left, then the next frame, but the image im seeing on my led matrix starts the next frame in the actual position of the previous frame.
I think my confusion lies in with my second issue which is the timer interrupt. I have defined it as 100 micro seconds but how far does the main loop run until that interrupt happens? I mean the double for loop that im confused on may only run the first frame and then get an interrupt or it may run all 8...I have no idea when that interrupt actually happens.
Sorry for my rant and the essay! Kind regards :slight_smile:

The interrupt service routine updates an 8x8 LED matrix 1000 times a second (or as fast as it can if it takes more than a millisecond to do an update). You might want to change that time to 10,000 microseconds (100 refreshes per second).

The main loop runs at close to 10 frames per second. For each frame it indexes to the next image to cause the wheel to appear to rotate and shifts all the wheel images one pixel to the left so it will appear as if one wheel is moving out of frame to the left while a second wheel moves into frame from the right.

Since 'frame' is written in loop() and read in the interrupt service routine you should declare it 'volatile'.

Thankyou for the reply johnwasser.

From what you said I am now picturing it as the following. The for loop loops through all 8 frames and shifts these 8 frames by one bit(on the first loop). Then the interrupt occurs, calls the screenUpdate() function, calls the shiftIt() function for the led current frame (and all the bits in that row) and then calls the shiftIt() function again to set that current row. The screenUpdate() loops through all the rows for that specific frame.

One thing that I'm still confused about is that the main loop has the frame variable which keeps changing from 0 to 7 all the time. When the screenUpdate function occurs(which is around 100 refreshes as you stated), am i correct in saying that on the very first time the screenUpdate function occurs, could the frame be set at, say 6? And say I reset the program, could the first frame start at say 3?

Or is the above wrong, and the first time the for loop occurs, 8 frames are iterated through, and then the screenUpdate is called while the frame is at 0? I think that would be more likely.

100 times a second the ISR will refresh all 64 pixels, one row at a time, using whatever image the 'frame' counter points to.

10 times a second (well, slower because the delay() isn't the only thing that takes time) the main loop updates the 'frame' counter. (it also scrolls all the frames sideways)

The net effect is that the pixels get refreshed about ten times between frame changes.

Brilliant, thank you for all your help John!