This is the MakeCode package for the KY-040 rotary encoder.
- Connect the appropriate pins (CLK, DT, SW, GND) to the micro:bit.
Initialises the rotary encoder.
Sets up the micro:bit to use the rotary encoder.
YFRotaryEncoder.init(P0, P1, P2)
This block must be placed before any of the other blocks in this list.
Button push event.
Called whenever the button is pushed down.
YFRotaryEncoder.onPressEvent(() => {
basic.showString("Button pressed!")
})
Rotation event.
Called whenever the encoder detects rotation.
YFRotaryEncoder.onRotateEvent(YFRotationDirection.Left, () => {
basic.showString("<")
})
YFRotaryEncoder.onRotateEvent(YFRotationDirection.Right, () => {
basic.showString(">")
})
The follow code creates a number input that can be adjusted by turning the encoder.
YFRotaryEncoder.init(P0, P1, P2)
let item = 0
basic.showNumber(item)
YFRotaryEncoder.onRotateEvent(RotationDirection.Left, () => {
item -= 1
basic.showNumber(item)
})
YFRotaryEncoder.onRotateEvent(RotationDirection.Right, () => {
item += 1
basic.showNumber(item)
})
YFRotaryEncoder.onPressEvent(() => {
basic.showString("selected!")
})
- for PXT/microbit