rric

kabel_zum_spielen_Aufgabe

Feb 6th, 2024 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. # Liest Werte des Beschleunigungssensors und schickt diese über die
  2. # serielle Schnittstelle
  3. # Copyright 2023, 2024 Roland Richter                [Mu: BBC micro:bit]
  4.  
  5. import microbit
  6.  
  7. # Wandle einen Gyro-Wert (-1000 bis 1000) in eine Zeile/Spalte (0 bis 4) um
  8. def compute_position(value):
  9.     if value <= -1000:
  10.         value = -999
  11.     if value >= 1000:
  12.         value = 999
  13.  
  14.     scaled = (value + 1000) // 400
  15.     return scaled
  16.  
  17.  
  18. # Zeichne eine senkrechte Linie in Spalte c (zwischen 0 und 4)
  19. def draw_vertical_line(c):
  20.     for row in range(5):
  21.         microbit.display.set_pixel(c, row, 9)
  22.  
  23.  
  24. # Zeichne eine waagrechte Linie in Zeile r (zwischen 0 und 4)
  25. def draw_horizontal_line(r):
  26.     for col in range(5):
  27.         microbit.display.set_pixel(col, r, 9)
  28.  
  29. # Gibt Sensor-Werte als String zurück, zum Beispiel
  30. # - Taste A gedrückt, geneigt nach links vorne: "Ab<^"
  31. # - keine Taste gedrückt, geneigt nach rechts hinten: "ab>v"
  32. # - Tasten A und B gedrückt, annähernde waagrecht: "AB|-"
  33. def create_sensor_string():
  34.     result = ""
  35.  
  36.     # Status der Tasten A und B
  37.     if microbit.button_a.is_pressed():
  38.         pass
  39.     else:
  40.         pass
  41.        
  42.     if microbit.button_b.is_pressed():
  43.         pass
  44.     else:
  45.         pass
  46.  
  47.     # x- und y-Wert des Beschleunigungssensors
  48.     x_val = microbit.accelerometer.get_x()
  49.    
  50.     if x_val < -200:
  51.         pass
  52.     elif x_val > 200:
  53.         pass
  54.     else:
  55.         pass
  56.  
  57.     return result
  58.  
  59. # Öffne die serielle Schnittstelle
  60. microbit.uart.init(115200)
  61.  
  62. while True:
  63.     x_val = microbit.accelerometer.get_x()
  64.     y_val = microbit.accelerometer.get_y()
  65.  
  66.     col = compute_position(x_val)
  67.     row = compute_position(y_val)
  68.  
  69.     microbit.display.clear()
  70.     draw_vertical_line(col)
  71.     draw_horizontal_line(row)
  72.  
  73.     # Sende Daten an den Plotter:
  74.     # print((x_val, y_val))
  75.  
  76.     res = microbit.uart.write(create_sensor_string())
  77.     if res is None:
  78.         microbit.display.show(microbit.Image.SAD)
  79.         microbit.sleep(1000)
  80.  
  81.     microbit.sleep(10)
  82.  
  83.  
  84. # ----------------------------------------------------------------------
  85. # This program is free software: you can redistribute it and/or modify
  86. # it under the terms of the GNU General Public License as published by
  87. # the Free Software Foundation, either version 3 of the License, or
  88. # (at your option) any later version.
  89. #
  90. # This program is distributed in the hope that it will be useful,
  91. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  92. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  93. # GNU General Public License for more details.
  94. #
  95. # You should have received a copy of the GNU General Public License
  96. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  97.  
Advertisement
Add Comment
Please, Sign In to add comment