Story
With the 25 LEDs on the Calliope you can build a great combination lock.
The program should enable the code entry with the 2 buttons. Button A switches through the columns (= digit in the code), button B is used to set the value of the current code digit (between 0 and 4).
At the beginning there is the code
First we define the correct combination of numbers as "character string", draw the first pixel in the upper left corner and set the LED color to red ("lock is closed"). We set the speed to a high value so that the beeps are nicely short:
Switching through the columns
Now we make sure that when pressing key A the column to be currently set is switched through. In addition, we change the value of the currently edited column to 0, so that we always start at the top when entering the values:
If the placeholder "edit_column" is larger than 4, we have to reset it to 0, because we only have 5 columns (0,1,2,3,4):
Finally we will switch off all LEDs in the column we just changed to. Then we let the current column value in this column flash 3 times briefly:
Setting the value of the column
The value should be increased by 1 each time the button is pressed. Again we start at 0 if the value is greater than 4. Before the increase we switch off the LED of the previous value, after the increase we switch on the LED with the new adjusted value:
Now just check if the code is correct and then turn the LED green!
Whenever a programmer says this sentence, he has about 20% of the task done - now its getting complicated!
To check whether the code was entered correctly, we'll go through the columns and check whether the set value corresponds to the value in the solution string (which we set when we started the program) at the respective position.
So we set the counter "code_correct" to 0, and check all columns in a loop. For this we first set a placeholder in which we extract the number from the solution string for the current column. We use the "character at position" block to get the character at the column position. Since this is a character, and we want to compare it with a number, this character must be converted into a number. This is done by the block "parse to integer":
Now we'll go through all rows in the current column. If the LED in a row is on (point x y - block is "true"), we look if the row matches the "solution of the column" determined in the previous step, and count the code_correct placeholder up by 1.
Now we only have to check (this time really!) whether all columns have been entered correctly. This is the case if the code_correct placeholder has the value 5! We signal the correct task by setting the color LED to green. In the otherwise branch of the comparison we set red. This happens as soon as the code input is changed by the buttons and no longer corresponds to the solution
Instead of setting the LED color, you could now use the pins to control something that opens and closes something "real", e.g. with a servo.
Schematics
This text as well is published under a CC BY-SA 3.0 DE license. It was originally published in German by Daniel Havlik and translated into English by the Calliope team.