树莓派旋转编码开关(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
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动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例