mirror of
https://github.com/alankrantas/microbit-micropython-cookbook.git
synced 2026-01-05 20:01:16 +01:00
Update README.md
This commit is contained in:
57
README.md
57
README.md
@@ -148,7 +148,7 @@ from microbit import pin1, pin2, sleep
|
|||||||
|
|
||||||
class Pin:
|
class Pin:
|
||||||
|
|
||||||
__slots__ = ['pin'] # not to use dictionary to store attributes in order to save memory
|
__slots__ = ['pin'] # not to use dictionary to store attributes in the class to save memory
|
||||||
|
|
||||||
def __init__(self, pin):
|
def __init__(self, pin):
|
||||||
self.pin = pin
|
self.pin = pin
|
||||||
@@ -237,31 +237,6 @@ while True:
|
|||||||
sleep(100)
|
sleep(100)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Servo Control
|
|
||||||
|
|
||||||
Note: currently (as for v2 beta) pin.set_analog_period throws ValueError and has been fixed in a future release.
|
|
||||||
|
|
||||||
```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)
|
|
||||||
```
|
|
||||||
|
|
||||||
Do not use servos and buzzers at the same time. They require different PWM frequencies and most microcontrollers can only use one frequency accross all pins at a time.
|
|
||||||
|
|
||||||
micro:bit V2 can output 190 mA from its 3V pin, which is enough for most hobby servos.
|
|
||||||
|
|
||||||
## Roll a Dice
|
## Roll a Dice
|
||||||
|
|
||||||
Define dice images in a dictionary, and retrieve one using a random number when the shake gesture detected.
|
Define dice images in a dictionary, and retrieve one using a random number when the shake gesture detected.
|
||||||
@@ -371,6 +346,36 @@ while True:
|
|||||||
print('Pitch:', rotationPitch(), ' / roll:', rotationRoll())
|
print('Pitch:', rotationPitch(), ' / roll:', rotationRoll())
|
||||||
sleep(100)
|
sleep(100)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Servo Control
|
||||||
|
|
||||||
|
```python
|
||||||
|
from microbit import pin0, sleep
|
||||||
|
|
||||||
|
class Servo: # define a servo class
|
||||||
|
def __init__(self, pin, degree=90):
|
||||||
|
self.pin = pin
|
||||||
|
self.degree = degree
|
||||||
|
self.write(degree)
|
||||||
|
|
||||||
|
def write(self, degree):
|
||||||
|
self.pin.set_analog_period(20)
|
||||||
|
self.pin.write_analog(round((degree * 92 / 180 + 30), 0))
|
||||||
|
|
||||||
|
|
||||||
|
servo = Servo(pin0) # servo object
|
||||||
|
|
||||||
|
while True:
|
||||||
|
servo.write(0)
|
||||||
|
sleep(1000)
|
||||||
|
servo.write(180)
|
||||||
|
sleep(1000)
|
||||||
|
```
|
||||||
|
|
||||||
|
Do not use servos and buzzers at the same time. They require different PWM frequencies and most microcontrollers can only use one frequency accross all pins at a time.
|
||||||
|
|
||||||
|
micro:bit V2 can output 190 mA from its 3V pin, which is enough for most hobby servos.
|
||||||
|
|
||||||
## NeoPixel Rainbow/Rotation Effect
|
## NeoPixel Rainbow/Rotation Effect
|
||||||
|
|
||||||
This code is based on Adafruit's example with adjustable brightness level.
|
This code is based on Adafruit's example with adjustable brightness level.
|
||||||
|
|||||||
Reference in New Issue
Block a user