accel.rst 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. The accelerometer
  2. =================
  3. Here you will learn how to read the accelerometer and signal using LEDs states like tilt left and tilt right.
  4. Using the accelerometer
  5. -----------------------
  6. The pyboard has an accelerometer (a tiny mass on a tiny spring) that can be used
  7. to detect the angle of the board and motion. There is a different sensor for
  8. each of the x, y, z directions. To get the value of the accelerometer, create a
  9. pyb.Accel() object and then call the x() method. ::
  10. >>> accel = pyb.Accel()
  11. >>> accel.x()
  12. 7
  13. This returns a signed integer with a value between around -30 and 30. Note that
  14. the measurement is very noisy, this means that even if you keep the board
  15. perfectly still there will be some variation in the number that you measure.
  16. Because of this, you shouldn't use the exact value of the x() method but see if
  17. it is in a certain range.
  18. We will start by using the accelerometer to turn on a light if it is not flat. ::
  19. accel = pyb.Accel()
  20. light = pyb.LED(3)
  21. SENSITIVITY = 3
  22. while True:
  23. x = accel.x()
  24. if abs(x) > SENSITIVITY:
  25. light.on()
  26. else:
  27. light.off()
  28. pyb.delay(100)
  29. We create Accel and LED objects, then get the value of the x direction of the
  30. accelerometer. If the magnitude of x is bigger than a certain value ``SENSITIVITY``,
  31. then the LED turns on, otherwise it turns off. The loop has a small ``pyb.delay()``
  32. otherwise the LED flashes annoyingly when the value of x is close to
  33. ``SENSITIVITY``. Try running this on the pyboard and tilt the board left and right
  34. to make the LED turn on and off.
  35. **Exercise: Change the above script so that the blue LED gets brighter the more
  36. you tilt the pyboard. HINT: You will need to rescale the values, intensity goes
  37. from 0-255.**
  38. Making a spirit level
  39. ---------------------
  40. The example above is only sensitive to the angle in the x direction but if we
  41. use the ``y()`` value and more LEDs we can turn the pyboard into a spirit level. ::
  42. xlights = (pyb.LED(2), pyb.LED(3))
  43. ylights = (pyb.LED(1), pyb.LED(4))
  44. accel = pyb.Accel()
  45. SENSITIVITY = 3
  46. while True:
  47. x = accel.x()
  48. if x > SENSITIVITY:
  49. xlights[0].on()
  50. xlights[1].off()
  51. elif x < -SENSITIVITY:
  52. xlights[1].on()
  53. xlights[0].off()
  54. else:
  55. xlights[0].off()
  56. xlights[1].off()
  57. y = accel.y()
  58. if y > SENSITIVITY:
  59. ylights[0].on()
  60. ylights[1].off()
  61. elif y < -SENSITIVITY:
  62. ylights[1].on()
  63. ylights[0].off()
  64. else:
  65. ylights[0].off()
  66. ylights[1].off()
  67. pyb.delay(100)
  68. We start by creating a tuple of LED objects for the x and y directions. Tuples
  69. are immutable objects in python which means they can't be modified once they are
  70. created. We then proceed as before but turn on a different LED for positive and
  71. negative x values. We then do the same for the y direction. This isn't
  72. particularly sophisticated but it does the job. Run this on your pyboard and you
  73. should see different LEDs turning on depending on how you tilt the board.