Go Down

### Topic: Scrolling LED Matrix code (Read 1 time)previous topic - next topic

#### PaulRB

#15
##### Sep 12, 2017, 08:08 am
Pick values for I, j & k and work though an example on paper. Use the reference page to find out about the "|" and ">>" operators.

#### cabyambo

#16
##### Sep 12, 2017, 07:14 pm
@PaulRb I've made sense of that line of code and edited it a little so that spaces work properly. The only problem I have is that I'd like to make the space a shorter length. It's too much waiting currently, but I can't seem to figure out how to shorten it without messing up everything else. Would you happen to know how to do this?

Code: [Select]
`const int rClock = 2; //chip pin 7const int rData = 0; //chip pin 5const int cData = 4; //chip pin 3const int latch = 1; //chip pin 6 const int cClock = 3; //chip pin 2//replaced with " bit(i) "//byte rowActivator[] = {B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001};//byte rowActivator[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000};byte letters[27][8] ={  {B00000000, B00111000, B01000100, B01000100, B01000100, B01111100, B01000100, B01000100}, //A0  {B00000000, B01111000, B01000100, B01000100, B01111000, B01000100, B01000100, B01111000}, //B1  {B00000000, B00111000, B01000100, B01000000, B01000000, B01000000, B01000100, B00111000}, //C2  {B00000000, B01110000, B01001000, B01000100, B01000100, B01000100, B01001000, B01110000}, //D3  {B00000000, B01111100, B01000000, B01000000, B01111100, B01000000, B01000000, B01111100}, //E4  {B00000000, B01111100, B01000000, B01000000, B01111000, B01000000, B01000000, B01000000}, //F5  {B00000000, B01111000, B01000100, B01000000, B01011100, B01000100, B01000100, B00111100}, //G6  {B00000000, B01000100, B01000100, B01000100, B01111100, B01000100, B01000100, B01000100}, //H7  {B00000000, B01110000, B00100000, B00100000, B00100000, B00100000, B00100000, B01110000}, //I8  {B00000000, B00011100, B00001000, B00001000, B00001000, B00001000, B01001000, B00110000}, //J9  {B00000000, B01000100, B01001000, B01010000, B01100000, B01010000, B01001000, B01000100}, //K10  {B00000000, B01000000, B01000000, B01000000, B01000000, B01000000, B01000000, B01111100}, //L11  {B00000000, B01000100, B01101100, B01010100, B01010100, B01000100, B01000100, B01000100}, //M12  {B00000000, B01000100, B01000100, B01100100, B01010100, B01001100, B01000100, B01000100}, //N13  {B00000000, B00111000, B01000100, B01000100, B01000100, B01000100, B01000100, B00111000}, //O14  {B00000000, B01111000, B01000100, B01000100, B01111000, B01000000, B01000000, B01000000}, //P15  {B00000000, B00111000, B01000100, B01000100, B01000100, B01010100, B01001000, B00110100}, //Q16  {B00000000, B01111000, B01000100, B01000100, B01111000, B01010000, B01001000, B01000100}, //R17  {B00000000, B00111100, B01000000, B01000000, B00111000, B00000100, B00000100, B01111000}, //S18  {B00000000, B01111100, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000}, //T19  {B00000000, B01000100, B01000100, B01000100, B01000100, B01000100, B01000100, B00111000}, //U20  {B00000000, B01000100, B01000100, B01000100, B01000100, B01000100, B00101000, B00010000}, //V21  {B00000000, B01000100, B01000100, B01000100, B01010100, B01010100, B01010100, B00101000}, //W22  {B00000000, B01000100, B01000100, B00101000, B00010000, B00101000, B01000100, B01000100}, //X23  {B00000000, B01000100, B01000100, B01000100, B00101000, B00010000, B00010000, B00010000}, //Y24  {B00000000, B01111100, B00000100, B00001000, B00010000, B00100000, B01000000, B01111100}, //Z25  {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000}, // Space to shift 32};void setup() {  //Serial.begin(9600); Isn't supported on the ATtiny85  pinMode(rClock, OUTPUT);  pinMode(rData, OUTPUT);  pinMode(cData, OUTPUT);  pinMode(latch, OUTPUT);  pinMode(cClock, OUTPUT);}void loop() {  scrollMessage(" HELLO WORLD "); }//function to clear the row registers preventing "ghosting"void clrData() {  digitalWrite(latch, LOW);  shiftOut(rData, rClock, LSBFIRST, B00000000);  digitalWrite(latch, HIGH); }void displayLetter(char letter){  for(int i=0; i<8; i++)  {    clrData();    digitalWrite(latch, LOW);    shiftOut(rData, rClock, LSBFIRST, bit(i)); //bit(i) used to be row activator     shiftOut(cData, cClock, LSBFIRST, letters[letter - 65][i]);    digitalWrite(latch, HIGH);    delay(0);  }}//add a space to the end of the messagevoid scrollMessage(char* message){    int scrollDelay = 130;  for(int i = 0; i < strlen(message)-1; i++)  {    for(int j=0; j<8; j++)    {      unsigned long timer = millis();      while(millis() - timer < scrollDelay)      {         for(int m=0; m<8; m++)        {          byte b;          //Allows spaces to work correctly          if(message[i+1] == ' ') {            b = letters[message[i] - 'A'][m] << j | letters[26][m] >> (8-j);          }          else if(message[i] == ' ') {            b = letters[26][m] << j | letters[message[i +1] - 'A'][m] >> (8-j);          }else {            b = letters[message[i] - 'A'][m] << j | letters[message[i+1] - 'A'][m] >> (8-j);          }          clrData();          digitalWrite(latch, LOW);          shiftOut(rData, rClock, LSBFIRST, bit(m));          shiftOut(cData, cClock, LSBFIRST, b);          digitalWrite(latch, HIGH);          delay(1);         }      }    }  }}`

#### PaulRB

#17
##### Sep 12, 2017, 09:02 pmLast Edit: Sep 12, 2017, 09:04 pm by PaulRB
Yes, I see what you mean about spaces and I did not think of that. The ASCII code for a space is 32, so when you subtract the code for an 'A', 65, the answer is negative. But then it gets assigned to a byte, which is  an unsigned type, so we get some value over 220, which is outside the bounds of the 'letter' array. Well done for spoting that and for figuring out a solution. Your solution could have been simpler, because
Code: [Select]
`letters[26][m] >> (8-j)`
will always be zero for any value of m or j, so you might as well delete those parts of the formula.

What I don't understand is that you say that space takes too long to scroll. Are you saying it takes longer than any other letter? I think it should take the same time as any other letter, and that sounds ok to me.

#### cabyambo

#18
##### Sep 12, 2017, 10:41 pm
Thanks for that way to simplify the space code. The problem I am having is that though the space works correctly, I personally think it is too long. I was wondering if there was a way to make the space 6 pixels wide instead of 8, or any width I'd like.

#### PaulRB

#19
##### Sep 12, 2017, 11:17 pm
Not easily. You would then have the situation where 3 characters would be partially visible on the matrix instead of two: the space and the letter before and the letter after.

What if you just increase the overall scroll speed for the message? Find a balance where you can still read the message but the space scrolling does not seem so slow.

Go Up