]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Chapter6_Smiley/APFD_Chapter6_Smiley.ino
clean
[arduino] / books / pdummies / APFD_Chapter6_Smiley / APFD_Chapter6_Smiley.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 just draws a smiley face on the display.
9  *
10  * v0.1 30.04.2013
11  * Adapted from Oomlout.com http://www.tinyurl.com/yhwxv6h
12 */
13
14 // Arduino Pin Definitions
15 int rowPin[] = {2,3,4,5,6,7,8,9};         // An Array defining which Arduino pin each row is attached to
16                                           // (The rows are common anode (driven HIGH))
17 int colPin[] = {17,16,15,14,13,12,11,10}; // An Array defining which pin each column is attached to
18                                           // (The columns are common cathode (driven LOW))
19 byte smile[] = {                          // The array used to hold a bitmap of the display 
20   B00111100,
21   B01000010,
22   B10100101, 
23   B10000001, 
24   B10100101,
25   B10011001, 
26   B01000010, 
27   B00111100};
28
29
30 void setup()
31
32   for(int i = 0; i <8; i++){             // Set the 16 pins used to control the array to be OUTPUTs
33     pinMode(rowPin[i], OUTPUT);          // These correspond to the Arduino pins stored in the arrays
34     pinMode(colPin[i], OUTPUT);
35   }
36 }
37
38 void loop()
39 {
40   displaySprite();                       // Display the Sprite
41 }
42
43 void displaySprite(){
44   for(int count = 0; count < 8; count++){ // A utility counter
45     for(int i = 0; i < 8; i++){                          
46       digitalWrite(rowPin[i], LOW);       // Turn off all row pins  
47     }
48     for(int i = 0; i < 8; i++){           // Activate only the Arduino pin of the column to light up
49       if(i == count){     
50         digitalWrite(colPin[i], LOW);     // Setting this LOW connects the current column's cathode to ground
51       }  
52       else{                
53         digitalWrite(colPin[i], HIGH);    // Setting HIGH Turns all the other rows off
54       }
55     }
56     for(int row = 0; row < 8; row++){       // Iterate through each pixel in the current column
57       int bit = (smile[count] >> row) & 1;  // Use a bit shift in the data[] array to do a bitwise comparison
58                                             // And assign the result of the comparison to the bit
59       if(bit == 1){                         // If the bitwise comparison is 1, 
60         digitalWrite(rowPin[row], HIGH);    // Then light up the LED
61       }
62     }
63   } 
64 }
65