From d0d26492d3d246aa3a9d981c9b6188064e29f926 Mon Sep 17 00:00:00 2001 From: shark Date: Mon, 19 Nov 2018 11:11:58 -0600 Subject: [PATCH] split acrylic art code into separate versions --- .../acrylic-art-code-nolib.ino | 103 ++++++++++++++++ acrylic-art-code/acrylic-art-code.ino | 111 +----------------- 2 files changed, 105 insertions(+), 109 deletions(-) create mode 100644 acrylic-art-code-nolib/acrylic-art-code-nolib.ino diff --git a/acrylic-art-code-nolib/acrylic-art-code-nolib.ino b/acrylic-art-code-nolib/acrylic-art-code-nolib.ino new file mode 100644 index 0000000..fe2d496 --- /dev/null +++ b/acrylic-art-code-nolib/acrylic-art-code-nolib.ino @@ -0,0 +1,103 @@ +// 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() { + int ss = 20; // sample size for light sensor + int average = 0; + for (int x = 0; x < ss; x++) { + //average += 256-log(256-analogRead(0) / 4) * (256 /5.55); // old linearization + + // linearization using resistance to lux relationship + average += (1250000.0 * pow(1023 - analogRead(0), -1.4059) - 73) / 2.08; + delay(3); + } + average /= ss; + //Serial.println(average); + if (oldavg == 0) { + oldavg = average; + } + digitalWrite(13, HIGH); + if (oldavg != average) { + Serial.print("Goal: "); + Serial.println(average); + } + while (oldavg < average) { + oldavg += 1; + setColor(oldavg); + } + while (oldavg > average) { + oldavg -= 1; + 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 = hue; + byte s = 255; + byte v = 60; + h = (h * 192) / 256; // 0..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 + + unsigned int sInv = 255 - s; // 0 -> 0xff, 0xff -> 0 + unsigned int fInv = 255 - f; // 0 -> 0xff, 0xff -> 0 + byte pv = v * sInv / 256; // pv will be in range 0 - 255 + byte qv = v * (256 - s * f / 256) / 256; + byte tv = v * (256 - s * fInv / 256) / 256; + + switch (i) { + case 0: + RedLight = v; + GreenLight = tv; + BlueLight = pv; + break; + case 1: + RedLight = qv; + GreenLight = v; + BlueLight = pv; + break; + case 2: + RedLight = pv; + GreenLight = v; + BlueLight = tv; + break; + case 3: + RedLight = pv; + GreenLight = qv; + BlueLight = v; + break; + case 4: + RedLight = tv; + GreenLight = pv; + BlueLight = v; + break; + case 5: + RedLight = v; + GreenLight = pv; + BlueLight = qv; + break; + } + //CRGB& color = CHSV(average, 255, 255); + Serial.print("Hue: "); + Serial.print(hue); + Serial.print(", RGB : "); + Serial.print(RedLight); + Serial.print(", "); + Serial.print(GreenLight); + Serial.print(", "); + Serial.println(BlueLight); + analogWrite(9, RedLight); + analogWrite(10, GreenLight); + analogWrite(11, BlueLight); +} diff --git a/acrylic-art-code/acrylic-art-code.ino b/acrylic-art-code/acrylic-art-code.ino index 9c3a0f0..5205a58 100644 --- a/acrylic-art-code/acrylic-art-code.ino +++ b/acrylic-art-code/acrylic-art-code.ino @@ -1,4 +1,4 @@ -#include < FastLED.h > +#include int light = 0; int r = 9; // define pins int g = 10; @@ -15,7 +15,7 @@ void loop() { for (int x = 0; x < ss; x++) { //average += 256-log(256-analogRead(0) / 4) * (256 /5.55); // old linearization - //linearization using resistance to lux relationship + // linearization using resistance to lux relationship average += (1250000.0 * pow(1023 - analogRead(0), -1.4059) - 73) / 2.08; delay(3); } @@ -54,110 +54,3 @@ void setColor(int hue) { Serial.print(", "); Serial.println(color.b); } - - // END OF CODE - - - /*// 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() { - int ss = 20; // sample size for light sensor - int average = 0; - for (int x = 0; x < ss; x++) { - //average += 256-log(256-analogRead(0) / 4) * (256 /5.55); // old linearization - - //linearization using resistance to lux relationship - average += (1250000.0 * pow(1023 - analogRead(0), -1.4059) - 73) / 2.08; - delay(3); - } - average /= ss; - //Serial.println(average); - if (oldavg == 0) { - oldavg = average; - } - digitalWrite(13, HIGH); - if (oldavg != average) { - Serial.print("Goal: "); - Serial.println(average); - } - while (oldavg < average) { - oldavg += 1; - setColor(oldavg); - } - while (oldavg > average) { - oldavg -= 1; - 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 = hue; - byte s = 255; - byte v = 60; - h = (h * 192) / 256; // 0..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 - - unsigned int sInv = 255 - s; // 0 -> 0xff, 0xff -> 0 - unsigned int fInv = 255 - f; // 0 -> 0xff, 0xff -> 0 - byte pv = v * sInv / 256; // pv will be in range 0 - 255 - byte qv = v * (256 - s * f / 256) / 256; - byte tv = v * (256 - s * fInv / 256) / 256; - - switch (i) { - case 0: - RedLight = v; - GreenLight = tv; - BlueLight = pv; - break; - case 1: - RedLight = qv; - GreenLight = v; - BlueLight = pv; - break; - case 2: - RedLight = pv; - GreenLight = v; - BlueLight = tv; - break; - case 3: - RedLight = pv; - GreenLight = qv; - BlueLight = v; - break; - case 4: - RedLight = tv; - GreenLight = pv; - BlueLight = v; - break; - case 5: - RedLight = v; - GreenLight = pv; - BlueLight = qv; - break; - } - //CRGB& color = CHSV(average, 255, 255); - Serial.print("Hue: "); - Serial.print(hue); - Serial.print(", RGB : "); - Serial.print(RedLight); - Serial.print(", "); - Serial.print(GreenLight); - Serial.print(", "); - Serial.println(BlueLight); - analogWrite(9, RedLight); - analogWrite(10, GreenLight); - analogWrite(11, BlueLight); -}*/ \ No newline at end of file