Arduino/elegoo-kit-lessons/Lesson 18 Photocell/Photocell/Photocell.ino

37 lines
675 B
Arduino
Raw Permalink Normal View History

2022-02-20 21:05:40 +00:00
//www.elegoo.com
//2016.12.9
int lightPin = 0;
int latchPin = 11;
int clockPin = 9;
int dataPin = 12;
int leds = 0;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
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();
}