https://github.com/nstansby/rpi-rotary-encoder-python
[김민성] [오후 9:37] [라즈베리파이 + DC모터 + PYTHON] 2. 라즈베리파이로 엔코더 읽기 : 네이버 블로그 - https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=roboholic84&logNo=221045887703
from RPi.GPIO import GPIO
from time import sleep
CLK = 21
DT = 20
GPIO.setmode(GPIO.BCM)
GPIO.setup(CLK, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(DT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
counter_now = 0
clkLastState = GPIO.input(CLK)
def Test(channel):
clkState = GPIO.input(CLK)
dtState = GPIO.input(DT)
if clkState != clkLastState:
if dtState != clkState:
counter_now += 1
else:
counter_now -= 1
print (counter_now)
clkLastState = clkState
sleep(0.005)
GPIO.add_event_detect(CLK, GPIO.RISING, callback=Test)
while True :
print (CLK, DT, counter_now)
from RPi import GPIO
from time import sleep
clk = 17
dt = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
counter = 0
clkLastState = GPIO.input(clk)
try:
while True:
clkState = GPIO.input(clk)
dtState = GPIO.input(dt)
if clkState != clkLastState:
if dtState != clkState:
counter += 1
else:
counter -= 1
print counter
clkLastState = clkState
sleep(0.01)
finally:
GPIO.cleanup()
import random from itertools import count import pandas as pd import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation
plt.style.use('fivethirtyeight')
x_val = [] y_val = []
index = count()
def animate(i): x_val.append(next(index)) y_val.append(random.randint(0,5)) plt.cla() plt.plot(x_val, y_val)
ani = FuncAnimation(plt.gcf(), animate, interval = 1000)
plt.tight_layout() plt.show()
index = count()
next(index) -> 순차적인 수를 생성해냅니다.
random.randint(0,5) -> 0~5 사이의 랜덤한 정수를 생성합니다.
plt.cla() -> 앞선 그래프를 삭제합니다.