#include <FastLED.h>
#define LED_PIN 7
#define NUM_LEDS 6
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(115200);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
pinMode(A0, INPUT);
}
float lasttemp = 0;
float lasttf = 0;
int red(float temp) { // Defines the red component of the color temperature
int r = (int)min(temp*255*2,255);
return r;
}
int green(float temp){ // Defines the green component of the color temperature
int g;
if(temp>0.5){
g =(int)(255 - (temp-0.5)*2*255);
} else g = 255;
return g;
}
int blue(float temp){ // Defines the blue component of the color temperature
int b = (int)max((255-temp*255*2),0);
return b;
}
void pulse(){
// Reads the temperature (or voltage) and defines start and end of color pulse:
float sensorValue = analogRead(A0);
float temp = (sensorValue/1023); // Float from 0 to 1
float t0 = lasttf;
float tf = max(temp-0.2,0);
int r;
int g;
int b;
// Pulse-in (100 steps of 4ms):
for (int i = 0; i<100; ++i){
r = red(((t0*(100-i))+(temp*i))/100);
g = green(((t0*(100-i))+(temp*i))/100);
b = blue(((t0*(100-i))+(temp*i))/100);
for (int j = 0; j<NUM_LEDS; ++j){
leds[j] = CRGB(r, g, b);
}
FastLED.show();
delay(6);
}
// Pulse-out (100 steps of 10ms):
for (int i = 0; i<100; ++i){
r = red(((temp*(100-i))+(tf*i))/100);
g = green(((temp*(100-i))+(tf*i))/100);
b = blue(((temp*(100-i))+(tf*i))/100);
for (int j = 0; j<NUM_LEDS; ++j){
leds[j] = CRGB(r, g, b);
}
FastLED.show();
delay(6);
}
// Stores the current temp to smooth in next pulse:
lasttemp = temp;
lasttf = tf;
}
void pulseWhite(){
//Max brightness 200
// Pulse-in:
for (int i = 0; i<200; ++i){
for (int j = 0; j<NUM_LEDS; ++j){
leds[j] = CRGB(i/1.5, i, i);
}
FastLED.show();
delay(3);
}
// Pulse-out:
for (int i = 200; i>0; --i){
for (int j = 0; j<NUM_LEDS; ++j){
leds[j] = CRGB(i/1.5, i, i);
}
FastLED.show();
delay(8);
}
}
void wave(){ //Wave of light from one end to the other, with fade in and out effects
// Reads the temperature (or voltage) and defines start and end of color pulse:
float sensorValue = analogRead(A0);
float temp = (sensorValue/1023); // Float from 0 to 1
float t0 = lasttf;
float tf = max(temp-0.2,0);
int r;
int g;
int b;
r = red(temp);
g = green(temp);
b = blue(temp);
//Number of steps the wave takes to complete
int steps = 100;
for(int i = 0; i<=steps; ++i){
for (int j = 0; j<NUM_LEDS; ++j){
//For each LED, calculate its brightness in each step
double completion = static_cast<double>(i)/static_cast<double>(steps);
double distance = static_cast<double>(j+1)/NUM_LEDS;
double brightness = 1- min(abs(completion-0.25-distance/2)*3.5,1);
leds[j] = CRGB(r*brightness, g*brightness, b*brightness);
}
FastLED.show();
delay(10);
}
// Stores the current temp to smooth in next pulse:
lasttemp = temp;
lasttf = tf;
}
void colorWave(){ //Wave of light from one end to the other, changing color in real time
int r;
int g;
int b;
//Number of steps the wave takes to complete
int steps = NUM_LEDS*10;
for(int i = 0; i<=steps; ++i){
// Reads the temperature (or voltage) and defines color:
float sensorValue = analogRead(A0);
float temp = (sensorValue/1023);
r = red(temp);
g = green(temp);
b = blue(temp);
for (int j = 0; j<NUM_LEDS; ++j){
//For each LED, calculate its brightness in each step
double completion = static_cast<double>(i)/static_cast<double>(steps);
double distance = static_cast<double>(j+1)/NUM_LEDS;
double brightness = 1- min(abs(completion-0.25-distance/2)*3.5,1);
leds[j] = CRGB(r*brightness, g*brightness, b*brightness);
FastLED.show();
//delay(10);
}
}
}
void loop() {
//pulse();
pulseWhite();
//wave();
colorWave();
}