'WIRE LEGEND for Rotary Encoder
'VCC/+ = 3.3V to 5V
'GND = GND
'SW = D2
'DT = D3
'CLK = D4
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Private CLK, DT, SW As Pin
Private Counter As Int = 0
Private LastStateTime As Long = 0
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
SW.Initialize(2, SW.MODE_INPUT_PULLUP)
SW.AddListener("SW_StateChanged")
DT.Initialize(3, DT.MODE_INPUT)
DT.AddListener("DT_StateChanged")
CLK.Initialize(4, CLK.MODE_INPUT)
End Sub
Sub DT_StateChanged (State As Boolean)
Dim StateTime As Long = Millis()
If StateTime - LastStateTime > 50 Then 'Only run if statechange firing interval is larger than 50ms, adjust to suit
If CLK.DigitalRead Then Counter = Counter - 1 Else Counter = Counter + 1 'Adjust the counter +-1
Counter = Min(100, Max(0, Counter)) 'Limit the counter between 0 and 100
LastStateTime = StateTime 'Store last state time
Log("Position: ", Counter)
End If
End Sub
Sub SW_StateChanged (State As Boolean)
If Not(State) Then Log("Clicked") 'RE Button has been clicked
End Sub