LED Circle Button Cycle

Combine your knowledge of variables, statements, and loops to create a light pattern on the LED circle of 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. 

/* Use button 12 to turn on more of your circle LEDs and button 11 to turn them off one at a time. */ int pressCount = 0; //Variable to keep track of which LEDs are on and off void setup(){ pinMode(11,INPUT_PULLUP); //buttons pinMode(12,INPUT_PULLUP); pinMode(3,OUTPUT); //LEDs pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); } void loop(){ if(digitalRead(12)==LOW){ //if button 12 pressed, increase PressCount variable by 1 pressCount++; delay(250); //delay 250 milliseconds so a single press doesn't count multiple times } if(digitalRead(11)==LOW){ //if button 11 pressed, decrease PressCount variable by 1 pressCount--; delay(250); } if(pressCount>4){ //Don't let pressCount go above 4. pressCount=4; } if(pressCount<0){ //Don't let the pressCount variable go into negative #s pressCount=0; } if(pressCount==1){ //When pressCount is 1, turn on LED3 and off everything else digitalWrite(3,HIGH); digitalWrite(4,LOW); digitalWrite(5,LOW); digitalWrite(6,LOW); } else if(pressCount ==2){ //When pressCount is 2, turn on LED3 and off 5 and 6 digitalWrite(4,HIGH); digitalWrite(5,LOW); digitalWrite(6,LOW); } else if(pressCount ==3){ //When pressCount is 3, turn on LED5 and off 6 digitalWrite(5,HIGH); digitalWrite(6,LOW); } else if(pressCount ==4){ //When pressCount is 4, turn everything on digitalWrite(6,HIGH); } else{ digitalWrite(3,LOW); //if pressCount isn't 1,2,3,or 4, turn everything off. digitalWrite(4,LOW); digitalWrite(5,LOW); digitalWrite(6,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

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: Conditions, conditions

When your program’s action is based on a condition as simple as ‘pressCount’, you can see how you can quickly build a complex program with very few inputs. This project is similar to the ‘Space Headlights’ project, but it takes the idea further about how many pressCounts you can have. When your button input increases the value of a variable, there’s almost no limit to how many different outcomes you could have. You could even have a special number of presses that triggers a ‘secret’ reaction.

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 will happen if you limited pressCount to 5 instead of 4?