Speak Like a Machine

SPEAK MACHINE CODE WITH BINARY

45 MINUTES

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.

 

CONCEPT CHECK

  • In decimal, we use numbers 0 through 9. In binary, we use only 0 and 1.

For example, 1101 in binary represents 13 in decimal.

STEP ONE:

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

  • Small  Breadboard
  • 2x Buttons
  • 4x Jumper Wires

STEP TWO:

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

STEP THREE:

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

 

CONCEPT CHECK

  • To check if your buttons are wired up correctly, go to the Electronics tab in PiperCode. A pin should flash orange when either button is pressed.

STEP FOUR:

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.

 

STEP FIVE:

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

STEP SIX:

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

STEP SEVEN:

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

STEP EIGHT:

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

 

CONCEPT CHECK

  • In written language, we communicate meaning and ideas using sequences of symbols.
  • Similarly, computers use ON and OFF electrical signals to send instructions in machine language.

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

STEP NINE:

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

STEP TEN:

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

STEP ELEVEN:

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.

STEP ELEVEN:

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.

 

 

STEP TWELVE:

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.

STEP THIRTEEN:

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 going 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.

 

 

CONCEPT CHECK

  • By typing in binary digits (0’s and 1’s), a.k.a. Bits, we’re communicating using the smallest unit of information possible
  • Bits were only physically recorded on punch-cards: “hole” or “no hole”. Now, we record bits using logical gates, which are switches that can be opened or closed to current.

0 comments

Leave a comment

Please note, comments must be approved before they are published