Blaster Buttons

Learn to blast your laser LEDs into space to break up obstacles at the press of a button.

Code

The code below already works and is ready to upload! Upload the code to Code Rocket and see what happens. Then, you'll 'take apart' the code to learn what each piece of the program does.

/* Light up the Blaster LEDs when the corresponding button is pressed */ void setup(){ pinMode(7,OUTPUT); //Blaster LED pinMode(8,OUTPUT); //Blaster LED pinMode(12,INPUT_PULLUP); //Button pinMode(11,INPUT_PULLUP); //Button } void loop(){ if(digitalRead(12)==LOW){ //if button 12 is pressed digitalWrite(8,HIGH); //turn on LED 8 } if(digitalRead(11)==LOW){ //if button 11 is pressed digitalWrite(7,HIGH); //turn on LED 7 } //the lights will turn off each and every loop, but it will be so fast you won't see it if the button is held. digitalWrite(7,LOW); digitalWrite(8,LOW); } // (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Walkthrough Videos

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!

Challenges

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.

Concepts

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: Multiple ‘if’ statements

In this program, you see that the code can react to two different inputs (the only two inputs you have on your Code Rocket) and react differently to each of them. Every time the void loop runs, the ‘condition’ of each ‘if’ statement is checked and the ‘action’ is run if the condition is true. If both of the conditions are true, both actions will run on your Code Rocket.

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 code commands can run as an 'action' inside an 'if' statement?




2. How many 'if' statements can you have in a single program?