LED Circle Cycle

Your circle of LEDs can be used for many things: signals, status indicators, or just cool patterns! Learn how to run them in a circular cycle.

Code

The code below already works and is ready to upload to your Code Rocket! Then take on the challenges and see what changes you can make to create an even cooler project!

/* Cycle LEDs 3, 4, 5, and 6 in a circle continuously */ void setup() { // set the circle LEDs as output pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); } void loop() { for(int i=3; i<=6; i++){ //Starting with LED3 and up to LED6... digitalWrite(i,HIGH); //turn on the LED equal to the variable 'i' delay(50); //wait 50 milliseconds digitalWrite(i,LOW); //turn off the LED delay(50); //wait 50 milliseconds } /* After the 'for' loop runs, it will increase 'i' to the next number and use that number for the LED pin to light next. This repeats for 3,4,5,6 and then resets to 3 */ } // (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.

New Concept: New approach, same outcome

You’ve been turning on and off LEDs a bunch of different ways while programming the Code Rocket. In fact, you already had the skills to make a light pattern like this way back with project 3 or 4! In this project, you see a new tool being used- the ‘for’ loop - to accomplish a task that would take many lines of code if you just used digitalWrite() and delay() commands to accomplish the same task. And when it came time to add more LEDs, like the challenge prompts you to, you’d be adding lots and lots of lines of code to get there.

Programmers are always looking for ways to use technology to make their code easier to write, run, and understand. As you add more tools to your mental toolbox as a coder, you might want to revisit some of your older programs and see how you could improve them.

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 'for' loop condition that would make the lights reverse direction would be: