Lesson 13: Accelerometer

Overview

In this lesson, students will explore the accelerometer and its capabilities. They’ll become familiar with its events and properties, as well as create multiple programs utilizing the accelerometer similar to those they’ve likely come across in real world applications.

Purpose

This lesson gives students an opportunity to work with the accelerometer sensor and explore its orientation properties and accelerometer-specific board events. Students will see the purpose and uses of an accelerometer in real world devices and programs and create their own versions of some of these applications. To do this, students will need to refer back to their past knowledge of the counter pattern to create functional accelerometer-based apps.

Agenda

Warm Up (5 Min)

Activity

Wrap Up (5 Min)

View on Code Studio

Objectives

Students will be able to:

  • Recognize the use and need for accelerometer orientation (pitch and roll).
  • Identify and explain the difference between the shake, data and change events.
  • Refer back to and use their past knowledge of the counter pattern.

Introduced Code

Teaching Guide

Warm Up (5 Min)

What Makes a Sensor?

Discussion Goal

Students have been exposed to sensors in previous lessons but have only seen a few aspects of what a sensor can measure. This discussion gives students a chance to think about the characteristics of a sensor, while also thinking about the other possible characteristics of a sensor they haven’t been exposed to.

Prompt: Refer back to the analog sensors, what makes a sensor a sensor? Could an accelerometer be a sensor?

Share: Have students share their thoughts and ideas in small groups.

Activity

The Accelerometer

Transition: Send students to Code Studio.

Wrap Up (5 Min)

Journal

Goal: Students should be able to recognize the use for an accelerometer.

Prompt: Now that you’ve created your own programs with the accelerometer, can you think of some ways you interact with accelerometers every day through technology?

View on Code Studio

Today you’re going to learn how to use and create functions.

Creating a function lets you make your own blocks! There are two parts to a function:

Making a newly named block that has all of the code you want to run inside of it. -xml of function definition-

The single green block itself that you use, or call, to run the function you created. -xml of function call-

Prepare to get familiar with functions, learn how to edit them, and create your own!

View on Code Studio

Student Instructions

Accelerometer

Look at the code below. What do you think it's going to do? Give it a guess, and run the code to find out!

View on Code Studio

Student Instructions

Pitch and Roll

This program displays the board's pitch and roll, and it will also change colors depending on their values.

Do This

Make the program change the following colors by tilting the board to the correct pitch and roll.

 

Pitch: 0 | Roll: -20

 

Pitch: 30 | Roll: 0

 

Pitch: -15 | Roll: 10

 

Pitch: 5 | Roll: -25

 

Pitch: 10 | Roll: 30

Which way should you tilt to get a positive pitch?

Which way should you tilt to get a negative roll?

View on Code Studio

Student Instructions

Pitch

This program lights the top LEDs (0 and 9) when you tilt the board forward. If you tilt the board back, the LEDs at the bottom (4 and 5) should light.

Do This

  • Use the board's pitch to make LEDs 4 and 5 light up when you tilt the board back.
  • Run your code and test it a few times.
View on Code Studio

Student Instructions

Roll

This program should display "Left" or "Right" on the screen, depending on which way the board is tilted.

Do this

  • Use the "roll" property of the accelerometer to determine whether the board is tilted to the left or right.
  • Use a conditional to change the text for the "direction" element to be either "Left" or "Right", depending on the board's orientation.
View on Code Studio

Today you’re going to learn how to use and create functions.

Creating a function lets you make your own blocks! There are two parts to a function:

Making a newly named block that has all of the code you want to run inside of it. -xml of function definition-

The single green block itself that you use, or call, to run the function you created. -xml of function call-

Prepare to get familiar with functions, learn how to edit them, and create your own!

  • Accelerometer Events
  • 8
  • 9
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Change with the Accelerometer

This program uses the change event to increase a movement meter every time the board moves. When the movement meter reaches 100, the alarm will sound.

Do This

This program has two bugs that you need to fix:

  • The movement meter is being triggered by a button press, instead of the accelerometer's change event.
  • The programmer forgot how to use the counter pattern to increase the movement variable.

Once you have found the bugs, run the program and try to pass the board to your partner without setting off the alerm.

View on Code Studio

Student Instructions

Jogger

This program should use the shake event, which is triggered when the board is shaken. It should add a step to the jogger's total every time the user shakes the board.

Do This

  • Change the event so it triggers every time the board is shaken.
  • Add code inside the event so that steps increases every time the board is shaken.
View on Code Studio

Today you’re going to learn how to use and create functions.

Creating a function lets you make your own blocks! There are two parts to a function:

Making a newly named block that has all of the code you want to run inside of it. -xml of function definition-

The single green block itself that you use, or call, to run the function you created. -xml of function call-

Prepare to get familiar with functions, learn how to edit them, and create your own!

  • Accelerometer Events
  • 11
  • 12
  • 13
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Updating Values with Data

In this program the goalie is trying to block the soccer ball, but right now the ball isn't moving.

Do This

  • Use the counter pattern to update the ball's x value so that it moves across the screen.
View on Code Studio

Student Instructions

Steering Wheel

This game is similar to the goalie game that you just played, except a little different. Use your board as a steering wheel for the car (moving side to side). You have to get to the checkered flag at while avoiding the red flags along the way!

Do This

  • Make the car move down the screen while the program runs.
  • Give the car the ability to steer side to side.

Think back to the programs you've made up until this point, how can you update these values?

View on Code Studio

Student Instructions

Limiting Turn

You might have noticed that it was really hard to steer the car unless your hands were moving slowly and carefully. This is because the number that roll returns can be a really big number depending on how much the accelerometer is being turned. Remember, roll has a range from -90 to 90.

Do This

Find a way to make roll a smaller number while still using it to steer the car. You have all of the math operators available to you for this.

Challenge: Can you keep the car from going off the side of the screen?