6_3 Tilt Toggle LED

Tilt Toggle LED

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 Sensor Trio 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 Sensor Trio Add-On yet, visit Chapter 0 to watch a video showing you how to do that.


//Tilt Code Lab Mini to the left to turn on LED 8, turn it to the right to turn off LED 8 int light = 0; //integer variable to control whether the LED is on or off void setup(){ pinMode(8,OUTPUT); //LED pinMode(A4,INPUT_PULLUP); //left tilt sensor pinMode(A5,INPUT_PULLUP); //right tilt sensor } void loop(){ if(digitalRead(A4)==LOW){ //If Code Lab Mini is tilted left light = 1; //set the variable to 1 } else if(digitalRead(A5)==LOW){ //Otherwise, if Code Lab Mini is tilted right light = 0; //set the variable to 0 } digitalWrite(8,light); //Use the variable to turn the LED on or off } // (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!