7_14 Turn Knob Beep Delay

Turn Knob Beep Delay

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* Use the turn knob to control the delay between beeps of a speaker */ void setup() { pinMode(A0,INPUT); //turn knob pinMode(2,OUTPUT); //speaker } void loop() { int wait = analogRead(A0);//create a variable with the name 'wait' and a value equal to the turn knob reading (range of 0-1023) tone(2,500); //play a tone of 500 hertz on the speaker delay(wait); //delay for 'wait' amount of time. This ranges from 0-1023 milliseconds noTone(2); //stop playing a tone delay(wait); //delay for 'wait' amount of time before starting the loop over again } // (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

Can you complete the challenge? 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!

7_13 Turn Knob Blink Delay

Turn Knob Blink Delay

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* Use the turn knob to control the delay between blinks of an LED */ void setup() { pinMode(A0,INPUT); //turn knob pinMode(8,OUTPUT); //LED } void loop() { int wait = analogRead(A0);//create a variable with the name 'wait' and a value equal to the turn knob reading (range of 0-1024) digitalWrite(8,HIGH); //turn on the LED delay(wait); //delay for 'wait' amount of time. This ranges from 0-1024 milliseconds digitalWrite(8,LOW); //turn off the LED delay(wait); //delay for 'wait' amount of time before starting the loop over again } // (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

Can you complete the challenge? 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!

7_12 Color Tuner

Color Tuner

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/*Use the turn knob and the red, green, and blue buttons to make a custom color on the multicolor LED. Turn the knob to set the intensity level, then press the button to show that color and that intensity on the RGB LED. red button = red light, blue button = blue light, green button = green light*/ int red = 0; //Create three variables named red, green, and blue that are integers (whole numbers) and have a starting value of 0 int green = 0; //These variables will store the different values of the intensity variable in the loop function int blue = 0; void setup(){ pinMode(9,OUTPUT); //LED pinMode(10,OUTPUT); //LED pinMode(11,OUTPUT); //LED pinMode(A0,INPUT); //turn knob pinMode(4,INPUT_PULLUP); //button pinMode(5,INPUT_PULLUP); //button pinMode(6,INPUT_PULLUP); //button } void loop(){ int intensity = analogRead(A0)/4; //Create a variable called 'intensity' that is an integer (whole number) //The intensity variable value takes the reading from the turn knob and divides it by 4, so it has a range of 0 to 255 //analogWrite works on LEDs 9,10,and 11 and has a range of 0-255 if(digitalRead(4)==LOW){ //If the button 4 reading is LOW (the red button is pressed) red = intensity; //Set the value of red to the intensity value //Turning the knob while holding the red button will change the amount of red in the LED } if(digitalRead(5)==LOW){ //If the button 5 reading is LOW (the green button is pressed) green = intensity; //Set the value of green to the intensity value //Turning the knob while holding the green button will change the amount of green in the LED } if(digitalRead(6)==LOW){ //If the button 6 reading is LOW (the blue button is pressed) blue = intensity; //Set the value of blue to the intensity value //Turning the knob while holding the blue button will change the amount of blue in the LED } analogWrite(9, red); //set the LED's red intensity equal to the value of the 'red' variable analogWrite(10, green); //set the LED's green intensity equal to the value of the 'green' variable analogWrite(11, blue); //set the LED's blue intensity equal to the value of the 'blue' variable } // (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

Can you complete the challenge? 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!

7_11 Turn Knob Motor Strength

Turn Knob Motor Strength

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* Use the turn knob to control the strength of the motor */ void setup() { pinMode(A0,INPUT); //turn knob pinMode(3,OUTPUT); //motor } void loop() { int strength = analogRead(A0)/4 ; //Create a strength integer variable //The strength variable value divides the reading from the turn knob by 4, so it has a range of 0 to 255 analogWrite(3,strength); //set the motors strength equal to the variable } // (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

Can you complete the challenge? 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!

7_10 Turn Knob LED Brightness

Turn Knob LED Brightness

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* Use the turn knob to control the brightness of an LED */ void setup() { pinMode(A0,INPUT); //turn knob pinMode(9,OUTPUT); //LED } void loop() { int brightness = analogRead(A0)/4 ; //Create a variable called 'brightness' that is an integer (whole number) //The brightness variable value takes the reading from the turn knob and divides it by 4, so it has a range of 0 to 255 //analogWrite works on pins 3,9,10, and 11 and has a range of 0-255 analogWrite(9,brightness); //set the LED's brightness equal to the value of the 'brightness' variable } // (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

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!

7_9 Button Doubles Tone

Button Doubles Tone

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* Pressing a button doubles the pitch of a speaker tone */ int pitch = 40; //create a variable named 'pitch' that is an integer (whole number) and starting value of 40 (where the speaker starts to sound good) void setup(){ pinMode(2,OUTPUT); //speaker pinMode(4,INPUT_PULLUP); //button } void loop(){ tone(2,pitch); //play a hertz value equal to 'pitch' on the speaker if(digitalRead(4)==LOW){ //If button 4 is pressed pitch=pitch*2; // Double the pitch variable. With no delays, pitch will rise quickly! delay(100); //a delay keeps the pitch from immediately going out of hearing range } if(pitch>10000){ //If pitch is greater than 10,000... pitch=40; //reset the variable to 40 so the scale can start again } } // (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

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!

7_8 Buttons Raise and Lower Motor Strength

Buttons Raise and Lower Motor Strength

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


// Press Button 7 to increase the strength of the motor, and button 4 to decrease it int strength = 0; //a variable named 'strength' that is an integer (whole number) and starting value of 0 void setup(){ pinMode(3,OUTPUT); //motor pinMode(4,INPUT_PULLUP); //button 1 (lowers strength) pinMode(7,INPUT_PULLUP); //button 2 (raises strength) } void loop(){ analogWrite(3,strength); //use the strength variable as the analogWrite strength (0-255) if(digitalRead(7)==LOW){ //If button 7 is pressed strength++; //raise the motor strength delay(20); //a delay to slow down the fade of the strength } if(digitalRead(4)==LOW){ //If button 4 is pressed strength--; //lower the motor strength delay(20); //adjust the delays for a slower, smoother fade of the motor // -- is shorthand for 'subtract one from the variable' } if(strength < 0 || strength > 255){ //if the variable goes below 0 OR above 255 strength = 0; //set it to 0. } } // (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

Can you complete the challenge? 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!

7_7 Button Raises and Lowers Brightness

Buttons Raise and Lower LED Brightness

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* Pressing one button raises the brightness of and LED, pressing another button lowers the brightness */ int brightness = 0; //create a variable named 'brightness' that is an integer (whole number) and starting value of 0 void setup(){ pinMode(9,OUTPUT); //LED. analogWrite() function works on pins 3,9,10,11 and lets you send values from 0 to 255 pinMode(7,INPUT_PULLUP); //button 1 (raises brightness) pinMode(4,INPUT_PULLUP); //button 2 (lowers brightness) } void loop(){ analogWrite(9,brightness); //use the brightness variable to determine how bright the LED is (0-255) if(digitalRead(7)==LOW){ //If the button 7 is pressed brightness++; //increase brightness variable by 1 delay(20); //a delay means you can see the fade. Without it, the light value jumps too quickly } if(digitalRead(4)==LOW){ //If button 4 is pressed brightness--; //lower the brightness on the LED delay(20); //delay allows you to see the fade. // -- is shorthand for 'subtract one from the variable' } } // (c) 2023 Let's Start Coding. License: www.letsstartcoding.com/bsdlicens
 

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 challenge? 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!

7_6 Buttons Raise and Lower Tone

Buttons Raise and Lower Tone

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* Pressing one button raises the tone on a speaker, pressing another button lowers the tone */ int pitch = 40; //create a variable named 'pitch' that is an integer (whole number) and has a starting value of 40 (where the speaker starts to sound good) void setup(){ pinMode(2,OUTPUT); //speaker pinMode(4,INPUT_PULLUP); //button 1 (raises tone) pinMode(7,INPUT_PULLUP); //button 2 (lowers tone) } void loop(){ tone(2,pitch); //play a hertz value equal to 'pitch' on the speaker if(digitalRead(4)==LOW){ //If button 4 is pressed pitch++; //raise pitch by one. With no delays, pitch will rise quickly! } if(digitalRead(7)==LOW){ //If button 7 is pressed pitch--; //lower the tone on the speaker // -- is shorthand for 'subtract one from the variable' } } // (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

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!

7_5 Button Raises Tone

Button Raises Tone

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* Pressing a button raises the tone on a speaker */ int pitch = 40; //create a variable named 'pitch' that is an integer (whole number) and starting value of 40 (where the speaker starts to sound good) void setup(){ pinMode(2,OUTPUT); //speaker pinMode(4,INPUT_PULLUP); //button } void loop(){ tone(2,pitch); //play a hertz value equal to 'pitch' on the speaker if(digitalRead(4)==LOW){ //If button 4 is pressed pitch++; //raise pitch by one. With no delays, pitch will rise quickly! } //Note: You'll have to unplug Code Lab Mini to make the pitch go low again! } // (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

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!

7_4 Blink Delay / 2

Blink Delay / 2

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* Blink an LED with a delay variable that gets cut in half every loop */ int wait = 5000; //Create a variable called 'wait' that is an integer (whole number) and has a starting value of 5000. void setup(){ pinMode(8,OUTPUT); //LED light } void loop(){ digitalWrite(8,HIGH); //turn on the LED light delay(wait); //delay for the number of milliseconds equal to 'wait' variable. First loop=5000, second loop=2500, and so on digitalWrite(8,LOW); //turn off the LED delay(wait); //delay for 'wait' number of milliseconds wait = wait/2; // divide 'wait' variable by 2. The single = sign means you're assigning the variable a new value. } // (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

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!

7_3 Rising Motor Strength

Rising Motor Strength

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


//Use a rising variable for the strength of the motor. int strength = 0; //Create an integer variable called 'strength' void setup() { pinMode(3,OUTPUT); //Motor } void loop() { analogWrite(3,strength); //set the strength of the motor equal to the variable strength++; //Increase the variable value by 1 delay(50); // delay 0.05 seconds before the loop starts again. if(strength>255){ //when strength increases beyond analogWrite's max value of 255 strength=0; //reset the variable to 0 } } // (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

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!

7_2 Rising Brightness Variable

Rising Brightness Variable

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/*Use a rising variable for the brightness of an LED Only LED pins 9, 10, and 11 work with analogWrite on Code Lab Mini, so you must use those pins. */ int brightness = 0; //Create a variable called 'brightness' that is an integer (whole number) and has a starting value of 0 void setup() { pinMode(9,OUTPUT); //LED color } void loop() { analogWrite(9,brightness); //set the brightness of the LED equal to the variable brightness++; //Increase the variable by 1 delay(50); // delay 0.05 seconds before the loop starts again. /*analogWrite has a maximum value of 255, so the code treats 256 like it equals 1, 257 like it equals 2, and so on. The write "overflows" and resets the brightness */ } // (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

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!

7_1 Rising Tone Variable

Rising Tone Variable

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/*Use a rising variable for the tone on a speaker*/ int pitch = 40; //Create a variable called 'pitch' that is an integer (whole number) and has a starting value of 40 void setup() { pinMode(2,OUTPUT); //speaker } void loop() { tone(2,pitch); //use the pitch variable as the hertz value playing on the speaker pitch++; //increase the value of the pitch variable by 1 delay(1000); //delay 1000 milliseconds (1 second) before starting the loop again } // (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

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!

7_0 Serial Print a Counter Variable

Serial Print a Counter Variable

Code

The code in the editor below is ready to run! Plug Code Lab Mini in to your computer's USB port with the cable 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.


/* 'Print' the value of a variable to the serial monitor Don't forget to hit the 'monitor' button on the top toolbar of the code editor after you've uploaded your code! */ int counter = 0; //Create a variable named counter, that is an integer (whole number) and has a starting value of 0 void setup(){ Serial.begin(9600); // set up the serial monitor to show you data } void loop(){ Serial.println(counter); //'print' the value of the counter variable on the serial monitor delay(1000); //delay 1000 milliseconds (1 second) counter++; // add one to the counter variable with ++ } // (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

Can you complete the challenge? 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!