From 9f8746650323e2d800d4b8b26ad7c0149ee5afd1 Mon Sep 17 00:00:00 2001 From: Alan Wang <44191076+alankrantas@users.noreply.github.com> Date: Wed, 19 Feb 2020 01:20:18 +0800 Subject: [PATCH] Create README.md --- README.md | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..2980ef8 --- /dev/null +++ b/README.md @@ -0,0 +1,171 @@ +# micro:bit MicroPython Cookbook - Tricks and Experiments + +## Easer Eggs + +Enter the following codes into REPL: + +```python +import this +import love +import antigravity +``` + +## Fill LED Display + +```python +from microbit import display, Image, sleep + +def fillScreen(b = 9): + f = (str(b) * 5 + ":") * 5 + display.show(Image(f[:len(f)-1])) + + +while True: + + for i in range(9): + fillScreen(i) + sleep(50) + + for i in reversed(range(9)): + fillScreen(i) + sleep(50) +``` + +## A More Convenient Pin Class + +Using **namedtuple** to "rename" pin methods. + +```python +from microbit import pin0, sleep +from ucollections import namedtuple + +Pin = namedtuple('Pin', ['set', 'get']) +setPin = lambda p: Pin(p.write_digital, p.read_digital) + + +led = setPin(pin0) + +while True: + print(led.set(1)) + sleep(500) + print(led.set(0)) + sleep(500) +``` + +## LED Bar Graph + +Not perfect. A bit slow. Interference with microbit.display.read_light_level(). + +```python +from microbit import display + +def plotBarGraph(value, maxValue, brightness = 9): + bar = value / maxValue + valueArray = ((0.96, 0.88, 0.84, 0.92, 1.00), + (0.76, 0.68, 0.64, 0.72, 0.80), + (0.56, 0.48, 0.44, 0.52, 0.60), + (0.36, 0.28, 0.24, 0.32, 0.40), + (0.16, 0.08, 0.04, 0.12, 0.20)) + for y in range(5): + for x in range(5): + if bar >= valueArray[y][x]: + display.set_pixel(x, y, brightness) + else: + display.set_pixel(x, y, 0) + + +while True: + for i in range(255): + plotBarGraph(i, 255, 9) +``` + +## Servo Control + +```python +from microbit import pin0, sleep + +def servoWrite(pin, degree): + pin.set_analog_period(20) + pin.write_analog(round((degree * 92 / 180 + 30), 0)) + + +servoPin = pin0 + +while True: + servoWrite(servoPin, 0) + sleep(1000) + servoWrite(servoPin, 180) + sleep(1000) +``` +## Get Pitch and Roll Degrees + +These function cannot tell if the board is facing up or down. Probably need to use accelerometer.get_z() for that. + +```python +from microbit import accelerometer, sleep +import math + +def rotationPitch(): + return math.atan2( + accelerometer.get_y(), + math.sqrt(accelerometer.get_x() ** 2 + accelerometer.get_z() ** 2) + ) * (180 / math.pi) + +def rotationRoll(): + return math.atan2( + accelerometer.get_x(), + math.sqrt(accelerometer.get_y() ** 2 + accelerometer.get_z() ** 2) + ) * (180 / math.pi) + + +while True: + print("Pitch:", rotationPitch(), " / roll:", rotationRoll()) + sleep(100) +``` +## Rainbow NeoPixel + +This code needs at least 3 LEDs in the NeoPixel chain. + +```python +from microbit import pin0, sleep +from neopixel import NeoPixel +from micropython import const + +led_num = const(12) +led_maxlevel = const(64) # max 255 +led_pin = pin0 + +np = NeoPixel(led_pin, led_num) + +def showRainbow(): + change_amount = int(led_maxlevel / (led_num / 3)) + index = [0, int(led_num / 3), int(led_num / 3 * 2)] + for i in range(led_num): + color = [0, 0, 0] + for j in range(3): + if abs(i - index[j]) <= int(led_num / 3): + color[j] = led_maxlevel - abs(i - index[j]) * change_amount + if color[j] < 0: + color[j] + if i >= int(led_num / 3 * 2): + color[0] = led_maxlevel - (led_num - i) * change_amount + if color[0] < 0: + color[0] = 0 + np[i] = (color[0], color[1], color[2]) + np.show() + +def rotate(): + tmp = np[led_num - 1] + for i in reversed(range(1, led_num)): # clockwise + np[i] = np[i - 1] + np[0] = tmp + np.show() + + +showRainbow() + +while True: + rotate() + sleep(50) + +```