1 /* Arduino Projects for Dummies
4 * Chapter 6: Making a Scrolling Sign
5 * Creates sprites and text messages using an
6 * 8x8 LED matrix display
8 * This sketch sends a string of text to the display
11 * Adapted from Oomlout.com http://www.tinyurl.com/yhwxv6h
14 char message[] = "HELLO WORLD! "; // The message to display
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
20 // Variables defining each charachters position in an array of integer arrays
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;
30 const int COL =26; const int DASH = 27; const int BRA2 = 28; const int _ = 29; const int LINE = 34;
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;
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))
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};
49 //Each Charachter is an 8 x 7 bitmap where 1 is on and 0 is off
50 const int _A[] = {B0001000,
59 const int _B[] = {B1111110,
68 const int _C[] = {B0011111,
77 const int _D[] = {B1111100,
86 const int _E[] = {B1111111,
95 const int _F[] = {B1111111,
104 const int _G[] = {B0011111,
113 const int _H[] = {B1000001,
122 const int _I[] = {B1111111,
131 const int _J[] = {B0001111,
140 const int _K[] = {B1000011,
149 const int _L[] = {B1000000,
158 const int _M[] = {B1110110,
167 const int _N[] = {B1000001,
176 const int _O[] = {B0011100,
185 const int _P[] = {B0111110,
194 const int _Q[] = {B0011100,
203 const int _R[] = {B1111110,
212 const int _S[] = {B0111111,
221 const int _T[] = {B1111111,
230 const int _U[] = {B1000001,
239 const int _V[] = {B1000001,
248 const int _W[] = {B1000001,
257 const int _X[] = {B1000001,
266 const int _Y[] = {B1000001,
275 const int _Z[] = {B1111111,
284 const int _COL[] ={B0000000,
293 const int _DASH[] = {B0000000,
302 const int _BRA2[] = {B0010000,
311 const int __[] = {B0000000,
320 const int _FULL[] = {B1111111,
329 const int _CHECK[] = {B1010101,
338 const int _B2[] = {B0111110,
347 const int _TEMP[] = {B0000011,
356 const int _LINE[] = {B0000001,
365 const int _SMILE[] = {B0000000,
375 const int _DOT[] = {B0000000,
384 const int _COLDOT[] = {B0000000,
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};
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};
400 // Setup runs once when power is applied
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);
412 displaySprite(data, 40); // 40 milliseconds delay
415 // Loads the current scroll state frame into the data[] display array
417 int currentChar = getChar(message[index]);
418 int nextChar = getChar(message[index+1]);
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
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
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
438 for(int i = 0; i < 8; i++){ // Set only the one pin
440 digitalWrite(colPin[i], LOW);
441 } // Turns the current row on
443 digitalWrite(colPin[i], HIGH); } // Turns the rest of the rows off
446 for(int row = 0; row < 8; row++){ // Iterate through each pixel in the current column
447 int bit = (data[column] >> row) & 1;
449 digitalWrite(rowPin[row], HIGH); // If the bit in the data array is set, turn the LED on
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
461 int getChar(char charachter){
462 int returnValue = CHECK;
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;
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;