Now that you know how to play sounds and draw on display, let's play with LEDs.
LEDs are light-emitting diodes, and your ByteBoi has four red, blue, and green LEDs located in the back.
Here you can see the input test for your LEDs:
Start fresh by creating a new sketch!
Firstly, we'll do a new variable and name it "color".
We'll drag the "set color to" variable into the "Arduino run first:" branch.
Now, let's take the blocks we use to change the variable when a certain event is triggered. We'll use the buttons for triggering the LEDs.
There are directional buttons on the left side of the console, while on the right side, there are A and B buttons and Menu (Select) buttons.
As you remember, those are the purple ones from the I/O section.
Drag the "when button-up gets pressed" and place it on the drawing board.
Now let's change the variable "color" value by 1 every time you press one of ByteBoi's buttons.
To do so, you'll need to pick a "change color by 1" block from the Variables section.
Drag a block and place it inside of the purple I/O block like shown in the photo below:
We won't change the numerical value needed for pressing the button "up" because we need to increase the value with every new button.
The next thing you can do is duplicate the I/O block and the "change color by _" block.
You can do that by simply clicking the right button and choosing Duplicate.
We will need six of these blocks because our game console has six buttons.
This is how your CircuitBlocks' sketch should look by now:
Since we duplicated all of the blocks, they all have the same button and the same numerical value written on them.
We have to change that for our code to work!
First, we need to set every purple block to a different button and change every button's numerical value. That way, you'll ensure that pressing every button
releases unique color from your LEDs.
Take a look at the photo below:
Now that we have assigned functions to these six buttons, we will use the Select button.
In CircuitBlocks, this button is called the Menu Button.
Once again, drag and drop the "When button up gets pressed" block from the I/O section to the drawing board.
Now, we'll define what the Menu button does. Let’s add some logic to it. From the “Logic” section drag, and drop the “if-else” block inside of the "When
button Menu gets pressed" block. Just like this:
Now, drag the "set color to" variable and the 123 blocks from the Math section and place them inside the Logic block.
Let's find out what the functions are!
The new thing we will learn right now is how to make a new function.
Functions are "self-contained" modules of code that accomplish a specific task. They are crucial if you want to keep your code clean and fast.
They are little pieces of code that you can tie together and call in your code multiple times..
Every program is made out of two main functions that we've already mentioned - void setup and void loop.
You can create a new function by clicking on the "Create a Function" in the Functions section.
This will open the “Edit Function” window. There, we’ll name our function “setLED”. To do that, delete the text that says “do_something” and type in “setLED” instead. Then click the “Done” button in the bottom right corner.
Once you do that, the function will automatically be added to the drawing board.
Just like this:
Now we’ll define what this function does. Let’s add some logic to it. Drag and drop the "if-else" block from the logic section. Place this block inside of the "setLED" function just like this:
In the “True” slot, drag and drop the comparison block from the “Logic” section.
On the right side of the block, place a variable block "color", and on the left side of the block, place zero (0).
Your new function will look like this:
Maybe you noticed that the logic block has minus (-) and plus (+) signs.
Right now, we'll use the plus (+) one. To add more "ifs" on your logic block, click on the little ‘+’ icon on the bottom left.
Click the ‘+’ icon six times.
The logic block should look like this:
Duplicate the comparison block you used at the beginning of the logic block, and place them in an empty circle between "if" and "else".
Numeric values should be set from 0 to 6, just like this:
Now, go to the Functions section and choose the block saying "call setLED" (the one which looks like a piece of a puzzle).
Duplicate the "call setLED" block and place it into the purple blocks like this:
In the end, we'll have to drag and drop purple "set RGB LED color" inside the "if-else if-else if-..." block. As you can see, we put a different color for every
button. Real light show it is!
Hit the red Run button!
ByteBoi will process your commands, and now you can press each button and get a different color from the LEDs.
Remember to save your hard work by pressing the save button on the top left.