期末作业: 已知树莓派4B中,将雨滴传感器、LM393模块、PCF8591模块、RGB-LED灯、双色LED灯、倾斜传感器、循迹传感器、有源蜂鸣器连接 (1)当雨滴传感器探测板--LM393模块检测到下雨时,RGB-LED灯将红灯蓝灯绿灯交替闪烁;未下雨时,RGB-LED灯不亮 (2)当倾斜传感器检测到倾斜时,蜂鸣器以0.5秒响一次,未检测到倾斜时,蜂鸣器不响。 (3)当循迹传感器检测到黑色物体时,双色LED灯亮红灯,未检测到黑色物体亮绿灯。 (4)将雨滴、倾斜、循迹这三个模块的状态用pygame同时显示在同一屏幕上 (5)初始状态中,RGB-LED灯与双色LED灯都不亮 连接状态为: 雨滴传感器探测板的两个引脚与LM393模块的一侧连接,LM393模块的DO连接GPIO.6,AO连接PCF8591模块的AIN0 RGB-LED灯中R连接GPIO.12,G连接GPIO.13,B连接GPIO.16 双色LED灯中G连接GPIO.27,R连接GPIO.18 倾斜传感器中DO口连接GPIO.17 循迹传感器中DO口连接GPIO.23 有源蜂鸣器中I/O口连接GPIO.22 上述已经连好 那么只需要写出main.py,main.py需要使用PCF8591.py
时间: 2024-03-18 18:43:51 浏览: 63
好的,下面是主要的代码实现:
```python
import RPi.GPIO as GPIO
import time
import PCF8591 as ADC
import pygame
# 定义GPIO口
LED_R = 12
LED_G = 13
LED_B = 16
LED_G2 = 27
LED_R2 = 18
BUZZER = 22
TILT_SENSOR = 17
TRACK_SENSOR = 23
RAIN_SENSOR = 6
# 颜色定义
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0,0,0)
WHITE = (255,255,255)
# 初始化GPIO口
def init():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(LED_R, GPIO.OUT)
GPIO.setup(LED_G, GPIO.OUT)
GPIO.setup(LED_B, GPIO.OUT)
GPIO.setup(LED_G2, GPIO.OUT)
GPIO.setup(LED_R2, GPIO.OUT)
GPIO.setup(BUZZER, GPIO.OUT)
GPIO.setup(TILT_SENSOR, GPIO.IN)
GPIO.setup(TRACK_SENSOR, GPIO.IN)
GPIO.setup(RAIN_SENSOR, GPIO.IN)
# RGB灯交替闪烁
def blink():
time.sleep(1)
GPIO.output(LED_R, GPIO.HIGH)
GPIO.output(LED_G, GPIO.LOW)
GPIO.output(LED_B, GPIO.LOW)
time.sleep(1)
GPIO.output(LED_R, GPIO.LOW)
GPIO.output(LED_G, GPIO.HIGH)
GPIO.output(LED_B, GPIO.LOW)
time.sleep(1)
GPIO.output(LED_R, GPIO.LOW)
GPIO.output(LED_G, GPIO.LOW)
GPIO.output(LED_B, GPIO.HIGH)
time.sleep(1)
# RGB灯不亮
def off():
GPIO.output(LED_R, GPIO.LOW)
GPIO.output(LED_G, GPIO.LOW)
GPIO.output(LED_B, GPIO.LOW)
# 双色LED灯亮红灯
def red():
GPIO.output(LED_G2, GPIO.LOW)
GPIO.output(LED_R2, GPIO.HIGH)
# 双色LED灯亮绿灯
def green():
GPIO.output(LED_R2, GPIO.LOW)
GPIO.output(LED_G2, GPIO.HIGH)
# 蜂鸣器响
def beep():
GPIO.output(BUZZER, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(BUZZER, GPIO.LOW)
time.sleep(0.5)
# 蜂鸣器不响
def un_beep():
GPIO.output(BUZZER, GPIO.LOW)
# 获取倾斜传感器的状态
def get_tilt_status():
return GPIO.input(TILT_SENSOR)
# 获取循迹传感器的状态
def get_track_status():
return GPIO.input(TRACK_SENSOR)
# 获取雨滴传感器的状态
def get_rain_status():
return GPIO.input(RAIN_SENSOR)
# 初始化ADC0832模块
def init_ADC():
ADC.setup(0x48)
# 获取ADC0832模块的值
def get_ADC_value():
value = ADC.read(0)
return value
# 显示传感器状态
def show_status(screen, font):
tilt_status = get_tilt_status()
track_status = get_track_status()
rain_status = get_rain_status()
text = "Tilt Sensor: %d, Track Sensor: %d, Rain Sensor: %d" % (tilt_status, track_status, rain_status)
text_surface = font.render(text, True, WHITE)
screen.blit(text_surface, (10, 10))
# 主程序
def main():
init()
init_ADC()
# 初始化Pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Sensor Status")
font = pygame.font.Font(None, 36)
# 主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
# 获取传感器的值
tilt_value = get_ADC_value()
track_value = get_ADC_value()
# 显示传感器状态
screen.fill(BLACK)
show_status(screen, font)
# 当雨滴传感器探测板--LM393模块检测到下雨时
if get_rain_status() == 1:
blink()
# 未下雨时
else:
off()
# 当倾斜传感器检测到倾斜时
if tilt_value > 200:
beep()
else:
un_beep()
# 当循迹传感器检测到黑色物体时
if track_value > 200:
red()
else:
green()
# 更新显示
pygame.display.update()
# 等待一段时间
time.sleep(0.1)
if __name__ == '__main__':
main()
```
以上代码中,使用了PCF8591.py模块来读取ADC0832模块的值,并使用Pygame库来实现将传感器状态显示在屏幕上。在主程序中,不断读取传感器的值,根据不同的传感器状态控制LED灯和蜂鸣器的状态,同时将传感器状态显示在屏幕上。
阅读全文