Tuesday, August 27, 2013

31. Displaying Scrolling Messages on the 8 x 8 LED Matrix

Now that we've got multiplexing fully sorted out (!!!) we can move on to not only displaying patterns, but scrolling them across the matrix.

Hers's the code:


1:  /*  
2:   * Example Code for an 8 x 8 LED Matrix  
3:   * For More Details Visit http://www.tinyurl.com/yhwxv6h  
4:   *  
5:   * Scrolls a message across an 8 x 8 LED Matrix  
6:   * To adjust the speed change the variable speed.  
7:   * The message is held in requestString[]  
8:   */  
9:    
10:    
11:  int speed = 20; //number of times to repeat each frame  
12:  int pauseDelay = 500; //microseconds to leave each row on before moving to the next  
13:    
14:  char requestString[] = " Hello World of sparks n smoke ... abcdefghijklmnopqrstuvwxyz ..."; //The string to display  
15:                        //to change the message in code you right yourself simply   
16:                        //change this data and reset index and offset to 0  
17:  //Variables used for scrolling (both start at 0  
18:  int index = 0; //this is the current character in the string being displayed  
19:  int offset = 0; //this is how many columns it is offset by  
20:    
21:  //Pin Definitions  
22:  int rowA[] = {2,7,19,5,13,18,12,16};     //An Array defining which pin each row is attached to  
23:                       //(rows are common anode (drive HIGH))  
24:  int colA[] = {6,11,10,3,17,4,8,9}; //An Array defining which pin each column is attached to  
25:                       //(columns are common cathode (drive LOW))  
26:    
27:  //Constants defining each characters position in an array of integer arrays  
28:  //Letters  
29:  const int A = 0; const int B = 1; const int C = 2; const int D = 3; const int E = 4;  
30:  const int F = 5; const int G = 6; const int H = 7; const int I = 8; const int J = 9;   
31:  const int K = 10; const int L =11; const int M = 12; const int N = 13; const int O = 14;   
32:  const int P = 15; const int Q =16; const int R = 17; const int S = 18; const int T = 19;   
33:  const int U = 20; const int V =21; const int W = 22; const int X = 23; const int Y = 24;   
34:  const int Z = 25;  
35:    
36:  //Punctuation  
37:  const int COL =26; const int DASH = 27; const int BRA2 = 28; const int _ = 29; const int LINE = 34;  
38:  const int DOT =36;  
39:    
40:  //Extra Characters  
41:  const int FULL =30; const int CHECK = 31; const int B2 = 32; const int TEMP = 33;   
42:  const int SMILE =35; const int COLDOT = 36;  
43:    
44:    
45:  //The array used to hold a bitmap of the display   
46:  //(if you wish to do something other than scrolling marque change the data in this  
47:  //variable then display)  
48:  byte data[] = {0,0,0,0,0,0,0,0};      
49:    
50:  //The alphabet  
51:  //Each character is an 8 x 7 bitmap where 1 is on and 0 if off  
52:  const int _A[] = {B0111000,  
53:           B1100100,  
54:           B1100100,  
55:           B1100100,  
56:           B1111100,  
57:           B1100100,  
58:           B1100100,  
59:           B0000000};  
60:    
61:  const int _B[] = {B1111000,  
62:           B1101100,  
63:           B1101100,  
64:           B1111100,  
65:           B1101100,  
66:           B1101100,  
67:           B1111000,  
68:              B0000000};  
69:    
70:  const int _C[] = {B0111000,  
71:           B1100100,  
72:           B1100000,  
73:           B1100000,  
74:           B1100000,  
75:           B1100100,  
76:           B0111000,  
77:              B0000000};  
78:    
79:  const int _D[] = {B1111000,  
80:           B1100100,  
81:           B1100110,  
82:           B1100110,  
83:           B1100110,  
84:           B1100100,  
85:           B1111000,  
86:              B0000000};  
87:    
88:  const int _E[] = {B1111100,  
89:           B1100100,  
90:           B1100000,  
91:           B1111000,  
92:           B1100000,  
93:           B1100100,  
94:           B1111100,  
95:              B0000000};  
96:    
97:  const int _F[] = {B1111100,  
98:           B1100100,  
99:           B1100000,  
100:           B1111000,  
101:           B1100000,  
102:           B1100000,  
103:           B1100000,  
104:              B0000000};  
105:    
106:  const int _G[] = {B0111100,  
107:           B1100100,  
108:           B1100000,  
109:           B1101110,  
110:           B1100100,  
111:           B1100100,  
112:           B0111100,  
113:              B0000000};  
114:    
115:  const int _H[] = {B1100110,  
116:           B1100110,  
117:           B1100110,  
118:           B1111110,  
119:           B1100110,  
120:           B1100110,  
121:           B1100110,  
122:              B0000000};  
123:    
124:  const int _I[] = {B0111100,  
125:           B0011000,  
126:           B0011000,  
127:           B0011000,  
128:           B0011000,  
129:           B0011000,  
130:           B0111100,  
131:              B0000000};  
132:    
133:  const int _J[] = {B0011110,  
134:           B0001100,  
135:           B0001100,  
136:           B0001100,  
137:           B1101100,  
138:           B1101100,  
139:           B0111100,  
140:              B0000000};  
141:    
142:  const int _K[] = {B1100011,  
143:           B1100110,  
144:           B1101100,  
145:           B1111000,  
146:           B1101100,  
147:           B1100110,  
148:           B1100011,  
149:              B0000000};  
150:    
151:  const int _L[] = {B1111000,  
152:           B0110000,  
153:           B0110000,  
154:           B0110000,  
155:           B0110000,  
156:           B0110010,  
157:           B1111110,  
158:              B0000000};  
159:    
160:  const int _M[] = {B1000001,  
161:           B1100011,  
162:           B1110111,  
163:           B1101011,  
164:           B1100011,  
165:           B1100011,  
166:           B1100011,  
167:              B0000000};  
168:    
169:  const int _N[] = {B1100011,  
170:           B1110011,  
171:           B1110011,  
172:           B1101011,  
173:           B1100111,  
174:           B1100111,  
175:           B1100011,  
176:              B0000000};  
177:    
178:  const int _O[] = {B0111100,  
179:           B1100110,  
180:           B1100110,  
181:           B1100110,  
182:           B1100110,  
183:           B1100110,  
184:           B0111100,  
185:              B0000000};  
186:    
187:  const int _P[] = {B1111000,  
188:           B1101100,  
189:           B1101100,  
190:           B1111000,  
191:           B1100000,  
192:           B1100000,  
193:           B1100000,  
194:              B0000000};  
195:    
196:  const int _Q[] = {B0111100,  
197:           B1100110,  
198:           B1100110,  
199:           B1100110,  
200:           B1101110,  
201:           B1100100,  
202:           B0111010,  
203:           B0000001};  
204:    
205:  const int _R[] = {B1111000,  
206:           B1101100,  
207:           B1101100,  
208:           B1111000,  
209:           B1111000,  
210:           B1101100,  
211:           B1100110,  
212:              B0000000};  
213:    
214:  const int _S[] = {B0111100,  
215:           B1100010,  
216:           B1100000,  
217:           B0111100,  
218:           B0000110,  
219:           B1000110,  
220:           B0111100,  
221:              B0000000};  
222:    
223:  const int _T[] = {B1111110,  
224:           B1011010,  
225:           B0011000,  
226:           B0011000,  
227:           B0011000,  
228:           B0011000,  
229:           B0011000,  
230:              B0000000};  
231:    
232:  const int _U[] = {B1100110,  
233:           B1100110,  
234:           B1100110,  
235:           B1100110,  
236:           B1100110,  
237:           B1100110,  
238:           B0111100,  
239:              B0000000};  
240:    
241:  const int _V[] = {B1100011,  
242:           B1100011,  
243:           B1100011,  
244:           B1100011,  
245:           B0110110,  
246:           B0011100,  
247:           B0001000,  
248:              B0000000};  
249:             
250:  const int _W[] = {B1100011,  
251:           B1100011,  
252:           B1100011,  
253:           B1101011,  
254:           B1110111,  
255:           B1100011,  
256:           B1000001,  
257:              B0000000};  
258:    
259:  const int _X[] = {B1100011,  
260:           B0110110,  
261:           B0011100,  
262:           B0001000,  
263:           B0011100,  
264:           B0110110,  
265:           B1100011,  
266:              B0000000};  
267:    
268:  const int _Y[] = {B1100010,  
269:           B0110100,  
270:           B0011000,  
271:           B0011000,  
272:           B0011000,  
273:           B0011000,  
274:           B0011000,  
275:              B0000000};  
276:    
277:  const int _Z[] = {B1111111,  
278:           B1000111,  
279:           B0001100,  
280:           B0011000,  
281:           B0110000,  
282:           B1110001,  
283:           B1111111,  
284:              B0000000};  
285:    
286:  const int _COL[] = {B0000000,  
287:           B0011000,  
288:           B0011000,  
289:           B0000000,  
290:           B0011000,  
291:           B0011000,  
292:           B0000000,  
293:              B0000000};  
294:    
295:  const int _DASH[] = {B0000000,  
296:           B0000000,  
297:           B0000000,  
298:           B0111110,  
299:           B0000000,  
300:           B0000000,  
301:           B0000000,  
302:              B0000000};  
303:    
304:  const int _BRA2[] = {B0010000,  
305:           B0001000,  
306:           B0000100,  
307:           B0000100,  
308:           B0001000,  
309:           B0010000,  
310:           B0000000,  
311:              B0000000};           
312:    
313:  const int __[] = {B0000000,  
314:           B0000000,  
315:           B0000000,  
316:           B0000000,  
317:           B0000000,  
318:           B0000000,  
319:           B0000000,  
320:              B0000000};  
321:    
322:  const int _FULL[] = {B1111111,  
323:             B1111111,  
324:             B1111111,  
325:             B1111111,  
326:             B1111111,  
327:             B1111111,  
328:             B1111111,  
329:              B0000000};           
330:    
331:  const int _CHECK[] = {B1010101,  
332:             B0101010,  
333:             B1010101,  
334:             B0101010,  
335:             B1010101,  
336:             B0101010,  
337:             B1010101,  
338:              B0000000};  
339:             
340:  const int _B2[] = {B0111110,  
341:            B0000001,  
342:            B0000001,  
343:            B0001111,  
344:            B0000001,  
345:            B1000001,  
346:            B0111110,  
347:              B0000000};  
348:    
349:  const int _TEMP[] = {B0000011,  
350:             B0011111,  
351:             B0111111,  
352:             B1111110,  
353:             B1111111,  
354:             B0011111,  
355:             B0000011,  
356:              B0000000};  
357:    
358:  const int _LINE[] = {B0000001,  
359:             B0000001,  
360:             B0000001,  
361:             B0000001,  
362:             B0000001,  
363:             B0000001,  
364:             B0000001,  
365:              B0000000};             
366:             
367:  const int _SMILE[] = {B000000,  
368:             B1100100,  
369:             B1100010,  
370:             B0011001,  
371:             B1100010,  
372:             B1100100,  
373:             B0000000,  
374:             B0000000};             
375:             
376:    
377:  const int _DOT[] = {B0000000,  
378:           B0000000,  
379:           B0000000,  
380:           B0000000,  
381:           B1100000,  
382:           B1100000,  
383:           B0000000,  
384:           B0000000};             
385:             
386:  const int _COLDOT[] = {B0000000,  
387:              B0110000,  
388:              B0110000,  
389:              B0000000,  
390:              B0110011,  
391:              B0110011,  
392:              B0000000,  
393:              B0000000};           
394:    
395:  //Load the bitmap characters into an array (each characters position corresponds to its previously defined index (ie _A (a's bitmap)   
396:  //is at index 0 and A = 0 so letters[A] will return the 'A' bitmap)  
397:  const int* letters[] = {_A,_B,_C,_D,_E,_F,_G,_H,_I,_J,_K,_L,_M,_N,_O,_P,_Q,_R,_S,_T,_U,_V,_W,_X,_Y,_Z,_COL,_DASH,_BRA2,__, _FULL, _CHECK, _B2, _TEMP, _LINE, _SMILE, _DOT, _COLDOT};  
398:    
399:  //Setup runs once when power is applied  
400:  void setup()  
401:  {   
402:   for(int i = 0; i <8; i++){ //Set the 16 pins used to control the array as OUTPUTs  
403:    pinMode(rowA[i], OUTPUT);  
404:    pinMode(colA[i], OUTPUT);  
405:   }  
406:  }  
407:    
408:  //repeats    
409:  void loop()  
410:  {  
411:   updateMatrix();  
412:  }  
413:    
414:    
415:    
416:  void updateMatrix(){  
417:   loadSprite();  
418:   showSprite(speed);  
419:  }  
420:    
421:    
422:  //An array holding the powers of 2 these are used as bit masks when calculating what to display  
423:  const int powers[] = {1,2,4,8,16,32,64,128};  
424:    
425:  //Loads the current scroll state frame into the data[] display array  
426:  void loadSprite(){  
427:   int currentChar = getChar(requestString[index]);  
428:   int nextChar = getChar(requestString[index+1]);  
429:     
430:   for(int row=0; row < 8; row++){          //iterate through each row  
431:    data[row] = 0;                  //reset the row we're working on  
432:    for(int column=0; column < 8; column++){     //iterate through each column  
433:     data[row] = data[row] + ((powers[column] & (letters[currentChar][row] << offset)));  //loads the current character offset by offset pixels   
434:     data[row] = data[row] + (powers[column] & (letters[nextChar][row] >> (8-offset) ));  //loads the next character offset by offset pixels  
435:    }  
436:   }  
437:   offset++;                     //increment the offset by one row  
438:   if(offset==8){offset = 0; index++; if(index==sizeof(requestString)-2){index=0;}}     //if offset is 8 load the next character pair for the next time through  
439:  }  
440:    
441:  void showSprite(int speed2){  
442:   for(int iii = 0; iii < speed2; iii++){         //show the current frame speed2 times  
443:   for(int column = 0; column < 8; column++){      //iterate through each column  
444:    for(int i = 0; i < 8; i++){               
445:      digitalWrite(rowA[i], LOW);           //turn off all row pins   
446:    }  
447:    for(int i = 0; i < 8; i++){ //Set only the one pin  
448:     if(i == column){   digitalWrite(colA[i], LOW);} //turns the current row on  
449:     else{        digitalWrite(colA[i], HIGH); }//turns the rest of the rows off  
450:    }  
451:    
452:    for(int row = 0; row < 8; row++){          //iterate through each pixel in the current column  
453:    int bit = (data[column] >> row) & 1;  
454:    if(bit == 1){   
455:      digitalWrite(rowA[row], HIGH);          //if the bit in the data array is set turn the LED on  
456:    }  
457:    
458:    }  
459:    delayMicroseconds(pauseDelay);            //leave the column on for pauseDelay microseconds (too high a delay causes flicker)  
460:   }   
461:   }  
462:  }  
463:    
464:  //returns the index of a given character  
465:  //for converting from a string to a lookup in our array of character bitmaps  
466:  int getChar(char character){  
467:   int returnValue = Z;  
468:   switch(character){  
469:   case 'A': returnValue = A; break;  
470:   case 'a': returnValue = A; break;  
471:   case 'B': returnValue = B; break;  
472:   case 'b': returnValue = B; break;  
473:   case 'C': returnValue = C; break;  
474:   case 'c': returnValue = C; break;  
475:   case 'D': returnValue = D; break;  
476:   case 'd': returnValue = D; break;  
477:   case 'E': returnValue = E; break;  
478:   case 'e': returnValue = E; break;  
479:   case 'F': returnValue = F; break;  
480:   case 'f': returnValue = F; break;  
481:   case 'G': returnValue = G; break;  
482:   case 'g': returnValue = G; break;  
483:   case 'H': returnValue = H; break;  
484:   case 'h': returnValue = H; break;  
485:   case 'I': returnValue = I; break;  
486:   case 'i': returnValue = I; break;  
487:   case 'J': returnValue = J; break;  
488:   case 'j': returnValue = J; break;  
489:   case 'K': returnValue = K; break;  
490:   case 'k': returnValue = K; break;  
491:   case 'L': returnValue = L; break;  
492:   case 'l': returnValue = L; break;  
493:   case 'M': returnValue = M; break;  
494:   case 'm': returnValue = M; break;  
495:   case 'N': returnValue = N; break;  
496:   case 'n': returnValue = N; break;  
497:   case 'O': returnValue = O; break;  
498:   case 'o': returnValue = O; break;  
499:   case 'P': returnValue = P; break;  
500:   case 'p': returnValue = P; break;  
501:   case 'Q': returnValue = Q; break;  
502:   case 'q': returnValue = Q; break;  
503:   case 'R': returnValue = R; break;  
504:   case 'r': returnValue = R; break;  
505:   case 'S': returnValue = S; break;  
506:   case 's': returnValue = S; break;  
507:   case 'T': returnValue = T; break;  
508:   case 't': returnValue = T; break;  
509:   case 'U': returnValue = U; break;  
510:   case 'u': returnValue = U; break;  
511:   case 'V': returnValue = V; break;  
512:   case 'v': returnValue = V; break;  
513:   case 'W': returnValue = W; break;  
514:   case 'w': returnValue = W; break;  
515:   case 'X': returnValue = X; break;  
516:   case 'x': returnValue = X; break;  
517:   case 'Y': returnValue = Y; break;  
518:   case 'y': returnValue = Y; break;  
519:   case 'Z': returnValue = Z; break;  
520:   case 'z': returnValue = Z; break;  
521:   case ' ': returnValue = _; break;  
522:   case '3': returnValue = B2; break;  
523:   case '<': returnValue = TEMP; break;  
524:   case '*': returnValue = FULL; break;  
525:   case '|': returnValue = LINE; break;   
526:   case '_': returnValue = _; break;   
527:   case ':': returnValue = COL; break;   
528:   case '-': returnValue = DASH; break;   
529:   case ')': returnValue = BRA2; break;   
530:   case '%': returnValue = SMILE; break;   
531:   case '.': returnValue = DOT; break;    
532:   case '^': returnValue = COLDOT; break;     
533:   }  
534:   return returnValue;  
535:  }  
The code was borrowed from http://oomlout.com/8X8M/8X8M-ScrollMessage.txt by oomlout.com who supply LED matrices and other goodies, and it was then adjusted by me to make my own characters.

I'm planning to extend the range of characters to numbers and lower case and maybe more!

Here's the result:
You may not see the effect on the video as much as with the eye, but the horizontals which have a lot of LEDs lit up, are (only slightly) feinter than those with just a few LEDs.  Actually the horizontals are in fact the matrix's columns and the verticals are the matrix's rows.  Because the LEDs in the columns (horizontals in the video) are being powered together - and through the 8 resistors - more LEDs lit up means more of a drain on the available current in each column, so the LEDs get slightly dimmer.

There's just no end to what you can do with an 8 x 8!!

The video doesn't do maximum justice to the super red colour of the LEDs, but you can see what's going on...





No comments:

Post a Comment