ledangle.py 522 B

12345678910111213141516171819202122232425
  1. import pyb
  2. def led_angle(seconds_to_run_for):
  3. # make LED objects
  4. l1 = pyb.LED(1)
  5. l2 = pyb.LED(2)
  6. accel = pyb.Accel()
  7. for i in range(20 * seconds_to_run_for):
  8. # get x-axis
  9. x = accel.x()
  10. # turn on LEDs depending on angle
  11. if x < -10:
  12. l1.on()
  13. l2.off()
  14. elif x > 10:
  15. l1.off()
  16. l2.on()
  17. else:
  18. l1.off()
  19. l2.off()
  20. # delay so that loop runs at at 1/50ms = 20Hz
  21. pyb.delay(50)