1./*
2.Adafruit Arduino - Lesson 9. Light sensing
3.*/
4.
5.int lightPin = 0;
6.int latchPin = 5;
7.int clockPin = 6;
8.int dataPin = 4;
9.
10.int leds = 0;
11.
12.void setup()
13.{
14. pinMode(latchPin, OUTPUT);
15. pinMode(dataPin, OUTPUT);
16. pinMode(clockPin, OUTPUT);
17.}
18.
19.void loop()
20.{
21. int reading = analogRead(lightPin);
22. int numLEDSLit = reading / 57; //1023 / 9 / 2
23. if (numLEDSLit > 8) numLEDSLit = 8;
24. leds = 0; // no LEDs lit to start
25. for (int i = 0; i < numLEDSLit; i++)
26. {
27. leds = leds + (1 << i); // sets the i'th bit
28. }
29. updateShiftRegister();
30.}
31.
32.void updateShiftRegister()
33.{
34. digitalWrite(latchPin, LOW);
35. shiftOut(dataPin, clockPin, LSBFIRST, leds);
36. digitalWrite(latchPin, HIGH);
37.}
|