mirror of
https://github.com/alankrantas/microbit-micropython-cookbook.git
synced 2026-01-05 11:50:50 +01:00
Update README.md
This commit is contained in:
36
README.md
36
README.md
@@ -1,4 +1,6 @@
|
|||||||
# micro:bit MicroPython Cookbook - Tricks and Experiments
|
# micro:bit MicroPython Cookbook (Updating)
|
||||||
|
|
||||||
|
A note for some Python tricks and experiments on BBC micro:bit.
|
||||||
|
|
||||||
## Easer Eggs
|
## Easer Eggs
|
||||||
|
|
||||||
@@ -25,6 +27,8 @@ Also, how micro:bit get its own version of MicroPython anyway: [The Story of Mic
|
|||||||
|
|
||||||
## Fill LED Display
|
## Fill LED Display
|
||||||
|
|
||||||
|
Light up every LEDs. Use fillScreen() as default.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from microbit import display, Image, sleep
|
from microbit import display, Image, sleep
|
||||||
|
|
||||||
@@ -46,7 +50,7 @@ while True:
|
|||||||
|
|
||||||
## A More Convenient Pin Class
|
## A More Convenient Pin Class
|
||||||
|
|
||||||
Using **namedtuple** to "rename" pin methods.
|
Use **namedtuple** to "rename" pin methods as set() and get().
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from microbit import pin0, sleep
|
from microbit import pin0, sleep
|
||||||
@@ -67,10 +71,8 @@ while True:
|
|||||||
|
|
||||||
## LED Bar Graph
|
## LED Bar Graph
|
||||||
|
|
||||||
Not perfect. A bit slow. Interference with microbit.display.read_light_level().
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from microbit import display
|
from microbit import display, sleep
|
||||||
|
|
||||||
def plotBarGraph(value, maxValue, brightness = 9):
|
def plotBarGraph(value, maxValue, brightness = 9):
|
||||||
bar = value / maxValue
|
bar = value / maxValue
|
||||||
@@ -88,10 +90,13 @@ def plotBarGraph(value, maxValue, brightness = 9):
|
|||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
for i in range(255):
|
lightLevel = display.read_light_level()
|
||||||
plotBarGraph(i, 255, 9)
|
plotBarGraph(lightLevel, 255, 9) # or plotBarGraph(lightLevel, 255)
|
||||||
|
sleep(50)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Since read_light_level() uses LEDs themselves as light sensors (see [this video](https://www.youtube.com/watch?v=TKhCr-dQMBY)), a short delay is added, but the LED screen would flicker a bit.
|
||||||
|
|
||||||
## Servo Control
|
## Servo Control
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@@ -110,6 +115,21 @@ while True:
|
|||||||
servoWrite(servoPin, 180)
|
servoWrite(servoPin, 180)
|
||||||
sleep(1000)
|
sleep(1000)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Do not use servos and buzzers at the same time. They require different PWM frequencies and would most microcontrollers can only set one frequency accross all pins at a time. Also micro
|
||||||
|
|
||||||
|
## Value Mapping
|
||||||
|
|
||||||
|
Translate a value in a range to its corresponding value in anoher range. Borrowed from [here](https://stackoverflow.com/questions/1969240/mapping-a-range-of-values-to-another).
|
||||||
|
|
||||||
|
```python
|
||||||
|
def translate(value, leftMin, leftMax, rightMin, rightMax):
|
||||||
|
leftSpan = leftMax - leftMin
|
||||||
|
rightSpan = rightMax - rightMin
|
||||||
|
valueScaled = float(value - leftMin) / float(leftSpan)
|
||||||
|
return rightMin + (valueScaled * rightSpan)
|
||||||
|
```
|
||||||
|
|
||||||
## Get Pitch and Roll Degrees
|
## 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.
|
These function cannot tell if the board is facing up or down. Probably need to use accelerometer.get_z() for that.
|
||||||
@@ -135,7 +155,7 @@ while True:
|
|||||||
print("Pitch:", rotationPitch(), " / roll:", rotationRoll())
|
print("Pitch:", rotationPitch(), " / roll:", rotationRoll())
|
||||||
sleep(100)
|
sleep(100)
|
||||||
```
|
```
|
||||||
## Rainbow NeoPixel
|
## NeoPixel Rainbow/Rotation
|
||||||
|
|
||||||
This code needs at least 3 LEDs in the NeoPixel chain.
|
This code needs at least 3 LEDs in the NeoPixel chain.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user