@@ -12,71 +12,49 @@ void setup() {
12
12
13
13
void loop () {
14
14
// try hovering over these functions to see the comments!
15
- gradientRed (1.5 , 10 );
16
- blinkingPatterns (2 , 2 , 10 );
17
- fadeInAndOut (1.5 , 1.5 );
15
+ rainbow (20 );
16
+ // Other functions to try after rainbow()
17
+ // blinkingPatterns(2, 2, 10);
18
+ // fadeInAndOut(1.5, 1.5);
18
19
}
19
20
20
21
/* *
21
22
* Parameters:
22
- * - "seconds": a number that describes how long you want each "iteration" to be
23
- * - "numSteps": a number that describes how many times you want the red color to change
23
+ * - "seconds": a number that describes how long you want the rainbow gradient to be
24
24
* ---
25
- * Function Description: This function sets your LED to white (aka all RGB values to 255) and decreases
26
- * the red RGB value by 10 a "numSteps" amount of times . It will decrease the value every "seconds" seconds.
25
+ * Function Description: This function rotates your LED through the RGB colors (from red to green to blue) by
26
+ * decreasing each pixel by a value of 1 . It will go through the entire rainbow of colors is "seconds" seconds.
27
27
* ---
28
- * Example Function Call: `gradientRed(2,4 )` --> this decreases the red RBG value every 2 seconds, for 4 times
28
+ * Example Function Call: `rainbowStep(30 )` --> creates a rainbow that lasts 30 seconds.
29
29
*/
30
- void gradientRed (float seconds, int numSteps ) {
31
- int Red = 250 ;
32
- int Green = 250 ;
33
- int Blue = 250 ;
34
-
35
- for ( int step = 0 ; step < numSteps; step++) {
36
- Red -= 50 ;
37
- lantern. setColor ( seconds, lantern. color (Red, Green, Blue)) ;
30
+ void rainbow (float seconds) {
31
+ int red = 0 ;
32
+ int green = 0 ;
33
+ int blue = 0 ;
34
+
35
+ // Set default value of 0.25 if an invalid number is given
36
+ if (seconds <= 0 ) {
37
+ seconds = 0.25 ;
38
38
}
39
- }
40
39
41
- /*
42
- * Parameters:
43
- * - "seconds": a number that describes how long you want each "iteration" to be
44
- * - "numSteps": a number that describes how many times you want the green color to change
45
- * ---
46
- * Function Description: This function sets your LED to white (aka all RGB values to 255) and decreases
47
- * the green RGB value by 10 a "numSteps" amount of times. It will decrease the value every "seconds" seconds.
48
- * ---
49
- * Example Function Call: `gradientGreen(2,4)` --> this decreases the green RBG value every 2 seconds, for 4 times
50
- */
51
- void gradientGreen (float seconds, int numSteps) {
52
- int Red = 250 ;
53
- int Green = 250 ;
54
- int Blue = 250 ;
40
+ const int steps = 255 ; // Number of steps for each color in the LED
41
+ const int num_led = 3 ; // Number of colors per LED (RGB)
42
+ const int total_steps = steps * num_led; // Total steps in the gradient
43
+ const float time_per_color = seconds / total_steps; // Time for each color step
55
44
56
- for (int step = 0 ; step < numSteps; step++ ) {
57
- Green -= 50 ;
58
- lantern.setColor (seconds , lantern.color (Red, Green, Blue ));
45
+ for (red = 255 ; red > 0 ; red -- ) {
46
+ green += 1 ;
47
+ lantern.setColor (time_per_color , lantern.color (red, green, blue ));
59
48
}
60
- }
61
49
62
- /*
63
- * Parameters:
64
- * - "seconds": a number that describes how long you want each "iteration" to be
65
- * - "numSteps": a number that describes how many times you want the blue color to change
66
- * ---
67
- * Function Description: This function sets your LED to white (aka all RGB values to 255) and decreases
68
- * the blue RGB value by 10 a "numSteps" amount of times. It will decrease the value every "seconds" seconds.
69
- * ---
70
- * Example Function Call: `gradientBlue(2,4)` --> this decreases the blue RBG value every 2 seconds, for 4 times
71
- */
72
- void gradientBlue (float seconds, int numSteps) {
73
- int Red = 250 ;
74
- int Green = 250 ;
75
- int Blue = 250 ;
50
+ for (green = 255 ; green > 0 ; green --) {
51
+ blue += 1 ;
52
+ lantern.setColor (time_per_color, lantern.color (red, green, blue));
53
+ }
76
54
77
- for (int step = 0 ; step < numSteps; step++ ) {
78
- Blue -= 50 ;
79
- lantern.setColor (seconds , lantern.color (Red, Green, Blue ));
55
+ for (blue = 255 ; blue > 0 ; blue -- ) {
56
+ red += 1 ;
57
+ lantern.setColor (time_per_color , lantern.color (red, green, blue ));
80
58
}
81
59
}
82
60
@@ -194,4 +172,4 @@ void pulsatingEffect(int seconds, int numPulses) {
194
172
lantern.setColor (seconds, lantern.color (Red, Green, Blue));
195
173
}
196
174
}
197
- }
175
+ }
0 commit comments