// Copyright 2007, Sandeep Gangadharan // For more free scripts go to http://www.sivamdesign.com/scripts/

Random Light Party on Code Car

 
 

Let the void loop “decide” which of Code Car’s LEDs to flash on and off every time it runs. We’re using functions inside functions in this project.

Code

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

//Turn on and off random Car LED lights void setup(){ //Set each LED as an OUTPUT pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); } void loop(){ digitalWrite(random(4,8),HIGH); //Select LED 4,5,6, or 7 and turn it on delay(500); //Delay for 1/2 second digitalWrite(random(4,8),LOW); //Select LED 4,5,6 or 7 and turn it off delay(500); //Delay for 500 milliseconds } // (c) 2024 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

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!


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: Random

The computer ‘makes a choice’ in this program about which tones it plays. Using this function can make your code do some things that are unpredictable, even to you, the programmer!

This program shows you a new way that you can replace numbers in a program with other functions. In past projects, you’ve replaced number values with variables, but those variables were stable values. Now you see how you can use functions or equations in place of a single number (assuming you get your syntax right!).

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 are the two arguments that the random function requires?