From 9e68874c7b7e448664be2768c1581955d6549f50 Mon Sep 17 00:00:00 2001 From: shark Date: Sat, 17 Nov 2018 19:09:51 +0000 Subject: [PATCH] add a smoother transition when color changes --- acrylic-art-code/acrylic-art-code.ino | 65 ++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/acrylic-art-code/acrylic-art-code.ino b/acrylic-art-code/acrylic-art-code.ino index 12ce93a..34cd60e 100644 --- a/acrylic-art-code/acrylic-art-code.ino +++ b/acrylic-art-code/acrylic-art-code.ino @@ -1,8 +1,9 @@ #include 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(", "); @@ -28,14 +49,17 @@ void loop() { Serial.print(", "); Serial.println(color.b); } - - // END OF CODE - -/* Library free version for regular RGB LEDs (tinkercad), using HSV --> RGB code not by me + + // END OF CODE + + + /*// Library free version for regular RGB LEDs (tinkercad), using HSV --> RGB code not by me //#include 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(", "); @@ -108,4 +151,4 @@ byte BlueLight; analogWrite(9, RedLight); analogWrite(10, GreenLight); analogWrite(11, BlueLight); -}*/ +}*/ \ No newline at end of file