Compass Code

Create a compass program to help Phiro detect directions using the Pocket Code app and the magnetic sensor in mobile phone.

Objective

By the end of this lesson, learners will

  • Understand how a compass works
  • Understand and use the magnetic sensor of mobile to make a compass direction detection program
  • Connect the physical and mobile digital worlds
  • Practise experimenting with code
Subjects

Robotics, Coding, Maths, Science.

Age

9 - 18 Years

Duration

45 min

Robot & Accessories Needed

Phiro Pro robot

Download Material

Click here to Download

Software

POCKETCODE4PHIRO

  • step1
  • step2
  • step3
  • step4
  • step5
  • step6
STEP 1

Ask

https://www.youtube.com/watch?v=r_rOcOeWHUg

What does a compass do?

How does a compass work?

What is a magnetic sensor in a mobile phone do?

How does the magnetic sensor work?

How would you create a compass program to help your phone detect direction?

How would you use the direction detection code to help Phiro detect direction?

STEP 2

Imagine

Brainstorm ways to help Phiro detect directions with a compass code.

Breakdown the challenge into parts:

  • Create compass and needle objects to detect direction
  • Program compass needle direction
  • Get user to enter direction to detect
  • Check conditions for each direction detection and give Phiro output for each
  • When code completed, place phone on Phiro and test!
STEP 3

Plan

Plan your Compass Code.

Explore which condition block and value for each direction to use for this program in Pocket Code. Also plan how to make Phiro move and stop according to Direction.

Create a sample program to find values of each direction

  • What block or command is used to find direction?
  • Imagine how to find value of each direction using Compass Direction block in Pocket Code.

Find value of each direction and note it down to do Direction Detection Program.

STEP 4

Create

Create your compass code to make Phiro to keep rotating with green light and stop and flashing red light when Phiro detects North direction.

1. Create compass and needle objects to detect direction. (Use images from download material section)

2. Program compass needle direction

  • When Program Started
  • Initializing Size to 100
  • Initializing look to Compass needle
  • Display object
  • Object always pointing NORTH direction using Forever and Compass Direction Block.

3. Get user to enter direction to detect. Use the "Ask" Block

  • When Program Started
  • Initialize Background
  • Initialising Background Costume.

4. Check conditions for each direction detection and give Phiro output for each

  • Check condition for North Direction
  • Set Compass value between - 5 to +5
  • Stop Phiro and LED glows Red.
  • Else, Phiro turns Left and LED glows Green.
  • Checks condition for East Direction
  • Set Compass value between -85 to 95
  • Stop Phiro and LED glows Red.
  • Else, Phiro turns Left and LED glows Green.
  • Check condition for South Direction
  • Set Compass value either > 175 or < -175
  • Stop Phiro and LED glows Red.
  • Else, Phiro turns Left and LED glows Green.
  • Check condition for West Direction
  • Set Compass value between 85 to 95
  • Stop Phiro and LED glows Red.
  • Else, Phiro turns Left and LED glows Green.

Testing out your Compass Code:

After you have completed creating your code

  • Switch ON Phiro and Turn ON Bluetooth mode
  • Place Smartphone aligned on top of Phiro
  • Press Play Button to start your code in Pocket Code
  • Enter the option number for direction.
  • Tap Submit answer and test!
STEP 5

Remix

Remix your Compass Code to make Phiro's lights glow different colors for different directions!

STEP 6

Share

Share your Phiro Compass Creation using #MyPhiro and @RobotixLS and we will share it with the world.