Make, Code, Have Fun

This site was created to collect all of our project done in our coding class.

Follow me on GitHub

A Simple Personal Assistant, Part 2


Our Goal Today

  • A view on light sensor data

  • Improve button pressed detection

  • Gesture


1. Light Sensor Data

We’ve all experienced in our first project, that the night safety light was flashing LED indoor when we think it was pretty bright. Our microbit decide when to flash LED by comparing to a fix number. This number is called the threshold. How to set a reasonable threshold? That requires us to have some concept about how sensor measures the environment feature.

Here is are two plots. The first one, I took a Microbit and record the lighting level in my office, and in my backyard from 7am to 8am. Have a look at these data plots:

light_level_plots

As a designer, we often need to collect, and analyze data from our design to build understanding, and improve our design. The night light design is a great example.

From these data plot, we see that indoor light are not very strong. My office is very bright, but only shows light_level of 30 in our sensor reading.

For outdoor, at light level of 30, it still feels quite dark, and the Microbit LEDs clearly stands out. Actually it was when I terminate the recording, that I feel the LEDs don’t stand out.

backyard microbit

Based on these observation, as a designer, I would keep my light threshold at between 50 to 100. And find an additional way to turn ON or OFF the night light feature.

2. Did our button works?

2. a) Button not responsive when LEDs are flashing

How program detect button press

  • Demo in slow motion

2/b) Solution: Use Event Handler

Say we have a continuous task of

  1. S1: Flashing LED
  2. S2: Check if button A has been pressed
  3. S3: Read temperature if button A is pressed

Our program originally follow the upper scenario. Task 1 takes most of the time in each iteration of a loop. The solution is to make our program do the bottom scenario: breaking up the task 1, and check for task 2.

concurrency task scheduler

How do we achieve this in our program? The technique is called event handling. To make this happen in MakeCode, we need two components: basic.pause() and event handler.

  • At each pause() we inserted into our program, we pass the control to Microbit scheduler so it can check all other events
  • The scheduler check all the events that need to be handle, and call the event handler function we defined.

See demo program below:

2. c) Update your code with event handler

Block programmer, please join Microbit classroom: https://microbit.org/join

microbit classroom

Python programmer, use the usually MakeCode editor, and share screen when necessary.


Task

  • Make your temperature reporting feature more responsive by using event handling

3. Add a simple Step Counter

We would like to continue enriching our personal assistant design. Let’s try to add a step counter feature. The step counter uses gesture to detect motion. Here is a quick demo for gesture:

A step counter counts your steps. It’s a good tool to count how much you have exercised. To do that, we will use the gesture shake

  1. Make a variable called step
  2. Add event handler: On shake, increase step by 1
  3. In our while True: loop, add step number report.

Home Work:

  • Complete step counter code, or create something of your own with gesture.
  • Try out your step counter. Does it work?
  • If you create your own idea, make a video to demonstrate.
  • Send your work (.hex file or the shared project url), and video) to fzhang00@gmail.com