Tackle the big coding topic of variables and see how much easier they can make it to update the way your Morse code LED message works!

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.

/*Use variables for Morse Code messages. This will make it much easier to update the speed of messages Rules of Morse Code: - Dots are 1 'unit' - Dashes are 3 'units' (3 times as long as a dot) - Spaces between symbols within the same letter are 1 'unit' of no signal - Spaces between letters are 3 'units' of no signal - Spaces between words are 7 'units' of no signal */ //all the variables used in this program are 'integers', shortened to int, which means they are whole numbers. int dot = 200; //milliseconds that a dot will last int dash = dot*3; //milliseconds that a dash will last int symbolSpace = dot; // space between symbols are the same duration as a dot int letterSpace = symbolSpace*3; //space between letters are 3 times the duration of symbol spaces int wordSpace = symbolSpace*7; //space between words are 7 times the duration of symbol spaces int LED = 6; void setup(){ pinMode(LED,OUTPUT); //Set the LED as an 'OUTPUT' } void loop(){ //S digitalWrite(LED,HIGH); //Turn on LED delay(dot); //wait one 'dot' with LED on digitalWrite(LED,LOW); //Turn off LED delay(symbolSpace); //wait before the next symbol in a letter digitalWrite(LED,HIGH); //Turn on LED delay(dot); //wait one 'dot' with LED on digitalWrite(LED,LOW); //Turn off LED delay(symbolSpace); //wait before the next symbol in a letter digitalWrite(LED,HIGH); //Turn on LED delay(dot); //wait one 'dot' with LED on digitalWrite(LED,LOW); //Turn off LED delay(letterSpace); //wait before the next symbol in a letter //O digitalWrite(LED,HIGH); //Turn on LED delay(dash); //wait one 'dash' with LED on digitalWrite(LED,LOW); //Turn off LED delay(symbolSpace); //wait before the next symbol in a letter digitalWrite(LED,HIGH); //Turn on LED delay(dash); //wait one 'dash' with LED on digitalWrite(LED,LOW); //Turn off LED delay(symbolSpace); //wait before the next symbol in a letter digitalWrite(LED,HIGH); //Turn on LED delay(dash); //wait one 'dash' with LED on digitalWrite(LED,LOW); //Turn off LED delay(letterSpace); //wait before the next letter in a word //S digitalWrite(LED,HIGH); //Turn on LED delay(dot); //wait one 'dot' with LED on digitalWrite(LED,LOW); //Turn off LED delay(symbolSpace); //wait before the next symbol in a letter digitalWrite(LED,HIGH); //Turn on LED delay(dot); //wait one 'dot' with LED on digitalWrite(LED,LOW); //Turn off LED delay(symbolSpace); //wait before the next symbol in a letter digitalWrite(LED,HIGH); //Turn on LED delay(dot); //wait one 'dot' with LED on digitalWrite(LED,LOW); //Turn off LED delay(wordSpace); //wait before the next word } // (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

How many can you complete? Change the code according to the challenges below. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

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: Variables replace fixed number values

By using variables throughout your loop in this project, you made it much easier to read the code now and modify it later. To speed up the message, you only need to change one value in the program and the rest of the program automatically updates. When you read the Morse code rules at the top of this program and then look at the lines where you created the variables, you can see how similar they are - you’re just turning the rules of Morse code into rules in your computer code!

If you wanted to write out a long Morse code message in computer code, your variables should also make that easier. Instead of having to constantly review the rules, you can type the variables more naturally.


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. What does 'int' stand for when creating a variable?




2. Based on this example program, how would you define 'variable'?