PiperCode: Temperature
Piper CODE: Goldilocks machine project
To do this project, you will need a Piper Computer Kit & Sensor Explorer.
Skills & Time
Time: 45 minutes Skills: Electronics, Coding Skill Level: Moderate
At the table in the kitchen, there were three bowls of porridge. Goldilocks was hungry. She tasted the porridge from the first bowl.
"This porridge is too hot!" she exclaimed. So, she tasted the porridge from the second bowl.
"This porridge is too cold," she said. So, she tasted the last bowl of porridge.
"Ahhh, this porridge is just right," she said happily and she ate it all up.
--
Objective:
Build a device to measure temperature.
Project Overview:
In this project, we’ll build a “Goldilocks Device”. This device can measure the temperature, and tell us if somewhere is “just right”! Our Goldilocks Device will require two components: a temperature probe, and an LED display gauge. We’ll build these two components separately, so try not to mix up the parts!
Materials Needed:
For the temperature probe, you will need:
- 1 Temperature Sensor
- 1 Mini Breadboard
- 1 4-pin diode jumper cable
- 4 Jumper wires
For the LED gauge, you will need:
- 1 Mini Breadboard
- 3 LED's
- 4 Jumper wires
Build A Temperature Probe
Let’s start building! Wire up your temperature sensor to the Pi, using the following diagrams as guidance.
Build a LED Display Gauge
Now, let’s build the component which will tell us whether the temperature is “too cold”; “too hot; or “just right”! Once again, use the following diagrams to help you assemble the LED gauge.
Once you are done, the Goldilocks Device should look like this:
Code the Goldilocks Finder
Let's start with a Main Loop. Go to the main menu of PiperCode and open Settings. Unlock all projects in order to start a new one. To start, we’re going to create a simple loop.
Find a Start block from Logic, and bring it into the programming area. Then, attach a repeat forever block, found in Loops, to the Start block.
Record Temperature as a Variable
Now, we are going to capture the output of the temperature sensor in a variable. Create and name a new variable - temperature - using the Variables pane. Take a set temperature to block from Variables and add it to your loop.
Then, find a Temperature (F) block from Expansions, and use it to set the value for temperature.
Set up Conditional Logic
Under Logic, find an If do else block with the “=” inequality as shown. Select the settings gear for this block and choose an else if and an else block. Drag these under the if block as shown.
The final statement should be “if do, else, else if, else, do”. This sets up a conditional that means three different things can happen depending on the input values from the sensor.
Add a Comment
Right click on the question mark part of this block to add a comment so that you know why you decided to create this conditional block.
Tidy your Coding Space
Make sure your coding space stays neat and tidy by dragging your comment to the side of your code. Use the example below to guide you.
Prepare a "Hot!" Mode
Next, you will want to create a range in your code that signals something being measured by the temperature sensor is “too hot.'' Label the “=” block as shown.
Define "Hot!" Conditions
Add a temperature block found under Variables and add it to the equality as shown. Change the equality to a greater than or “>” symbol to ensure that temperatures above the set temperature result in the desired outcome. Add an integer variable (“0”) found under the variables tab. Change the integer to a value that you believe is “too hot” in Fahrenheit. The example below shows 82 degrees.
Turn on the "Hot!" LED
Your code should tell the red LED to turn on when something is “too hot” or greater than (>) 82 degrees Fahrenheit. Drag a turn on pin block into the do part of your conditional as shown. Set the pin number to 40.
(Note: you may want to check your wiring to make sure the red LED is wired up to pin 40 on the Raspberry Pi. You can use the diagrams at the beginning of this tutorial to help you!)
Create a "Cold!" Mode
Next, you will label the else if section of your conditional to make the “cold” mode of your Goldilocks device. Duplicate the inequality statement and label it as shown. Drag the inequality statement into the else if part of the conditional.
Define "Cold!" Mode
Set the inequality to less than or “<” to ensure that temperatures colder than the value will result in the blue LED lighting up. Choose a temperature that you deem as “too cold” For example, we have chosen 72 degrees (which is room temperature). Duplicate the Turn Pin on block and add it to the next do section of the conditional. Set the pin number to 37.
Finish with a "Just Right" Mode
Finally, time for the “just right” temperature which will light up a yellow LED. Duplicate the Turn Pin on block and add it to the final else part of the conditional. Set the pin number to 38. This is a good time to check your wiring to ensure the pin numbers in your code match the pins that are wired to the LEDs.
If the temperature read by the sensor is outside of the cold or hot range, then the yellow LED will light to signal the temperature is “just right!”
Turn Off the Lights
You will need to ensure that you set all pins to off during each loop, so only one LED remains on at any moment. To do this, grab a “Turn all pins” block under chip and drag it to the top of the loop (below repeat forever). Set it to OFF.
Want more fun?
Find objects or places around you that are at different temperatures and measure them with your temperature sensor- see which ones are “just right”!
ATTENTION: Do not place your Piper Temperature Sensor in liquids or on hot metal surfaces.
Not official Minecraft product. Not approved by or associated with Mojang. The Raspberry Pi Edition of Minecraft by the Raspberry Pi Foundation is licensed under the Creative Commons Attribution 4.0 International License.