组件:
环境:
四路巡线模块连接的树莓派引脚是11, 7, 13, 15。
#-*- coding:UTF-8 -*- # 导入GPIO和time库 import RPi.GPIO as GPIO import time # 设置GPIO的编号模式 Tracking_Leftmost = 13 # 左边第一个传感器 Tracking_Left = 15 # 左边第二个传感器 Tracking_Right = 11 # 右边第一个传感器 Tracking_Rightmost = 7 # 右边第二个传感器 # 设置引脚的编码方式 GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) # 忽略警告 # 设置循迹模块的GPIO输入 GPIO.setup(Tracking_Leftmost, GPIO.IN) GPIO.setup(Tracking_Left, GPIO.IN) GPIO.setup(Tracking_Right, GPIO.IN) GPIO.setup(Tracking_Rightmost, GPIO.IN) # 读取循迹模块的值,并打印出来。 try: while True: Tracking_Leftmost_Value = GPIO.input(Tracking_Leftmost); Tracking_Left_Value = GPIO.input(Tracking_Left); Tracking_Right_Value = GPIO.input(Tracking_Right); Tracking_Rightmost_Value = GPIO.input(Tracking_Rightmost); print(Tracking_Leftmost_Value, end='') print(Tracking_Left_Value, end='') print(Tracking_Right_Value, end='') print(Tracking_Rightmost_Value) time.sleep(1) except KeyboardInterrupt: pass print("Ending") GPIO.cleanup() # 清除GPIO
输出结果:
0000
1100
1110
1111