linearize light input according to physics (100% accurate)

master
shark 6 years ago
parent 9e68874c7b
commit d9733c95c0

@ -11,25 +11,29 @@ void setup() {
void loop() {
int ss = 20; // sample size for light sensor
int average = 0;
for (int x = 0; x < ss; x++) {
average += analogRead(light) / 4;
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;
if (oldavg == 0) {
//Serial.println(average);
if(oldavg == 0) {
oldavg = average;
}
digitalWrite(13, HIGH);
if (oldavg != average) {
Serial.print("Goal: ");
Serial.println(average);
if(oldavg != average) {
Serial.print("Goal: ");
Serial.println(average);
}
while (oldavg < average) {
oldavg ++;
while(oldavg < average) {
oldavg += 1;
setColor(oldavg);
}
while (oldavg > average) {
oldavg --;
while(oldavg > average) {
oldavg -= 1;
setColor(oldavg);
}
digitalWrite(13, LOW);
@ -65,25 +69,29 @@ void setup() {
void loop() {
int ss = 20; // sample size for light sensor
int average = 0;
for (int x = 0; x < ss; x++) {
average += analogRead(light) / 4;
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);
Serial.print("Goal: ");
Serial.println(average);
}
while(oldavg < average) {
oldavg ++;
oldavg += 1;
setColor(oldavg);
}
while(oldavg > average) {
oldavg --;
oldavg -= 1;
setColor(oldavg);
}
digitalWrite(13, LOW);

Loading…
Cancel
Save