7_3 Countdown Screen Alarm

Countdown Screen Alarm

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port, make sure that your Code Lab Mini Screen Add-On (sold separately) is attached, and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.

If you haven't set up your Code Lab Mini Screen Add-On yet, visit Chapter 0 to watch a video showing you how to do that.


//Set an alarm variable that counts down. When time is up, beep the speaker and show a message on the screen. #include "CLMiniScreen.h" //A code library enables new functions to be used with the screen CLMiniScreen lcd; //the library name is followed by the 'name' you give the LCD screen. Here it is 'lcd' int alarm = 10; //create a variable for the duration of the countdown, in seconds. void setup(){ lcd.begin(); //.begin() 'sets up' the screen pinMode(2,OUTPUT); //speaker } void loop(){ if(alarm == 0){ //if the alarm has reached its end... lcd.clear(); //clear the screen lcd.print("Time's Up!"); //print the message tone(2,500); //play a tone of 500 hertz on the speaker delay(1000); //delay for 1 second with the tone playing noTone(2); //stop the tone delay(1000); //delay 1 second with no tone playing } else{ //otherwise, if the alarm hasn't reached its end... lcd.clear(); //Clear the screen lcd.print("Time Left: "); //Print the message lcd.setCursor(0,1); //Move the cursor to the 2nd line, 1st column of the screen lcd.print(alarm); //Print the value of the variable delay(1000); //Wait 1 second with the alarm value showing alarm--; //Reduce the value of the alarm variable by 1 } } // (c) 2023 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

Coming Soon!