agsdix-fas fa-child

Tilt Sensor

Learn how to create a tilt sensor to prevent hanging crooked pictures.

info outline icon Basic Activity; Multiple Conditionals; Tilt Sensor; Main Loop.

What are we going to do?

Are you a teacher?

Lesson Plan

Courses

  • Grades 3-12
  • 3rd Grade – 8th Grade Courses

Materials

  • Cell phone, tablet, or computer
  • Internet connection

Description

In this activity, students can experiment in an introductory way with Protobject, learning visual programming and computer science safely and playfully using a device.

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 ideas for improvement

Introduction (10 minutes)

Welcome the students to the class and briefly introduce the day’s activity: “Today, we will learn to prototype a ‘fall detector’.”

We will start by providing technical knowledge to the students for meaningful learning of the practical activity.

To begin, we can ask a question:

Do you remember what a conditional statement in programming is?

You can give the students a chance to offer their answers and then provide the correct response.

It’s a way to tell the computer to do different things based on whether a statement is true or false.

For example, you can tell the computer to turn off the light if it’s daytime, otherwise, turn it on.

If you have doubts, you can review conditional statements “if/else” in Activity 9.

What would happen if we wanted to add more than two options?

Allow time for exploration and student responses. Then, supplement with the following:

The “else if” (elif) structure is a way to add more options to a decision being made in the “if/else” structure. You can think of it as choosing between different paths.

For example, imagine you have to feed your dog and that depending on the time of day, it will either receive breakfast, lunch, or dinner.

  • IF it’s before 9:00 AM, THEN you have to give it breakfast.
  • ELSE IF it’s between 9:00 AM and 6:00 PM, THEN you have to give it lunch.
  • ELSE at another time, THEN you have to give it dinner.

“Else if” is used to choose between different options based on different conditions. If one condition is true, the computer will do one thing; if another condition is true, the computer will do something different. And so on, following the order in which you programmed it (sequence).

Finally, to initiate the prototyping process and make them feel like protagonists of their learning, ask them:

Do you want to learn how to prototype a fall detector?

Before you begin prototyping, you can review supplementary content:

The Cartesian plane is a way to represent different positions in a two-dimensional space using two perpendicular lines that intersect at a point. These lines are called axes and are labeled with the letters X and Y.

You may have seen it used to represent two different variables on a graph, and today, we will use it to represent up-and-down movement, which is along the Y-axis.

Development (20-30 minutes)

Connect our devices to the Protobject platform. For this exercise, we will use the motion sensor SMARTPHONEMOTION and the lamp LAMP.

Remember to experiment on your own beforehand so that you can anticipate questions that may arise from the students.

First, we will program our monitor to respond by turning on the green light if the movement on the Y-axis is less than 6.

We will repeat the same steps to turn on the yellow light for movement less than 20 and the orange light for movement less than 40. Finally, we will use an “else if” to turn on the red light if none of the other conditions are met.

Finally, we will place everything inside a loop so that the condition repeats indefinitely.

To test the prototype, instruct the students to click on the activation button. 

Congratulations!

Closing (5-10 minutes)

Now that you have prototyped a fall detector with Protobject, challenge the students with a question:

  • What would happen if we changed the axis from Y to X?

To solve the problem posed at the end of the activity, all you need to do is use the orientation value in the component’s intensity block. However, be sure to use the absolute value; otherwise, you might set negative values, which wouldn’t make sense.

Check the challenge solution.

agsdix-fas fa-book-reader

Learn

Do you remember what conditionals are in programming?

It’s a way to tell the computer to do different things depending on whether something is true or false.

For example, you can tell the computer to turn off the light if it’s daytime, and if not, to turn it on.

If you have doubts, you can check the “if/else” conditionals in activity 9.

But… What if we want to add more than two options?

The “sinosi” (elif) structure is a way to add more options to a decision being made within the “if/else” structure. You can think of this as choosing between different paths.

For example, imagine you have to program the logic for when a referee calls a foul:

  • IF you receive a red card, THEN you must leave the field.
  • ELSE IF you receive a yellow card, THEN you should be careful, because if you receive another yellow card, you have to leave the field.
  • ELSE if no card is shown, THEN it’s a simple foul, and everything is fine.

ELSE IF: 4 tilt options

Next, we’ll put this knowledge into practice by programming a tilt detector that will light up your screen with different colors based on the tilt value as follows:

If there’s no tilt, the screen will light up in green. If there’s a little tilt, it’ll be yellow, with even more tilt, it’ll be orange, and with a lot of tilt, it’ll be red.

We’ll achieve this using the “else if,” and it will help us hang pictures straight!

But… How does the smartphone detect the tilt?

To detect the tilt of a phone, we use a small sensor called an accelerometer.

This sensor can measure the force of gravity pushing the phone in different directions.

The accelerometer has three axes of measurement: the X-axis, the Y-axis, and the Z-axis, as if they were three imaginary lines in different directions.

This way, the phone can understand if it’s tilted to one side or the other, or if it’s standing upright.

Let’s get to work!

To create this prototype, we will use the “Absolute Value” code block and adjust the sensitivity of the tilt so that the sensor can recognize when to activate the green, yellow, and orange lights. We will also program the system so that if none of these conditions are met, the red light will turn on automatically.

Make

Let’s create a prototype that allows us to detect the tilt of a picture frame.

The first thing to do is to add two devices that will enable us (1) to detect the tilt and (2) to turn on colored lights.

1

Click on Add device and select Inclination

2

Click on DevicesAdd device and select Lamp

Add both devices using the same smartphone.

plus square icon

Important: In this activity, you need to add both devices on the same smartphone by pressing the SCAN button after adding the first one.

We are ready to start prototyping!

Code composition

Click on the question circle icon icon to open the comments explaining the code.

The “absolute” block is located in the Mathematics category, specifically in the “square root” block (selected from the dropdown menu).

agsdi-bulb-options

Reflect

Now that you’ve created a tilt sensor with Protobject

What other project could you use a tilt sensor for?

plus square icon

Challenge: Create a music player where you can adjust the volume by tilting your smartphone.

Hint: Use the AudioPlayer device to play a song, and in the main loop, adjust the volume based on the tilt of the smartphone.

In the next activity, we will create a fall detector.