Programming with MicroPython by Nicholas H. Tollervey

Programming with MicroPython by Nicholas H. Tollervey

Author:Nicholas H. Tollervey
Language: eng
Format: epub, pdf
Publisher: O'Reilly Media
Published: 2017-10-11T04:00:00+00:00


from microbit import * x = 2 y = 2 sensitivity = 50 pause = 90 fade = 2 while True: roll = accelerometer.get_x() yaw = accelerometer.get_y() if roll < -sensitivity: x = max(0, x - 1) elif roll > sensitivity: x = min(4, x + 1) if yaw < -sensitivity: y = max(0, y - 1) elif yaw > sensitivity: y = min(4, y + 1) for i in range(5): for j in range(5): brightness = max(0, display.get_pixel(i, j) - fade) display.set_pixel(i, j, brightness) display.set_pixel(x, y, 9) sleep(pause)

The important lines are where we call accelerometer.get_x and accelerometer.get_y. There is also a notion of sensitivity such that movement won’t register in a direction until some threshold is reached (in the example, this is given the arbitrary value 50, arrived at through experimentation). This is because the accelerometer is very sensitive, and humans do not have such fine-grained motor skills to work with such sensitive devices. The threshold means we have to tip the device in one direction or another more than enough for us humans to be able to register the difference. When some aspect of the hardware is used to interact with humans, there often needs to be some sort dampening of sensitivity via thresholds or bucketing values so the device is both manageable and usable.

In the case of an accelerometer, such interactions are reminiscent of the Wii remote and other similar peripherals for console games used to control player characters and other game-related assets. The tilting and movement of an accelerometer is a useful metaphor for controlling an aspect of some other thing (such as the position of a pixel). An accelerometer is also useful if you need to log the forces applied to an object, such as a model rocket.

Another use of the accelerometer is to detect gestures, such as shake, freefall, or face up. Such gestures are useful from a user interaction point of view since they can also represent states or instructions. For example, shake may mean something negative like “cancel and restart the game”; freefall is probably an indication that the device is in the process of being dropped, so prepare for a crash landing; and face up may just mean display something (since you look down upon the display of the micro:bit) in a similar manner to the way mobile phones activate their screens when you take them away from your ear to look at them.

Currently, only the micro:bit has built-in support for gestures. The following example demonstrates how they can be used:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.