Skip to content

Commit

Permalink
added microbit example code
Browse files Browse the repository at this point in the history
  • Loading branch information
cpmpercussion committed Aug 15, 2024
1 parent fe92c3a commit 7dae39b
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/microbit_serial_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from microbit import *
import utime
import math

def norm_acc(x):
new = round(min(max((x + 2000)/4000, 0.0), 1.0), 4)
return str(new)

last_acc_msg = ""

def send_accelerometer_data():
global last_acc_msg
if accelerometer.get_strength() > 1500:
x, y, z = accelerometer.get_values()
accs = map(norm_acc, [x,y,z])
out = ','.join(accs)
if out != last_acc_msg:
print(out)
last_acc_msg = out

def receive_and_display():
if uart.any():
data = uart.readline()
if data is None:
return
data = data.decode('utf-8').strip().split(',')
if len(data) == 3:
try:
x, y, z = map(float, data)
display.clear()
display.set_pixel(0, 4 - min(math.floor((x + 0.2) * 4), 4), 9)
display.set_pixel(2, 4 - min(math.floor((y + 0.2) * 4), 4), 9)
display.set_pixel(4, 4 - min(math.floor((z + 0.2) * 4), 4), 9)
except Exception as e:
pass

uart.init(baudrate=115200)

while True:
send_accelerometer_data()
receive_and_display()

0 comments on commit 7dae39b

Please sign in to comment.