Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Liest Werte des Beschleunigungssensors und schickt diese über die
- # serielle Schnittstelle
- # Copyright 2023, 2024 Roland Richter [Mu: BBC micro:bit]
- import microbit
- # Wandle einen Gyro-Wert (-1000 bis 1000) in eine Zeile/Spalte (0 bis 4) um
- def compute_position(value):
- if value <= -1000:
- value = -999
- if value >= 1000:
- value = 999
- scaled = (value + 1000) // 400
- return scaled
- # Zeichne eine senkrechte Linie in Spalte c (zwischen 0 und 4)
- def draw_vertical_line(c):
- for row in range(5):
- microbit.display.set_pixel(c, row, 9)
- # Zeichne eine waagrechte Linie in Zeile r (zwischen 0 und 4)
- def draw_horizontal_line(r):
- for col in range(5):
- microbit.display.set_pixel(col, r, 9)
- # Taste A gedrückt, geneigt nach links vorne: "Ab<^"
- # keine Taste gedrückt, geneigt nach rechts hinten: "ab>v"
- # Tasten A und B gedrückt, annähernde waagrecht: "AB|-"
- def create_sensor_string():
- result = ""
- # Status der Tasten A und B
- result += ('A' if microbit.button_a.is_pressed() else 'a')
- result += ('B' if microbit.button_b.is_pressed() else 'b')
- # x- und y-Wert des Beschleunigungssensors
- x_val = microbit.accelerometer.get_x()
- y_val = microbit.accelerometer.get_y()
- result += ('<' if x_val < -200 else ('>' if x_val > 200 else '|'))
- result += ('^' if y_val < -200 else ('v' if y_val > 200 else '-'))
- # PROBIERE, auch die Daten des Kompass in den Text einzufügen
- return result
- # Öffne die serielle Schnittstelle
- microbit.uart.init(115200)
- while True:
- x_val = microbit.accelerometer.get_x()
- y_val = microbit.accelerometer.get_y()
- col = compute_position(x_val)
- row = compute_position(y_val)
- microbit.display.clear()
- draw_vertical_line(col)
- draw_horizontal_line(row)
- # Sende Daten an den Plotter:
- # print((x_val, y_val))
- res = microbit.uart.write(create_sensor_string())
- if res is None:
- microbit.display.show(microbit.Image.SAD)
- microbit.sleep(1000)
- microbit.sleep(10)
- # ----------------------------------------------------------------------
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement