8_4 Tilt to Increase Pitch

Tilt to Increase Pitch

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 right to increase the pitch playing on the speaker, and hit a button to reset pitch int pitch = 50; //pitch variable starts at 50 void setup() { pinMode(A5,INPUT_PULLUP); //Right tilt sensor pinMode(6,INPUT_PULLUP); //"reset" button 6 pinMode(2,OUTPUT); //Speaker } void loop() { if(digitalRead(A5)==LOW){ //if the sound reading is greater than 600 pitch++; //increase the variable by 1 delay(10); //Pause for 1/10th of a second to slow the speed of the tone of the fade up } tone(2,pitch); //play the variable as the hertz value on the speaker if(digitalRead(6)==LOW){ //if button 6 is pressed noTone(2); //Stop any tones on the speaker pitch = 50; //reset the variable to 50 } } // (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!