microbit-voting.py 1.8 KB

123456789101112131415161718192021222324252627282930313233
  1. # Micro:bit Voting Application
  2. # Copyright Owen Maple, All Rights Reserved
  3. from microbit import * # This imports the micro:bit packages from the micro:bit
  4. # The variables below are set to 0 to create the variable for use in the following code
  5. votea = 0
  6. voteb = 0
  7. startup = 0
  8. # A while statement is used to continuously loop the code within it, this is required for the Micro:bit to function properly
  9. while True:
  10. # The if statement below checks whether the variable startup is set to 0, if it is then it will instruct the user what to do, if not then the voting program will run
  11. if startup == 0:
  12. display.scroll("Vote A or B")
  13. startup = 1
  14. display.clear() # This clears the screen of current content
  15. else:
  16. # The if statement below activates when the A button is pressed, it is detected by a function within the micro:bit packages
  17. if button_a.is_pressed():
  18. votea = votea + 1 # Adds 1 vote to the votea variable
  19. display.show(str(votea)) # This converts the variable votea into a string and shows it on the micro:bit display
  20. sleep(500)
  21. display.clear()
  22. # The eflif statement below activates when the B button is pressed, it is is detected by a function within the micro:bit packages
  23. elif button_b.is_pressed():
  24. voteb = voteb + 1 # Adds 1 vote to the voteb variable
  25. display.show(str(voteb)) # This converts the variable voteb into a string and shows it on the micro:bit display
  26. sleep(500)
  27. display.clear()
  28. # The elif statement below activates when the device is shaken, it is detected by an accelerometer and functions within the micro:bit packages
  29. elif accelerometer.was_gesture("shake"):
  30. display.scroll("A:" + str(votea) + " B:" + str(voteb) + " ")
  31. display.clear()