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