const int sensorPin = 0;
const int ledPin = 9;
int lightLevel, high = 0, low = 1023;
void setup()
{
pinMode(ledPin, OUTPUT);
// Serial.begin(9600);
}
void loop()
{
lightLevel = analogRead(sensorPin);
// lightLevel = map(lightLevel, 0, 1023, 0, 255);
// lightLevel = constrain(lightLevel, 0, 255);
manualTune();
//autoTune();
analogWrite(ledPin, lightLevel);
// Serial.print(lightLevel);
// Serial.println("");
// delay(500);
}
void manualTune()
{
lightLevel = map(lightLevel, 300, 800, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}
void autoTune()
{
if (lightLevel < low)
{
low = lightLevel;
}
if (lightLevel > high)
{
high = lightLevel;
}
lightLevel = map(lightLevel, low+0, high-30, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
}