12 Shadow Alarm

Shadow Alarm

/* Turn on or off an alarm by passing a shadow over the light sensor */ int shadowCount = 0; //Variable keeping track of the shadows that cross the light sensor void setup() { pinMode(A2,INPUT); //light sensor pinMode(A5,OUTPUT); //speaker Serial.begin(9600); //set up the serial monitor } void loop() { if(analogRead(A2)<4){ //if the light sensor reading is below 4... 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... tone(A5,500); // start playing a tone of 500 hertz delay(100); //delay 100 milliseconds tone(A5,1000); //play a tone of 1000 hertz delay(100); //delay 100 milliseconds } else{ //otherwise, if the shadow count variable doesn't equal 1... noTone(A5); shadowCount = 0; //set the variable equal to 0. } Serial.println(analogRead(A2)); //show the value of the analogRead the light sensor } // (c) 2024 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

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!


 

TRANSFORMERS and HASBRO and all related trademarks and logos are trademarks of Hasbro, Inc. ©2024 Hasbro.