Let’s explore binary code, a foundational part of computing! In this project, you will try to speak a few words in machine language, just as computers do.

First, we want to build a simple binary keypad, with 1 and 0 keys. Next, we will create a decoder that reads binary and prints a value.

Let’s get started! Here are the parts we’ll need:

Wire up your 2 large buttons to the Raspberry Pi as shown:

Make sure your jumper wires are connected to the correct pins using the pin map and table below:

Once your keypad is ready, it’s time to make some code!


From the Chip menu on the left, get a “when pin 1 turns on” block and drag it into the programming area. Create a new variable called “buttonPressed”. In the “when pin turns on” block, add a “set buttonPressed to true” statement.

 

Right click and duplicate the “when pin 1 turns on” block and change the copy’s pin number to 8 as shown below:

Next, take a new empty function block and name it “waitForButton”.

In “waitForButton”, add a statement to setbuttonPressed” to false. Next, obtain a wait until block. Finally, set “buttonPressed” as the condition to wait for.

Now, we’ll create our binary decoder. This function is a bit long; get ready! 

Grab an empty function from the Function menu, name it “scanBinary”.

Add two parameters to the function using the gear in the top left: base and digits.

Create a variable: total. At the beginning of “scanBinary”, add a “set total to 0” statement. Also, make “total” as the return for “scanBinary”.

Create another variable: value. Initialize it with: “set value to base ^ digits”.

Get a “repeat _ times” loop block, and put “digits” as the number of times to repeat.

In the loop, add a “set value to” block. Set the new value to “value ÷ base”, using blocks from the Logic and Variable menus.

 

Next, add a “shout” block, and have it output “value”.

 

Finally, add the function “waitForButton” as the last statement of the loop.

 

Nice job, we’ve finished the decoder! Check your work against the code below, and make sure everything is correct.

 

 

Finish defining the 1 button’s behavior by adding a “change total by value” statement to the “when pin 1 turns on” block.

 

To complete the program, we’ll need to write an entry point - the first line of code to run. Take a “Start” block, and attach it to a “shout” block, with your color of choice.

Then, attach a “scanBinary” function to the “shout” block. For parameters, set base to 2 and digits to 8.

Now it’s time to run our code and test it yourself!

Click “START” and press the buttons and see how the combinations of zeroes and ones form numbers. 

Once you’re ready to quiz yourself, pick a number between 0 and 255. Using the buttons, try to find the correct combinations of ones and zeroes that make up the number you chose.

After getting a good feel for how base-2 works, try changing the number of digits, or use a different base, like base-10! To do so, simply change the value of scanBinary’s parameters.

 

 

0 comments

Leave a comment