User Manual

179
/
223
Code
After wiring, please open the program in the code folder- Lesson 26 Photocell and
click UPLOAD to upload the program. See Lesson 2 for details about program
uploading if there are any errors.
The first thing to note is that we have changed the name of the analog pin to be
'lightPin' rather than 'potPin' since we no longer have a pot connected.
The only other substantial change to the sketch is the line that calculates how many
of the LEDs to light:
int numLEDSLit = reading / 57; // all LEDs lit at 1k
This time, we divide the raw reading by 57 rather than 114. In other words, we divide
it by half as much as we did with the pot to split it into nine zones, from no LEDs lit
to all eight lit. This extra factor is to account for the fixed 1 kΩ resistor. This means
that when the photocell has a resistance of 1 (the same as the fixed resistor), the
raw reading will be 1023 / 2 = 511. This will equate to all the LEDs being lit and then
a bit (numLEDSLit) will be 8.
Example picture