3.33 Compass

3.33.1 Overview

The AK8975 module is adopted to read direction values. According to these values, the OLED displays different arrows.

3.33.2 Test Code

3.33.2.1 Code Flow

6-33-2-1

3.33.2.2 Build Code

There are two ways to upload the code: directly open the code file we provide; or manually build blocks.

Directly open the code file we provide:

  1. Click and choose Load from your computer

  1. We have already downloaded the codes on computer desktop, so open the file and choose 3-33-compass.sb3

Manually build blocks:

  1. Build the two basic blocks:6-1-4-1-1

  2. Initialize AK8975 module and OLED display, declare a variable “direction”.

  1. Clear the OLED, and assign the direction value of AK8975 to the variable direction

  1. Add a j25 to determine whether 340 < direction < 360. If yes, OLED shows “↑”.

    Block condition: direction > 340 and direction < 360

  1. Duplicate blocks and determine whether direction > 230 and direction < 250. If yes, OLED shows “→”.

  2. If direction > 140 and direction < 170, OLED shows “↓”.

  3. If direction > 70 and direction < 90, OLED shows “←”.

  1. Add an OLED refresh block and add a delay of 0.5S.

Complete Test Code

6-33-2-2

3.33.2.3 Test Result

After uploading code, the arrow on the OLED will nearly point to the South. Move the coding box and you will see the direction changes.