1 /* Arduino Projects for Dummies
4 * Chapter 5: Making a Light Pet
5 * Changes the intensity of output of three LEDs
6 * with different combinations to simulate "moods"
11 // Set which pins will use each color
13 const int greenLED = 10;
14 const int blueLED = 11;
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
24 int newGreenValue = 0;
26 // Assign "utility" variables for the random number, and the fading speed
30 // Assign variables to choose the mood
32 const int moodTime = 10000; // the time in millisections 10000 = 10 seconds
34 // Setup the three LED pins for OUTPUT
36 pinMode(redLED, OUTPUT);
37 pinMode(blueLED, OUTPUT);
38 pinMode(greenLED, OUTPUT);
41 // the main loop of the program
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
48 if (mood == 1){ // if the number picked was 1, run the happy function
51 if (mood == 2){ // if the number picked was 2, run the sad function
54 if (mood > 2){ // if the number picked was anything higher than 2, run the color blending function
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);
70 for (int x=redValue; x>newRedValue; x--) {
71 analogWrite(redLED, x);
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);
87 for (int x=greenValue; x>newGreenValue; x--) {
88 analogWrite(greenLED, x);
92 greenValue=newGreenValue;
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);
103 for (int x=blueValue; x>newBlueValue; x--) {
104 analogWrite(blueLED, x);
108 blueValue=newBlueValue;
112 // the purr function makes all three LEDs pulsate 10 times
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);
121 for(int x=255;x>0;x--){
122 analogWrite(redLED, x);
123 analogWrite(greenLED, x);
124 analogWrite(blueLED, x);
130 // The happy function turns on only the green LED for the mood time
132 for(int x=greenValue;x<255;x++){
133 analogWrite(greenLED, x);
136 for(int x=redValue;x>0;x--){
137 analogWrite(redLED, x);
140 for(int x=blueValue;x>0;x--){
141 analogWrite(blueLED, x);
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);
151 // The sad function turns on only the red LED for the mood time
153 for(int x=redValue;x<255;x++){
154 analogWrite(redLED, x);
157 for(int x=greenValue;x>0;x--){
158 analogWrite(greenLED, x);
161 for(int x=blueValue;x>0;x--){
162 analogWrite(blueLED, x);
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);