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