turns out linearization isn't necessary IRL

master
shark 6 years ago
parent e35fa13802
commit 3608d32d1c

@ -1,5 +1,5 @@
#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;
int b = 11; int b = 11;
@ -13,31 +13,30 @@ 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 // mapping to correct values
average += map(analogRead(0), 0, 1023, 0, 127);
// linearization using resistance to lux relationship
average += map(1250000.0 * pow(1023-analogRead(0), -1.4059), 73, 339, 0, 128);
delay(3); delay(3);
} }
average /= ss; average /= ss;
//Serial.println(analogRead(0));
//Serial.println(average); //Serial.println(average);
if (oldavg == 0) { if (oldavg == 0) {
oldavg = average; oldavg = average;
} }
if (oldavg != average) { if (abs(oldavg - average) > 1) {
Serial.print("Goal: "); Serial.print("Goal: ");
Serial.println(average); Serial.println(average);
digitalWrite(13, HIGH); digitalWrite(13, HIGH);
while (oldavg < average) {
oldavg += 2;
setColor(oldavg);
}
while (oldavg > average) {
oldavg -= 2;
setColor(oldavg);
}
digitalWrite(13, LOW);
} }
while (oldavg < average) {
oldavg += 1;
setColor(oldavg);
}
while (oldavg > average) {
oldavg -= 1;
setColor(oldavg);
}
digitalWrite(13, LOW);
} }
void setColor(int hue) { void setColor(int hue) {
@ -53,4 +52,4 @@ void setColor(int hue) {
Serial.print(color.g); Serial.print(color.g);
Serial.print(", "); Serial.print(", ");
Serial.println(color.b); Serial.println(color.b);
} }
Loading…
Cancel
Save