您的位置:首页 > 编程语言 > Python开发

树莓派旋转编码开关(python程序)

2016-06-11 18:02 435 查看
1,工具:pri3,微雪Rotation Sensor

2,基本原理:

Rotation Sensor SIA SIB SW分别与树莓派3、5、7PIN链接

Rotation Sensor SIA和SIB组合判断旋转方向

SIA开始从1到0时 读取SIB电位 记为lastSib

SIA从0到1时 读取SIB电位 记为currentSib

lastSib 0 currentSib 1 顺时针旋转

lastSib 1 currentSib 0 逆时针旋转

SW从1到0复位

3,GPIO管脚连接实物图



4,测试代码
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

SIA = 3
SIB= 5
SW = 7

GPIO.setup(SW,GPIO.IN)
GPIO.setup(SIA,GPIO.IN)
GPIO.setup(SIB,GPIO.IN)
flag = 0
resetflag = 0
globalCount = 0
while 1:
lastSib = GPIO.input(SIB)
while not GPIO.input(SW):
resetflag = 1
while not GPIO.input(SIA):
currentSib = GPIO.input(SIB)
flag =1
if resetflag:
globalCount = 0
resetflag = 0
print 'Count reset'
continue
if flag:
if lastSib == 0 and currentSib == 1:
print 'right rotate'
globalCount += 1
if lastSib == 1 and currentSib ==0:
print 'left rotate'
globalCount -=1
flag =0
print 'current = %s' % globalCount
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  树莓派 python 传感器