Random Rocket Lights

Even astronauts need to relax sometimes. Use the random function to create a color and light party on the Code Rocket.

Code

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.

//Turn on and off the LEDs in the LED ring randomly with random delays void setup(){ pinMode(3,OUTPUT); //Set each LED as an OUTPUT pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); } void loop(){ digitalWrite(random(3,7),HIGH); //Select LED 3,4,5, or 6 and turn it on delay(random(100,300)); //Delay for 100 - 299 milliseconds digitalWrite(random(3,7),LOW); //Select LED 3,4,5, or 6 and turn it off delay(random(100,300)); //Delay for 100 - 299 milliseconds } // (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 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!


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.

Concept: Random, continued

In this project you’ll continue to explore the random function and see that it can be dropped in place of any number or group of numbers in a program. As your code statements get more complex, you’re probably seeing the importance of paying attention to your syntax. In this program, for example, you have a random function inside a digitalWrite function, which is inside a the void loop function. Syntax can be tricky to keep up with, but it’s what allows your code to have more ‘layers’ like this program does.

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 line of code delay(random(100,300)); means that the delay can last how long?