Advertisement
rric

gyro_sensor

Jan 22nd, 2024
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. # Reads the gyro sensor, and displays a '+' accordingly
  2. # Copyright 2023, 2024 Roland Richter                [Mu: BBC micro:bit]
  3.  
  4. import microbit
  5.  
  6. def compute_position(value):
  7.     if value < -600:
  8.         return 0
  9.     elif value < -200:
  10.         return 1
  11.     elif value < 200:
  12.         return 2
  13.     elif value < 600:
  14.         return 3
  15.     else:
  16.         return 4
  17.  
  18.  
  19. # Draw a vertical line at column c (between 0 and 4)
  20. def draw_vertical_line(c):
  21.     microbit.display.set_pixel(c, 0, 9)
  22.     microbit.display.set_pixel(c, 1, 9)
  23.     microbit.display.set_pixel(c, 2, 9)
  24.     microbit.display.set_pixel(c, 3, 9)
  25.     microbit.display.set_pixel(c, 4, 9)
  26.  
  27.  
  28. # Draw a horizontal line at row r (between 0 and 4)
  29. def draw_horizontal_line(r):
  30.     microbit.display.set_pixel(0, r, 9)
  31.     microbit.display.set_pixel(1, r, 9)
  32.     microbit.display.set_pixel(2, r, 9)
  33.     microbit.display.set_pixel(3, r, 9)
  34.     microbit.display.set_pixel(4, r, 9)
  35.  
  36.  
  37. while True:
  38.     microbit.display.clear()
  39.     x_val = microbit.accelerometer.get_x()
  40.     col = compute_position(x_val)
  41.  
  42.     y_val = microbit.accelerometer.get_y()
  43.     row = compute_position(y_val)
  44.  
  45.     draw_vertical_line(col)
  46.     draw_horizontal_line(row)
  47.     microbit.sleep(100)
  48.  
  49.  
  50. # ----------------------------------------------------------------------
  51. # This program is free software: you can redistribute it and/or modify
  52. # it under the terms of the GNU General Public License as published by
  53. # the Free Software Foundation, either version 3 of the License, or
  54. # (at your option) any later version.
  55. #
  56. # This program is distributed in the hope that it will be useful,
  57. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  58. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  59. # GNU General Public License for more details.
  60. #
  61. # You should have received a copy of the GNU General Public License
  62. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement