KRITSADA

Encoder Example for iBIT

Oct 2nd, 2018
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let Cnt1 = 0
  2. let Cnt = 0
  3. control.onEvent(EventBusSource.MICROBIT_ID_IO_P8, EventBusValue.MICROBIT_PIN_EVT_RISE, () => {
  4.     Cnt += 1
  5. })
  6. control.onEvent(EventBusSource.MICROBIT_ID_IO_P12, EventBusValue.MICROBIT_PIN_EVT_FALL, () => {
  7.     Cnt1 += 1
  8. })
  9. control.onEvent(EventBusSource.MICROBIT_ID_IO_P8, EventBusValue.MICROBIT_PIN_EVT_FALL, () => {
  10.     Cnt += 1
  11. })
  12. control.onEvent(EventBusSource.MICROBIT_ID_IO_P12, EventBusValue.MICROBIT_PIN_EVT_RISE, () => {
  13.     Cnt1 += 1
  14. })
  15. pins.setEvents(DigitalPin.P8, PinEventType.Edge)
  16. pins.setEvents(DigitalPin.P12, PinEventType.Edge)
  17. Cnt = 0
  18. Cnt1 = 0
  19. basic.forever(() => {
  20.     if (input.buttonIsPressed(Button.A)) {
  21.         Cnt = 0
  22.         Cnt1 = 0
  23.         iBIT.Motor(ibitMotor.Forward, 50)
  24.     } else if (input.buttonIsPressed(Button.B)) {
  25.         Cnt = 0
  26.         Cnt1 = 0
  27.         iBIT.Motor(ibitMotor.Backward, 50)
  28.     } else if (Cnt >= 40) {
  29.         iBIT.MotorStop()
  30.     } else if (Cnt1 >= 40) {
  31.         iBIT.MotorStop()
  32.     }
  33. })
Advertisement
Add Comment
Please, Sign In to add comment