]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Chapter5/APFD_Chapter5.ino
first commit
[arduino] / books / pdummies / APFD_Chapter5 / APFD_Chapter5.ino
1 /* Arduino Projects for Dummies
2  * by Brock Craft 
3  *
4  * Chapter 5: Making a Light Pet
5  * Changes the intensity of output of three LEDs
6  * with different combinations to simulate "moods" 
7  *
8  * v0.1 30.04.2013
9 */
10
11 // Set which pins will use each color
12 const int redLED = 9;
13 const int greenLED = 10;
14 const int blueLED = 11;
15
16 // Assign variables for the LEDs, current value and the new value to change to
17 int redValue = 0;    // The current value of brightness
18 int newRedValue = 0; // The new value of brightness
19
20 int blueValue = 0;
21 int newBlueValue = 0;
22
23 int greenValue = 0;
24 int newGreenValue = 0;
25
26 // Assign "utility" variables for the random number, and the fading speed
27 int randomValue = 0;
28 int fadeSpeed = 50;
29
30 // Assign variables to choose the mood
31 int mood = 0;
32 const int moodTime = 10000; // the time in millisections 10000 = 10 seconds
33
34 // Setup the three LED pins for OUTPUT
35 void setup(){
36   pinMode(redLED, OUTPUT);
37   pinMode(blueLED, OUTPUT);
38   pinMode(greenLED, OUTPUT);
39 }
40
41 // the main loop of the program
42 void loop() {
43   // first, determine and set the mood of the pet
44   mood = random(20); // randomly pick a number from 0-19 to select what mood the pet should be in
45   if (mood == 0){   // if the number picked was 0, run the purr function
46     purr(); 
47   }
48   if (mood == 1){  // if the number picked was 1, run the happy function
49     happy();
50   }
51   if (mood == 2){  // if the number picked was 2, run the sad function
52     sad(); 
53   }
54   if (mood > 2){   // if the number picked was anything higher than 2, run the color blending function
55     blendColors(); 
56   }
57 }
58
59 void blendColors(){
60
61   // Pick a random value for the red LED
62   newRedValue= random(255); 
63   if (redValue < newRedValue){
64     for (int x=redValue; x<newRedValue; x++) {
65       analogWrite(redLED, x);
66       delay(fadeSpeed);
67     }
68   } 
69   else {
70     for (int x=redValue; x>newRedValue; x--) {
71       analogWrite(redLED, x);
72       delay(20);      
73       delay(fadeSpeed);    
74     } 
75   }
76   redValue=newRedValue;
77
78   // Pick a random value for the green LED
79   newGreenValue= random(255);
80   if (greenValue < newGreenValue){
81     for (int x=greenValue; x<newGreenValue; x++) {
82       analogWrite(greenLED, x);
83       delay(fadeSpeed);
84     }
85   } 
86   else {
87     for (int x=greenValue; x>newGreenValue; x--) {
88       analogWrite(greenLED, x);
89       delay(fadeSpeed);
90     } 
91   }
92   greenValue=newGreenValue;
93
94   // Pick a random value for the blue LED
95   newBlueValue= random(255);
96   if (blueValue < newBlueValue){
97     for (int x=blueValue; x<newBlueValue; x++) {
98       analogWrite(blueLED, x);
99       delay(fadeSpeed);
100     }
101   } 
102   else {
103     for (int x=blueValue; x>newBlueValue; x--) {
104       analogWrite(blueLED, x);
105       delay(fadeSpeed);
106     } 
107   }
108   blueValue=newBlueValue;
109 }
110
111
112   // the purr function makes all three LEDs pulsate 10 times
113 void purr(){
114   for (int count=0;count<10;count++){
115     for(int x=0;x<255;x++){
116       analogWrite(redLED, x);
117       analogWrite(greenLED, x);     
118       analogWrite(blueLED, x);
119       delay(10);
120     }
121     for(int x=255;x>0;x--){
122       analogWrite(redLED, x);
123       analogWrite(greenLED, x);     
124       analogWrite(blueLED, x);
125       delay(10);
126     }
127   }
128 }
129
130 // The happy function turns on only the green LED for the mood time
131 void happy(){
132   for(int x=greenValue;x<255;x++){
133     analogWrite(greenLED, x);
134     delay(fadeSpeed);
135   }
136   for(int x=redValue;x>0;x--){
137     analogWrite(redLED, x);
138     delay(fadeSpeed);
139   }
140   for(int x=blueValue;x>0;x--){
141     analogWrite(blueLED, x);
142     delay(fadeSpeed);
143   }
144   delay(moodTime); // Sets how long the pet will wait in this mood
145   for(int x=255;x>0;x--){
146     analogWrite(greenLED, x);
147     delay(fadeSpeed);
148   }
149 }
150
151 // The sad function turns on only the red LED for the mood time
152 void sad(){
153   for(int x=redValue;x<255;x++){
154     analogWrite(redLED, x);
155     delay(fadeSpeed);
156   }
157   for(int x=greenValue;x>0;x--){
158     analogWrite(greenLED, x);
159     delay(fadeSpeed);
160   }
161   for(int x=blueValue;x>0;x--){
162     analogWrite(blueLED, x);
163     delay(fadeSpeed);
164   }
165   delay(moodTime); // Sets how long the pet will wait in this mood
166   for(int x=255;x>0;x--){
167     analogWrite(redLED, x);
168     delay(fadeSpeed);
169   }
170
171