In this lesson, you will learn how to measure light intensity using an Analog Input. You will build on lesson 8 and use the level of light to control the number of LEDs to be lit.
int lightPin = 0;
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int leds = 0;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
int reading = analogRead(lightPin);
int numLEDSLit = reading / 57; //1023 / 9 / 2
if (numLEDSLit > 8) numLEDSLit = 8;
leds = 0; // no LEDs lit to start
for (int i = 0; i < numLEDSLit; i++)
{
leds = leds + (1 << i); // sets the i'th bit
}
updateShiftRegister();
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
You can do it, too! Sign up for free now at https://www.jimdo.com