beautify code and add a few fixes

master
shark 6 years ago
parent d9733c95c0
commit daf46856eb

@ -1,4 +1,4 @@
#include <FastLED.h> #include < FastLED.h >
int light = 0; int light = 0;
int r = 9; // define pins int r = 9; // define pins
int g = 10; int g = 10;
@ -6,40 +6,41 @@ int b = 11;
int oldavg = 0; int oldavg = 0;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
pinMode(13, OUTPUT);
} }
void loop() { void loop() {
int ss = 20; // sample size for light sensor int ss = 20; // sample size for light sensor
int average = 0; int average = 0;
for (int x = 0; x < ss; x++) { for (int x = 0; x < ss; x++) {
//average += 256-log(256-analogRead(0) / 4) * (256 /5.55); // old linearization //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; average += (1250000.0 * pow(1023 - analogRead(0), -1.4059) - 73) / 2.08;
delay(3); delay(3);
} }
average /= ss; average /= ss;
//Serial.println(average); //Serial.println(average);
if(oldavg == 0) { if (oldavg == 0) {
oldavg = average; oldavg = average;
} }
digitalWrite(13, HIGH); digitalWrite(13, HIGH);
if(oldavg != average) { if (oldavg != average) {
Serial.print("Goal: "); Serial.print("Goal: ");
Serial.println(average); Serial.println(average);
} }
while(oldavg < average) { while (oldavg < average) {
oldavg += 1; oldavg += 1;
setColor(oldavg); setColor(oldavg);
} }
while(oldavg > average) { while (oldavg > average) {
oldavg -= 1; oldavg -= 1;
setColor(oldavg); setColor(oldavg);
} }
digitalWrite(13, LOW); digitalWrite(13, LOW);
} }
void setColor(int hue) { void setColor(int hue) {
CRGB color = CHSV(hue, 255, 255); CRGB color = CHSV(hue, 255, 255);
analogWrite(9, color.r); analogWrite(9, color.r);
analogWrite(10, color.g); analogWrite(10, color.g);
@ -58,7 +59,7 @@ void loop() {
/*// 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> //#include < FastLED.h >
int light = 0; int light = 0;
int oldavg = 0; int oldavg = 0;
void setup() { void setup() {
@ -69,49 +70,49 @@ void setup() {
void loop() { void loop() {
int ss = 20; // sample size for light sensor int ss = 20; // sample size for light sensor
int average = 0; int average = 0;
for (int x = 0; x < ss; x++) { for (int x = 0; x < ss; x++) {
//average += 256-log(256-analogRead(0) / 4) * (256 /5.55); // old linearization //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; average += (1250000.0 * pow(1023 - analogRead(0), -1.4059) - 73) / 2.08;
delay(3); delay(3);
} }
average /= ss; average /= ss;
//Serial.println(average); //Serial.println(average);
if(oldavg == 0) { if (oldavg == 0) {
oldavg = average; oldavg = average;
} }
digitalWrite(13, HIGH); digitalWrite(13, HIGH);
if(oldavg != average) { if (oldavg != average) {
Serial.print("Goal: "); Serial.print("Goal: ");
Serial.println(average); Serial.println(average);
} }
while(oldavg < average) { while (oldavg < average) {
oldavg += 1; oldavg += 1;
setColor(oldavg); setColor(oldavg);
} }
while(oldavg > average) { while (oldavg > average) {
oldavg -= 1; oldavg -= 1;
setColor(oldavg); setColor(oldavg);
} }
digitalWrite(13, LOW); digitalWrite(13, LOW);
} }
void SetColor(int hue) { void SetColor(int hue) {
byte RedLight; byte RedLight;
byte GreenLight; byte GreenLight;
byte BlueLight; byte BlueLight;
// this is the algorithm to convert from RGB to HSV // this is the algorithm to convert from RGB to HSV
byte h = hue; byte h = hue;
byte s = 255; byte s = 255;
byte v = 60; byte v = 60;
h = (h * 192) / 256; // ..191 h = (h * 192) / 256; // 0..191
unsigned int i = h / 32; // We want a value of 0 thru 5 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 f = (h % 32) * 8; // 'fractional' part of 'i' 0..248 in jumps
unsigned int sInv = 255 - s; // 0 -> 0xff, 0xff -> 0 unsigned int sInv = 255 - s; // 0 -> 0xff, 0xff -> 0
unsigned int fInv = 255 - f; // 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 pv = v * sInv / 256; // pv will be in range 0 - 255
byte qv = v * (256 - s * f / 256) / 256; byte qv = v * (256 - s * f / 256) / 256;
byte tv = v * (256 - s * fInv / 256) / 256; byte tv = v * (256 - s * fInv / 256) / 256;

Loading…
Cancel
Save