Advertisement
rric

chronometer

Jan 22nd, 2024
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. # Turns your micro:bit into a very simple stopwatch
  2. # Copyright 2023, 2024 Roland Richter                [Mu: BBC micro:bit]
  3.  
  4. import microbit
  5.  
  6. microbit.display.show(microbit.Image.DIAMOND_SMALL)
  7.  
  8. # "pass" means "do nothing"; here, do nothing in a loop until A is pressed
  9. while not microbit.button_a.is_pressed():
  10.     pass
  11.  
  12. # set the start time (in milliseconds)
  13. start_time = microbit.running_time()
  14.  
  15. while not microbit.button_b.is_pressed():
  16.     # calculate the last three digits of the time elapsed, e.g.
  17.     #   917 ms -> 917
  18.     #  2451 ms -> 451
  19.     #  8056 ms ->  56
  20.     # 10189 ms -> 189
  21.     last_digits = (microbit.running_time() - start_time) % 1000
  22.  
  23.     if last_digits < 250:
  24.         microbit.display.show(microbit.Image.CLOCK12)
  25.     elif last_digits < 500:
  26.         microbit.display.show(microbit.Image.CLOCK3)
  27.     # TRY to complete this "if-then-else" statement to show a rotating clock hand
  28.  
  29. # set the stop time (in milliseconds)
  30. stop_time = microbit.running_time()
  31.  
  32. # show the elapsed time (in milliseconds)
  33. elapsed = stop_time - start_time
  34. microbit.display.scroll(str(elapsed) + " ms")
  35.  
  36. # TRY to show the elapsed time in seconds, instead of milliseconds.
  37.  
  38.  
  39. # ----------------------------------------------------------------------
  40. # This program is free software: you can redistribute it and/or modify
  41. # it under the terms of the GNU General Public License as published by
  42. # the Free Software Foundation, either version 3 of the License, or
  43. # (at your option) any later version.
  44. #
  45. # This program is distributed in the hope that it will be useful,
  46. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  47. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  48. # GNU General Public License for more details.
  49. #
  50. # You should have received a copy of the GNU General Public License
  51. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement