Photoresistor

Hello,

Big newbie here. Basically I’m trying to take the example listed on: http://playground.arduino.cc/Learning/PhotoResistor and do the opposite behavior with it. Meaning when the resistor value is lower, the LED is also lower. Bright light in = bright LED. Been playing around with it for a while and can’t seem to get it to work. Any suggestions?

Thanks

This might do the trick.

Since currently it goes from 0 - 255, if you subtract that from 255, it will make it go 255 - 0.

analogWrite(ledPin, 255 - analogRead(lightPin)/4);

Thanks! Makes sense. Also the function:

Serial.println(255-analogRead(lightPin)/4);

seems to work as well.

Problem now is when the value gets lower, the LED starts blinking/flashing like crazy.

Final code with help from a friend:

int prmin = 600;
int prmax = 1000;

int lightPin = A0;
int ledPin=11;

void setup()
{
Serial.begin(9600);
pinMode( lightPin, INPUT);
pinMode( ledPin, OUTPUT );
}

void loop()
{

// input voltage on A0 of 2.5v to 3.3v, clamp and map to an appropriate range

// step 1: read A0
int val = analogRead(lightPin);
// step 2: constrain it to our expected range (based on MATH)
val = constrain(val, prmin, prmax);
// step 3: map the range [511, 675] to the range [0, 255]
val = map(val, prmin, prmax, 0, 255);
// step 4: write the result out to the pwm pin
analogWrite(ledPin, val);
// step 5: diagnostic println()
Serial.println(val);
}