-
Notifications
You must be signed in to change notification settings - Fork 46
/
fade_toward_solid_color.ino
106 lines (82 loc) · 3.25 KB
/
fade_toward_solid_color.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/****************************************************************
This example displays a color on all pixels, then blends the
display to a new color and holds it for a bit. Then repeats...
The serial monitor will print out the target color when a new
one is picked.
Marc Miller, Jan 2020
****************************************************************/
#include "FastLED.h"
#define LED_TYPE LPD8806
#define COLOR_ORDER GRB
#define DATA_PIN 11
#define CLOCK_PIN 13 // ignore for clockless pixel types
#define NUM_LEDS 32
uint8_t BRIGHTNESS = 128; // master brightness [0-255]
CRGB leds[NUM_LEDS];
CRGB currentColor(130,25,25); // starting color
CRGB targetColor(30,40,130); // starting target color
const uint8_t fadeRate = 42; // larger number is a slower fade
boolean fadeToColor = 1; // turns on/off the fading toward target
//===============================================================
void setup() {
Serial.begin(115200);
delay(3000); // (optional) safety start up delay
//FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS); // set initial master brightness
fill_solid(leds, NUM_LEDS, currentColor);
FastLED.show(); // update the display
Serial.println("\nSetup done.\n");
}
//===============================================================
void loop() {
EVERY_N_SECONDS(15) {
newTargetColor(); // set the new target color
fadeToColor = 1; // start fading toward target
}
EVERY_N_MILLISECONDS(fadeRate) {
if (currentColor == targetColor) {
fadeToColor = 0; // stop fading
}
if (fadeToColor) {
nblendU8TowardU8( currentColor.r, targetColor.r);
nblendU8TowardU8( currentColor.g, targetColor.g);
nblendU8TowardU8( currentColor.b, targetColor.b);
currentColor = CRGB(currentColor.r,currentColor.g,currentColor.b);
}
fill_solid(leds, NUM_LEDS, currentColor);
// For Testing --- always display target on first pixel
leds[0] = targetColor;
FastLED.show();
}
} // end_main_loop
//===============================================================
// Modified helper function that blends one uint8_t toward another,
// based on function from Mark Kriegsman's fadeTowardColor example:
// https://gist.github.com/kriegsman/d0a5ed3c8f38c64adcb4837dafb6e690
void nblendU8TowardU8(uint8_t& current, const uint8_t target)
{
if( current == target) {
return;
}
if( current < target ) {
uint8_t delta = target - current;
delta = scale8_video( delta, 1);
current += delta;
} else {
uint8_t delta = current - target;
delta = scale8_video( delta, 1);
current -= delta;
}
}
//---------------------------------------------------------------
// Pick a new target color to fade toward
void newTargetColor() {
random16_add_entropy( random() );
targetColor = CHSV( random8(), 255, 255);
Serial.print("New targetColor ");
Serial.print("\tr: "); Serial.print(targetColor.r);
Serial.print("\tg: "); Serial.print(targetColor.g);
Serial.print("\tb: "); Serial.println(targetColor.b);
}
//---------------------------------------------------------------