

Open KidsBlock and connect the board to your computer. Click File –> Load from your computer.

Choose D:\Code\2.Code_kidsIOT to open 4.4Motion Alarm.sb3 file.

Click
to connect to port and then
.

Conceive:
Set a threshold angle range to determine whether objects have been moved.
Place the object and ensure its Course Angle is within the range, which means the object is not moved. Once the Course Angle value is greater than the threshold, it indicates that the item has been moved, so the amplifier alarms and the 6812 RGB lights up.

Conceive:
Initialization
Initialization: initialize AK8975 three-axis magnetic sensor; set the pin and the pixel number of the 6812 RGB module and set the brightness to 5; initialize serial port; define a float variable angle to store the read Course Angle.


Main Code
Assign the calculated Course Angle to the variable angle and print it on the serial monitor.
Determine whether the angle is within 0° ~ 45°(this range can be modified according to needs).
0° < angle < 45°: the amplifier stay quiet and LED keeps off.
angle is not within 0° ~ 45°: the object is moved, so the amplifier alarms and the LED lights up in yellow.

Build blocks:
① Add code blocks to the editing area and build as follows:

Read the Course Angle of the AK8975 three-axis magnetic sensor and assign it to variable angle.
② Add code blocks to the editing area and build as follows:


determines whether the left value is greater than the right one. If yes, output True.
determines whether the left value is smaller than the right one. If yes, output True.
determines whether the two conditions are both satisfied. If yes, output True.

if else block determines whether the object is moved.
Conditions:
sets the range to 0 ~ 45. When the object is moved, the angle exceeds this range.
So this block determines whether the variable angle is within 0 ~ 45.
If yes, execute codes in if to turn off all pixels.
If not, execute codes in else to light up all pixels in yellow, and the amplifier alarms.


After uploading code, place the device and maintain the Course Angle within 0° ~ 45°. In the initial state, the amplifier does not emit sound and the pixels are off. Move this device, and the angle will exceed the range of 0° ~ 45°, so the amplifier alarms and the pixels are on in yellow.



