When the big moment comes for takeoff, all systems must be tested and everything has to go just right! Program the perfect countdown sequence to ensure a successful liftoff.

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.

// Countdown to takeoff, then light the boosters and use static as your engine noise void setup(){ //the countdown only happens once when the board is turned on pinMode(2,OUTPUT); //speaker pinMode(3,OUTPUT); //LED pinMode(4,OUTPUT); //LED pinMode(5,OUTPUT); //LED pinMode(6,OUTPUT); //LED pinMode(9,OUTPUT); //Booster pinMode(10,OUTPUT); //Booster digitalWrite(6,HIGH); //Turn on LED digitalWrite(5,HIGH); //Turn on LED digitalWrite(4,HIGH); //Turn on LED digitalWrite(3,HIGH); //Turn on LED delay(3000); digitalWrite(6,LOW); //Start the countdown by turning off LED 6 tone(2,800); delay(1000); //Wait 1 second digitalWrite(5,LOW); tone(2,700); delay(1000); //Wait 1 second digitalWrite(4,LOW); tone(2,600); delay(1000); //Wait 1 second digitalWrite(3,LOW); tone(2,500); delay(1000); //Wait 1 second digitalWrite(9,HIGH); // turn on LED9 digitalWrite(10,HIGH); //turn on LED10 } void loop(){ //the 'flying' actions repeat inside the loop tone(2,150); //playing two similar tones really close together makes a rumbling sound like an engine delay(1); tone(2,50); delay(1); } // (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 challenge? 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: Using void setup as an active part of your program

Up to this point, the code in void setup has been ‘behind the scenes’. It was used for pinMode functions, mostly, as a way to ensure your circuit was set up correctly for whatever program you were about to run. In this code, though, void setup takes up most of the program! But just like a rocket liftoff, sometimes the one-time sequence is the most important part. Try to think of other creative ways to use the void setup, like an ‘introduction song’ that plays when you first plug in your Code Rocket.

New Concept: Logical planning

When working on a program like this that has a sequence of steps that must be followed, it can be easy to get a little lost. There are two approaches that will help you think through the logic of your program before you write it.

1) Code and Upload (often)! Once you have the bare minimum of a program - void setup(){} and void loop(){} - you can upload your code to the Code Rocket and see what it does. You don’t need to complete the entire program before trying to upload it. If you upload a little bit of code, then add more, then upload again, you might find something that was wrong much earlier than you otherwise would have.

2) Screen-free planning. You can write down the steps you want Code Rocket to follow, draw diagrams, or write a story of how you want Code Rocket to work before you ever touch the keyboard. Sometimes the syntax and challenges of writing code get in the way of the original idea. Rather than focusing on what exactly the code will look like, first think through the steps and make a guide for yourself. The ‘story’ for this example program might be:

“Start with all of the LED circle lights on. Each second, turn one off and play a tone that gets lower. After all of the circle LEDs are off, turn on the booster LED lights for takeoff. Make some kind of sound effect like the rocket’s engines are roaring as it lifts off.”

This short outline provides the basics of the programs without getting too specific. Then you can use the Code and Upload (often!) technique to turn the outline into a program.


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 code inside the void setup function runs...