PiperCode: Speak Machine Code with Binary!

PiperCode: Speak Machine Code with Binary!

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.

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

PiperCode: Speak Machine Code with Binary!

PiperCode: Speak Machine Code with Binary!

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.

 PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary! 

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

 

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary! 

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

 

PiperCode: Speak Machine Code with Binary! 

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

PiperCode: Speak Machine Code with Binary! 

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.

PiperCode: Speak Machine Code with Binary!

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

PiperCode: Speak Machine Code with Binary!

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.

Once you've gotten a good feel for how binary works, let's try go from machine to human language by switching from binary to decimal! To do so, simply change the value of scanBinary’s base parameter. You can also adjust the length of the input by changing digits.

 

PiperCode: Speak Machine Code with Binary!

PiperCode: Speak Machine Code with Binary!