Advertisement
rric

here_and_there

Jan 22nd, 2024
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. # Turns two micro:bits into two "counterparts", counting clicks of each other
  2. # Copyright 2023, 2024 Roland Richter                [Mu: BBC micro:bit]
  3.  
  4. import microbit
  5. import radio
  6.  
  7. radio.on()
  8. radio.config(channel=)  # set your channel here
  9. radio.config(power=3)
  10.  
  11. count = 0
  12.  
  13. while True:
  14.     # button A: increment my *counterparts* count
  15.     if microbit.button_a.is_pressed():
  16.         radio.send(str(1))
  17.         while microbit.button_a.is_pressed():
  18.             pass
  19.  
  20.     # button B: reset my *own* count
  21.     if microbit.button_b.is_pressed():
  22.         count = 0
  23.         while microbit.button_b.is_pressed():
  24.             pass
  25.  
  26.     # increment my own count if my *counterpart* sent a message
  27.     incoming = radio.receive()
  28.  
  29.     if incoming is not None:
  30.         if int(incoming) == 1:
  31.             count += 1
  32.  
  33.     microbit.display.show(str(count))
  34.     microbit.sleep(100)
  35.  
  36.  
  37. # ----------------------------------------------------------------------
  38. # This program is free software: you can redistribute it and/or modify
  39. # it under the terms of the GNU General Public License as published by
  40. # the Free Software Foundation, either version 3 of the License, or
  41. # (at your option) any later version.
  42. #
  43. # This program is distributed in the hope that it will be useful,
  44. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  46. # GNU General Public License for more details.
  47. #
  48. # You should have received a copy of the GNU General Public License
  49. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement