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