import RPi.GPIO as GPIO from LCD1602 import LCD_1602 import time BtnPin = 13 R = 4 G = 12 B = 6 TRIG = 17 ECHO = 18 buzzer = 20 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(TRIG, GPIO.OUT, initial=GPIO.LOW) GPIO.setup(ECHO, GPIO.IN) GPIO.setup(R, GPIO.OUT) GPIO.setup(B, GPIO.OUT) GPIO.setup(G, GPIO.OUT) GPIO.setup(buzzer, GPIO.OUT) GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.output(buzzer, GPIO.HIGH) m_lcd = LCD_1602(Address=0x27, bus_id=1, bl=1) flag = m_lcd.lcd_init() def get_distance(): GPIO.output(TRIG, GPIO.HIGH) time.sleep(0.000015) GPIO.output(TRIG, GPIO.LOW) while not GPIO.input(ECHO): pass t1 = time.time() while GPIO.input(ECHO): pass t2 = time.time() distance = round((t2-t1) * 340 / 2, 5) return distance def display_distance(distance): a = '%f'%distance m_lcd.lcd_display_string(0, 0, 'The distance is') m_lcd.lcd_display_string(0, 1, a) m_lcd.lcd_display_string(8, 1, 'm') def turn_on_red(): GPIO.output(R, GPIO.HIGH) def turn_on_green(): GPIO.output(G, GPIO.HIGH) def turn_on_blue(): GPIO.output(B, GPIO.HIGH) def turn_off_leds(): GPIO.output(R, GPIO.LOW) GPIO.output(G, GPIO.LOW) GPIO.output(B, GPIO.LOW) def turn_on_buzzer(): GPIO.output(buzzer, GPIO.LOW) def turn_off_buzzer(): GPIO.output(buzzer, GPIO.HIGH) def main(): while True: if GPIO.input(BtnPin) == 0: flag += 1 elif GPIO.input(BtnPin) == 1: pass if flag % 2 == 0: turn_off_leds() turn_on_buzzer() distance = get_distance() if distance < 0.2: turn_on_blue() turn_off_buzzer() display_distance(distance) time.sleep(1) elif flag % 2 == 1: turn_on_green() if __name__ == '__main__': main() GPIO.cleanup(),帮我把每一行代码注释一下
时间: 2023-11-16 10:04:45 浏览: 184
```python
import RPi.GPIO as GPIO # 引入RPi.GPIO库,用于控制GPIO口
from LCD1602 import LCD_1602 # 引入LCD1602类,用于控制LCD屏幕
import time # 引入time库,用于延时
BtnPin = 13 # 按钮引脚
R = 4 # 红色LED引脚
G = 12 # 绿色LED引脚
B = 6 # 蓝色LED引脚
TRIG = 17 # 超声波模块Trig引脚
ECHO = 18 # 超声波模块Echo引脚
buzzer = 20 # 蜂鸣器引脚
GPIO.setwarnings(False) # 关闭警告
GPIO.setmode(GPIO.BCM) # 使用BCM编码方式
GPIO.setup(TRIG, GPIO.OUT, initial=GPIO.LOW) # 设置超声波模块Trig引脚为输出,初值低电平
GPIO.setup(ECHO, GPIO.IN) # 设置超声波模块Echo引脚为输入
GPIO.setup(R, GPIO.OUT) # 设置红色LED引脚为输出
GPIO.setup(B, GPIO.OUT) # 设置蓝色LED引脚为输出
GPIO.setup(G, GPIO.OUT) # 设置绿色LED引脚为输出
GPIO.setup(buzzer, GPIO.OUT) # 设置蜂鸣器引脚为输出
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # 设置按钮引脚为输入,启用上拉电阻
GPIO.output(buzzer, GPIO.HIGH) # 初始化蜂鸣器引脚为高电平
m_lcd = LCD_1602(Address=0x27, bus_id=1, bl=1) # 创建LCD1602对象
flag = m_lcd.lcd_init() # 初始化LCD屏幕,获取初始化状态
def get_distance(): # 获取超声波模块测量的距离
GPIO.output(TRIG, GPIO.HIGH) # 发送高电平信号至Trig引脚
time.sleep(0.000015) # 持续15微秒
GPIO.output(TRIG, GPIO.LOW) # 发送低电平信号至Trig引脚
while not GPIO.input(ECHO): # 等待Echo引脚输入高电平
pass
t1 = time.time() # 记录开始时间
while GPIO.input(ECHO): # 等待Echo引脚输入低电平
pass
t2 = time.time() # 记录结束时间
distance = round((t2-t1) * 340 / 2, 5) # 计算距离
return distance
def display_distance(distance): # 在LCD屏幕上显示距离
a = '%f'%distance # 将距离转换为字符串类型
m_lcd.lcd_display_string(0, 0, 'The distance is') # 在第一行第一列显示"The distance is"
m_lcd.lcd_display_string(0, 1, a) # 在第二行第一列显示距离
m_lcd.lcd_display_string(8, 1, 'm') # 在第二行第八列显示"m"
def turn_on_red(): # 打开红色LED
GPIO.output(R, GPIO.HIGH)
def turn_on_green(): # 打开绿色LED
GPIO.output(G, GPIO.HIGH)
def turn_on_blue(): # 打开蓝色LED
GPIO.output(B, GPIO.HIGH)
def turn_off_leds(): # 关闭所有LED
GPIO.output(R, GPIO.LOW)
GPIO.output(G, GPIO.LOW)
GPIO.output(B, GPIO.LOW)
def turn_on_buzzer(): # 打开蜂鸣器
GPIO.output(buzzer, GPIO.LOW)
def turn_off_buzzer(): # 关闭蜂鸣器
GPIO.output(buzzer, GPIO.HIGH)
def main(): # 主函数
while True: # 无限循环
if GPIO.input(BtnPin) == 0: # 如果按钮被按下
flag += 1 # 将flag加1
elif GPIO.input(BtnPin) == 1: # 如果按钮被释放
pass # 不进行任何操作
if flag % 2 == 0: # 如果flag为偶数
turn_off_leds() # 关闭LED
turn_on_buzzer() # 打开蜂鸣器
distance = get_distance() # 获取距离
if distance < 0.2: # 如果距离小于0.2米
turn_on_blue() # 打开蓝色LED
turn_off_buzzer() # 关闭蜂鸣器
display_distance(distance) # 在LCD屏幕上显示距离
time.sleep(1) # 延时1秒
elif flag % 2 == 1: # 如果flag为奇数
turn_on_green() # 打开绿色LED
if __name__ == '__main__':
main() # 运行主函数
GPIO.cleanup() # 清除GPIO口
阅读全文