Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions rotary_encoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rpi_gpio'

RPi::GPIO.set_numbering :bcm

CLOCK = 17
DATA = 18

RPi::GPIO.setup(CLOCK, as: :input)
RPi::GPIO.setup(DATA, as: :input)

def dtState
RPi::GPIO.high?(DATA) ? 1 : 0
end

def clock_state
RPi::GPIO.high?(CLOCK) ? 1 : 0
end

counter = 0
clock_last_state = clock_state

loop do
if clock_state != clock_last_state
if dtState != clock_state
counter += 1
else
counter -= 1
end

puts counter
end

clock_last_state = clock_state
sleep 0.01
end

at_exit do
RPi::GPIO.clean_up
end