rric

gyro_sensor

Feb 5th, 2024 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. # Liest Werte des Beschleunigungssensors, und zeigt ein '+' am Display an
  2. # Copyright 2023, 2024 Roland Richter                [Mu: BBC micro:bit]
  3.  
  4. import microbit
  5.  
  6. # Wandle einen Gyro-Wert (-1000 bis 1000) in eine Zeile/Spalte (0 bis 4) um
  7. def compute_position(value):
  8.     # PROBIERE, diese Funktion mit Hilfe der ganzzahligen Division // zu vereinfachen
  9.     if value < -600:
  10.         return 0
  11.     elif value < -200:
  12.         return 1
  13.     elif value < 200:
  14.         return 2
  15.     elif value < 600:
  16.         return 3
  17.     else:
  18.         return 4
  19.  
  20.  
  21. # Zeichne eine senkrechte Linie in Spalte c (zwischen 0 und 4)
  22. def draw_vertical_line(c):
  23.     # PROBIERE, diese Funktion mit Hilfe einer for-Schleife zu vereinfachen
  24.     microbit.display.set_pixel(c, 0, 9)
  25.     microbit.display.set_pixel(c, 1, 9)
  26.     microbit.display.set_pixel(c, 2, 9)
  27.     microbit.display.set_pixel(c, 3, 9)
  28.     microbit.display.set_pixel(c, 4, 9)
  29.  
  30.  
  31. # Zeichne eine waagrechte Linie in Zeile r (zwischen 0 und 4)
  32. def draw_horizontal_line(r):
  33.     # PROBIERE, diese Funktion mit Hilfe einer for-Schleife zu vereinfachen
  34.     microbit.display.set_pixel(0, r, 9)
  35.     microbit.display.set_pixel(1, r, 9)
  36.     microbit.display.set_pixel(2, r, 9)
  37.     microbit.display.set_pixel(3, r, 9)
  38.     microbit.display.set_pixel(4, r, 9)
  39.  
  40.  
  41. while True:
  42.     microbit.display.clear()
  43.  
  44.     x_val = microbit.accelerometer.get_x()
  45.     col = compute_position(x_val)
  46.  
  47.     y_val = microbit.accelerometer.get_y()
  48.     row = compute_position(y_val)
  49.  
  50.     draw_vertical_line(col)
  51.     draw_horizontal_line(row)
  52.  
  53.     # PROBIERE, die x- und y-Werte als Tuple auszugeben und im Plotter anzusehen,
  54.     # siehe https://codewith.mu/en/tutorials/1.2/plotter
  55.  
  56.     microbit.sleep(100)
  57.  
  58.     # PROBIERE den Code so zu ändern, dass aus deinem micro:bit
  59.     #     eine Wasserwaage wird, siehe auch
  60.     #     https://microbit-challenges-de.readthedocs.io/de/latest/challenges/sensors/spirit_level.html
  61.  
  62.  
  63. # ----------------------------------------------------------------------
  64. # This program is free software: you can redistribute it and/or modify
  65. # it under the terms of the GNU General Public License as published by
  66. # the Free Software Foundation, either version 3 of the License, or
  67. # (at your option) any later version.
  68. #
  69. # This program is distributed in the hope that it will be useful,
  70. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  71. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  72. # GNU General Public License for more details.
  73. #
  74. # You should have received a copy of the GNU General Public License
  75. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  76.  
Advertisement
Add Comment
Please, Sign In to add comment