The Spirit Level: Is It Straight?
Our programs can already make simple choices: IF this, THEN that, ELSE do something else. But life is usually more complicated than just two options! In this mission, you’ll learn how to handle multiple conditions using the powerful ‘ELSE IF’ command. Get ready to use your phone’s motion-sensing accelerometer to build a digital spirit level that changes color based on how much it’s tilted.
Conditionals; Else if; Multi-condition; Tilt sensor; Accelerometer; Main loop
Are you a teacher?
Courses
- Grades 3-12
Materials
- Cell phone, tablet, or computer
- Internet connection
Educational Objectives
- Understand the concepts of “else if” (elif) and the “Cartesian plane.”
- Create a technological object (prototype) using a device.
- Identify relationships between technology and the surrounding world.
- Evaluate personal and others’ work in individual or team tasks.
- Engage in dialogue and reflection on improvement ideas.
Start (10 minutes) - Decisions with Many Paths
- Welcome students and introduce the day’s activity: “Today, we will learn to prototype a digital tilt detector, just like a carpenter’s spirit level.”
- Start by briefly recapping the ‘IF/ELSE’ conditional for two options (like ON/OFF). Then, introduce a more complex problem: “An IF/ELSE block is great for two choices. But what if you have four choices? For example, you want a program to suggest a meal based on the time of day.”
- Use this analogy to show why we need more than just one ‘ELSE’. This is the perfect setup to introduce the ‘ELSE IF’ structure as a way to chain multiple checks together to make more complex decisions.
More Than Two Options?
You’ve seen how conditionals can work with two options: IF a condition is true, do something, ELSE do something different. This is perfect for a simple on/off switch. But what if you need to check for multiple, different conditions?
Introducing ‘Else If’
The “else if” structure lets you chain multiple checks together in a single, neat block. You can think of it like a referee in a soccer match:
- IF the foul is very serious, THEN show a red card.
- ELSE IF the foul is just a warning, THEN show a yellow card.
- ELSE (if neither of the above is true), THEN it’s just a simple foul. The program checks each condition in order until it finds one that is true.
ELSE IF: 4 Options for our Spirit Level
We’ll use this multi-path logic to program a tilt detector that helps us hang pictures perfectly straight. The screen will light up with different colors based on the tilt value: green for level, yellow for slightly off, orange for more tilted, and red for very tilted.
How does the phone detect tilt?
To detect the tilt of a phone, we use a tiny built-in sensor called an accelerometer. This sensor can measure forces like gravity, telling the phone its orientation in space. It measures tilt on three different axes: X (left-right), Y (forward-backward), and Z (up-down). This allows the phone to know if it’s tilted to one side, standing upright, or lying flat.
Development (20-30 minutes) - Building the Spirit Level
- Now that the students understand how to build a multi-path decision with ‘ELSE IF’, it’s time to apply that logic to a real sensor.
- Lead them through the instructions for building the spirit level and programming the multi-conditional logic, as detailed in the hands-on section below. Make sure they understand how each ‘ELSE IF’ block checks for a different range of tilt values, creating the different color warnings.
Closure (5-10 minutes) - Reflection on Sensors and Logic
- Once everyone’s spirit level is changing colors correctly, it’s time to reflect on the sensor and the powerful logic they’ve used.
- Use the final section to guide a discussion about the accelerometer’s other capabilities and to challenge them to create a new prototype combining tilt and sound.
Reflect
You’ve built a program that can make complex decisions!
This ‘IF / ELSE IF / ELSE’ chain is a super powerful tool.
- Our spirit level used the Y-axis (forward-backward tilt). What do you think would happen if you changed the code to read the X-axis (left-right tilt) instead?
- What other real-world tool or game could you build using your phone’s tilt sensor?
More Than Two Options?
You’ve seen how conditionals can work with two options: IF a condition is true, do something, ELSE do something different. This is perfect for a simple on/off switch. But what if you need to check for multiple, different conditions?
Introducing ‘Else If’
The “else if” structure lets you chain multiple checks together in a single, neat block. You can think of it like a referee in a soccer match:
- IF the foul is very serious, THEN show a red card.
- ELSE IF the foul is just a warning, THEN show a yellow card.
- ELSE (if neither of the above is true), THEN it’s just a simple foul. The program checks each condition in order until it finds one that is true.
ELSE IF: 4 Options for our Spirit Level
We’ll use this multi-path logic to program a tilt detector that helps us hang pictures perfectly straight. The screen will light up with different colors based on the tilt value: green for level, yellow for slightly off, orange for more tilted, and red for very tilted.
How does the phone detect tilt?
To detect the tilt of a phone, we use a tiny built-in sensor called an accelerometer. This sensor can measure forces like gravity, telling the phone its orientation in space. It measures tilt on three different axes: X (left-right), Y (forward-backward), and Z (up-down). This allows the phone to know if it’s tilted to one side, standing upright, or lying flat.
Make the Tilt Sensor
Let’s create a prototype that helps us detect if a picture frame is tilted.
First, we’ll add two components: one to detect the phone’s tilt and another to display the colored light.
1
Click on ✙ Add device and select Inclination.
2
Click on ✙ Add device again and select Lamp.
3
Add both devices to the same smartphone for this project.
We are ready to start prototyping!
Code Composition
Click on the question mark icon to open the comments that explain the code. The “absolute” block (which makes a number positive) is located in the Mathematics category, inside the “square root” block’s dropdown menu.
Reflect
You've built a program that can make complex decisions!
This ‘IF / ELSE IF / ELSE’ chain is a super powerful tool.
- Our spirit level used the Y-axis (forward-backward tilt). What do you think would happen if you changed the code to read the X-axis (left-right tilt) instead?
- What other real-world tool or game could you build using your phone’s tilt sensor?
Challenge: Create a music player where you can adjust the volume by tilting your smartphone up and down.
Hint: You’ll need the AudioPlayer component. Inside your main loop, you can set the volume based on the value you get from the tilt sensor.