Customize your Morse code messages in real time when you program one of your Code Rocket’s buttons to ‘beep’ the speaker when pressed!

The code in the editor below already works Just plug in your Code Rocket and press upload to see what it does! Tinker with it and make changes to see what each line of code controls.

/* Make a beep on the speaker when you press button 12. Useful for manual Morse Code messages. */ void setup(){ pinMode(2,OUTPUT);//Speaker pinMode(12,INPUT_PULLUP); //Button } void loop(){ if(digitalRead(12)==LOW){ //if the button sends a LOW (pressed) signal tone(2,500); //play a tone of 500 hertz on pin 2 (speaker) } else{ //otherwise if no LOW signal from button noTone(2); //stop any tones that are playing on the speaker } } // (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Watch the videos for line-by-line explanation of how the example program works. Then you'll be ready to make some changes of your own!


Challenge

Can you complete the challenges? Change the code in the editor above, then upload it to your Code Rocket and see if you got the result. Don't stop here, keep experimenting with the code!

These are the new code concepts covered in this example program. To become a great coder, read through these concepts to learn new vocabulary.

New Concept: Inputs to your code!

Most programs you use daily require some kind of input from you - a password for an account, or maybe a ‘time to cook’ on the microwave. Now you’re integrating inputs into your program and giving your code ‘choices’ about what to do based on the input it receives.


Quiz

If you're having trouble, try to run an experimental program or look at the example code to help you find the answer.

1. The two main parts of an 'if' statement, according to the code walkthrough video, are:




2. Can you have an 'if' statement without an 'else' statement?



3. Can you have an 'else' statement without an 'if' statement?