<aside> 📎 This is a solution to the challenge within Interacting with CYOBrain

</aside>

Challenge Solution

import machine
from lib.brain.display import *

First, think of a library as a treasure trove of knowledge and skills. The microcontroller is like a librarian who manages and organizes all the different sections of the library. They know exactly where to find each book, ensuring that information is easily accessible.

The lib.brain.display, or machine, is like a specialized corner of the library, dedicated to the fascinating world of visual storytelling. It's as if this corner is filled with toolkits and resources - a limited number of ready-made codes. These resources are carefully curated to help you look up and create the desired output faster.

So, when you import machine and lib.brain.display, it's like asking the librarian to open up the sections you need. You're gaining access to a vast array of tools and resources – from the meticulous organization of information to the enchanting display of visual knowledge – all available at your fingertips, just like the diverse offerings of a well-stocked library.

matrix = Matrix() # initialize LED matrix
left_button = machine.Pin(27, [machine.Pin.IN](<http://machine.pin.in/>)) # initialize left button
right_button = machine.Pin(0, [machine.Pin.IN](<http://machine.pin.in/>)) # initialize right button
counter = 0 # initialize counting number as variable

Now you are looking around the toolkit and the resources. You want to take out a a magical board with a grid of colorful lights that can display numbers. You told the librarian this by calling out what you want to get - we call this initializing. You initialize the magical board or the LED matrix by using the function matrix = Matrix().

This board also has two enchanted buttons, one on the left and one on the right. At the start, the left button is set up on the 27th magical spot, and the right button is set up on the 1st magical spot. There is also a helpful assistant who keeps track of a special number for you. The assistant's number is initially set to zero.

The assistant can use the lights on the board to show you the number it's keeping track of. Initially, it displays the number zero in a shade of blue.

if left_button.value() == 0: # when pressed, value of button is 0
    print("Left button is pressed") # print content to Console
    counter = max(0, counter - 1) # decrease the counting number, lowest value is 0
    matrix.reset()
    matrix.set_character("{}".format(counter), red=0, green=0, blue=100) # show number on LED matrix
elif right_button.value() == 0: # when pressed, value of button is 0
    print("Right button is pressed") # print content to Console
    counter = min(9, counter + 1) # increase the counting number, highest value is 9
    matrix.reset()
    matrix.set_character("{}".format(counter), red=0, green=0, blue=100) # show number on LED matrix

Now, the fun begins:

Throughout this magical interaction, the assistant is very responsive. It listens for button presses, reacts accordingly, and updates the number on the display as needed. So, whether you're moving to the left or the right, the assistant ensures the number stays within the limits (0 to 9).