/* Turn on or off one of the jumbo LEDs by passing a shadow over the light sensor */
int shadowCount = 0; //Variable keeping track of the shadows that cross the light senso
void setup() {
pinMode(A2,INPUT); //light sensor
pinMode(11,OUTPUT); //jumbo LED color 11
Serial.begin(9600); //set up the serial monitor
}
void loop() {
if(analogRead(A2)<6){ //if the light sensor reading is below 6...
shadowCount++; //increase the value of the shadow count variable by 1
delay(500); //pause 500 milliseconds (half a second)
}
if(shadowCount == 1){ //if the shadow count variable equals 1...
digitalWrite(11,HIGH); //turn on LED 11
}
else{ //otherwise, if the shadow count variable doesn't equal 1...
digitalWrite(11,LOW); //turn off the LED
shadowCount = 0; //set the variable equal to 0.
}
Serial.println(analogRead(A2)); //show the value of the analogRead the light senso
}
// (c) 2024 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
It looks like your computer isn't set up to code yet.
Success!
Compile errors:
Serial port monitor:
Input:
Walkthrough Video
Watch the video for a line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!
Challenges
Can you complete the challenges? Change the code in your code editor above. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!