我想知道如何接近PWM输出并交替LCD上的每个传感器读数。我最近刚改变了它。我可以得到每个传感器的读数,但是当达到输入的限制时,PWM没有变化。我在下面粘贴的代码是enter code here
的新方法。我必须上课吗?
此前的代码工作原理相同,但每个传感器LCD显示屏都在代码的末尾。
`import os
import glob
import RPi.GPIO as GPIO # IMPORT GPIOS
import time # IMPORT CURRENT TIME
from RPLCD import CharLCD, BacklightMode # IMPORT LCD DISPLAY CODE
GPIO.setwarnings(False)
lcd = CharLCD(cols = 16, rows=2, pin_rs=26,pin_e=24,pins_data=[22,32,36,37])
# SET OUTPUTS
GPIO.setup(29,GPIO.OUT)
GPIO.setup(31,GPIO.OUT)
GPIO.setup(33,GPIO.OUT)
# Set frequency limits
RED = GPIO.PWM(29,100)
GREEN = GPIO.PWM(31,100)
BLUE = GPIO.PWM(33,100)
# High Temperature Limits
tHIGHroom1 = input('Enter High Limit for room 1: ')
tHIGHroom2 = input('Enter High Limit for room 2: ')
tHIGHroom3 = input('Enter High Limit for room 3: ')
# Low Temperature Limits
tLOWroom1 = input('Enter Low Limit for room 1: ')
tLOWroom2 = input('Enter Low Limit for room 2: ')
tLOWroom3 = input('Enter Low Limit for room 3: ')
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
# DEFINE EACH SENSOR
temp_sensor1 = '/sys/bus/w1/devices/28-0000075f7386/w1_slave' # CHANGE DEVICE ID IF NEEDED
temp_sensor2 = '/sys/bus/w1/devices/28-0000075ffa1b/w1_slave'
temp_sensor3 = '/sys/bus/w1/devices/28-00000744d665/w1_slave'
while True:
def temp_raw():
f = open(temp_sensor1, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = temp_raw()
temp_output = lines[1].find('t=')
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = int(temp_string) / 1000.0
temp_f1 = temp_c * 9 / 5 +32
temp_f1 = str(round(temp_f1,1))
return temp_f1
def sesor1():
value1 = read_temp()
if value1 >= tHIGHroom1:
RED.start(70)
GREEN.start(100)
BLUE.start(100)
return none
if value1 <= tLOWroom1:
RED.start(100)
GREEN.start(100)
BLUE.start(60)
return none
if tLOWrooom1 <= value1 <= tHIGHroom1:
RED.start(0)
GREEN.start(50)
BLUE.start(50)
return none
lcd.cursor_pos = (0,0)
lcd.write_string("Room1: "+read_temp()+unichr(223))
lcd.cursor_pos = (1,0)
lcd.write_string("Hi:" + str(tHIGHroom1) + " Lo:" + str(tLOWroom1))
time.sleep(4)
RED.stop()
GREEN.stop()
BLUE.stop()
lcd.clear()
def temp_rawr():
f = open(temp_sensor2, 'r')
lines = f.readlines()
f.close()
return lines
def read_tempr():
lines = temp_rawr()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = temp_rawr()
temp_output = lines[1].find('t=')
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = int(temp_string) / 1000.0
temp_f2 = temp_c * 9 / 5 +32
temp_f2 = str(round(temp_f2,1))
return temp_f2
def sensor2():
value2 = read_tempr()
if value2 >= tHIGHroom2:
RED.start(70)
GREEN.start(100)
BLUE.start(100)
return none
if value2 <= tLOWroom2:
RED.start(100)
GREEN.start(100)
BLUE.start(60)
return none
if tLOWrooom2 <= value2 <= tHIGHroom2:
RED.start(0)
GREEN.start(50)
BLUE.start(50)
return none
lcd.cursor_pos = (0,0)
lcd.write_string("Room2: "+read_tempr()+unichr(223))
lcd.cursor_pos = (1,0)
lcd.write_string("Hi:" + str(tHIGHroom2) + " Lo:" + str(tLOWroom2))
time.sleep(4)
RED.stop()
GREEN.stop()
BLUE.stop()
lcd.clear()
def temp_rawx():
f = open(temp_sensor3, 'r')
lines = f.readlines()
f.close() n
return lines
def read_tempx():
lines = temp_rawx()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = temp_rawx()
temp_output = lines[1].find('t=')
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = int(temp_string) / 1000.0
temp_f3 = temp_c * 9 / 5 +32
temp_f3 = str(round(temp_f3,1))
return temp_f3
def sensor3():
value3 = read_tempx()
if value3 >= tHIGHroom3:
RED.start(70)
GREEN.start(100)
BLUE.start(100)
return none
if value3 <= tLOWroom3:
RED.start(100)
GREEN.start(100)
BLUE.start(60)
return none
if tLOWrooom3 <= value3 <= tHIGHroom3:
RED.start(0)
GREEN.start(50)
BLUE.start(50)
return none
lcd.cursor_pos = (0,0)
lcd.write_string("Room3: "+read_temp()+unichr(223))
lcd.cursor_pos = (1,0)
lcd.write_string("Hi:" + str(tHIGHroom3) + " Lo:" + str(tLOWroom3))
time.sleep(4)
RED.stop()
GREEN.stop()
BLUE.stop()
lcd.clear()`