add a smoother transition when color changes

master
shark 6 years ago
parent 1492a4148d
commit 9e68874c7b

@ -1,8 +1,9 @@
#include <FastLED.h>
int light = 0;
int r = 9;
int r = 9; // define pins
int g = 10;
int b = 11;
int oldavg = 0;
void setup() {
Serial.begin(9600);
}
@ -15,12 +16,32 @@ void loop() {
delay(3);
}
average /= ss;
CRGB color = CHSV(average, 255, 255);
if (oldavg == 0) {
oldavg = average;
}
digitalWrite(13, HIGH);
if (oldavg != average) {
Serial.print("Goal: ");
Serial.println(average);
}
while (oldavg < average) {
oldavg ++;
setColor(oldavg);
}
while (oldavg > average) {
oldavg --;
setColor(oldavg);
}
digitalWrite(13, LOW);
}
void setColor(int hue) {
CRGB color = CHSV(hue, 255, 255);
analogWrite(9, color.r);
analogWrite(10, color.g);
analogWrite(11, color.b);
Serial.print("Hue: ");
Serial.print(average);
Serial.print(hue);
Serial.print(", RGB : ");
Serial.print(color.r);
Serial.print(", ");
@ -31,11 +52,14 @@ void loop() {
// END OF CODE
/* Library free version for regular RGB LEDs (tinkercad), using HSV --> RGB code not by me
/*// Library free version for regular RGB LEDs (tinkercad), using HSV --> RGB code not by me
//#include <FastLED.h>
int light = 0;
int oldavg = 0;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
@ -46,15 +70,34 @@ void loop() {
delay(3);
}
average /= ss;
if(oldavg == 0) {
oldavg = average;
}
digitalWrite(13, HIGH);
if(oldavg != average) {
Serial.print("Goal: ");
Serial.println(average);
}
while(oldavg < average) {
oldavg ++;
setColor(oldavg);
}
while(oldavg > average) {
oldavg --;
setColor(oldavg);
}
digitalWrite(13, LOW);
}
void SetColor(int hue) {
byte RedLight;
byte GreenLight;
byte BlueLight;
// this is the algorithm to convert from RGB to HSV
byte h = average;
byte h = hue;
byte s = 255;
byte v = 60;
h = (h * 192) / 256; // 0..191
h = (h * 192) / 256; // ..191
unsigned int i = h / 32; // We want a value of 0 thru 5
unsigned int f = (h % 32) * 8; // 'fractional' part of 'i' 0..248 in jumps
@ -98,7 +141,7 @@ byte BlueLight;
}
//CRGB& color = CHSV(average, 255, 255);
Serial.print("Hue: ");
Serial.print(average);
Serial.print(hue);
Serial.print(", RGB : ");
Serial.print(RedLight);
Serial.print(", ");

Loading…
Cancel
Save