Motors PWM by photoresistors - code

This code allows us to control 2 motors with motor controller, arduino and potentiometer. Hovewer I would like to change potentiometers with photoresistors. What should be changed/corrected in a code? Sorry for bad english :confused:

int knobvalue; int potpin = 4; int potpinn = 5;

void setup() { pinMode(11, OUTPUT); pinMode(9, OUTPUT); pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(7, OUTPUT); pinMode(6, OUTPUT); }

void loop() { knobvalue = analogRead(potpin); knobvalue = map(knobvalue, 0, 1023, 0, 255); analogWrite(11, knobvalue); knobvalue = analogRead(potpinn); knobvalue = map(knobvalue, 0, 1023, 0, 255); analogWrite(9, knobvalue); digitalWrite(13, HIGH); digitalWrite(12, HIGH); digitalWrite(7, LOW); digitalWrite(8, LOW); }

Well that depends. If you make a resisitive divider with a photoresistor and a fixed resistor of the right value nothing might need changing... It depends what kind of response to light you are looking for - fairly linear or logarithmic?

I am looking for fairly linear response to light.

Replace the pot with an ldr and appropriate resistor in series with it (something more than resistance of ldr in bright light, less than it in dark light - this is something you may want to tweak)

Then, you'll need to adjust the code to get the right response curve. This will take experimenting; ldr's aren't precision devices.

If you actually need it linear, the bh1750 is a cheap i2c light sensor that gives a much more accurate measure of light level.

Photo diodes are more linear, use them reverse biased in series with a resistor to ground so you can measure the current directly, this is linear in illumination intensity until saturation. Photodiode cathode to 5V, anode to 100k resistor and analog pin, other end of resistor to ground. Add 100nF cap across the resistor to reduce noise and make the signal low impedance.

Well, in theory, and try different resistor values.

Phototransistors will also work, but are less linear I think.