In this tech demo, seven LEDs arranged in a row light up consecutively and then turn off in the reverse order. The output is activated when the photosensor senses near-darkness.
//LED "ladder" activated by a photosensor //Adapted from https://courses.ideate.cmu.edu/16-223/f2017/text/lib/SoundDemo.html (sound demo) // ================================================================================ const int LED2 = 2; const int LED3 = 3; const int LED4 = 4; const int LED5 = 5; const int LED6 = 6; const int LED7 = 7; const int LED8 = 8; //adapted from https://learn.adafruit.com/photocells/using-a-photocell int photocellPin = A0; int photocellReading; // ================================================================================ void setup() { // Initialize the outputs. pinMode(LED2, OUTPUT); digitalWrite(LED2, LOW); pinMode(LED3, OUTPUT); digitalWrite(LED3, LOW); pinMode(LED4, OUTPUT); digitalWrite(LED4, LOW); pinMode(LED5, OUTPUT); digitalWrite(LED5, LOW); pinMode(LED6, OUTPUT); digitalWrite(LED6, LOW); pinMode(LED7, OUTPUT); digitalWrite(LED7, LOW); pinMode(LED8, OUTPUT); digitalWrite(LED8, LOW); } // ================================================================================ void loop() { photocellReading = analogRead(photocellPin); if (photocellReading < 10) { //if it is very dark, enter the loop for(int i = 0; i < 4; i++) { //Start going up the ladder with all LEDs in off position digitalWrite(LED2, HIGH); delay(50); digitalWrite(LED3, HIGH); delay(250); digitalWrite(LED4, HIGH); delay(450); digitalWrite(LED5, HIGH); delay(650); digitalWrite(LED6, HIGH); delay(850); digitalWrite(LED7, HIGH); delay(1050); digitalWrite(LED8, HIGH); //Now we go down the ladder delay(1250); digitalWrite(LED8, LOW); delay(1300); digitalWrite(LED7, LOW); delay(1350); digitalWrite(LED6, LOW); delay(1400); digitalWrite(LED5, LOW); delay(1450); digitalWrite(LED4, LOW); delay(1500); digitalWrite(LED3, LOW); delay(1550); digitalWrite(LED2, LOW); delay(1600); } } }
Leave a Reply
You must be logged in to post a comment.