from machine import Pin, I2C, PWM, ADC
import utime
from dht import DHT22
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# Bileşenler
led = Pin(3, Pin.OUT, value=0)
mode_button = Pin(4, Pin.IN, Pin.PULL_UP) # Otomatik/manuel geçiş butonu
screen_button = Pin(5, Pin.IN, Pin.PULL_UP) # LCD gösterim geçiş butonu
# LCD Ayarları
i2c = I2C(0, sda=Pin(0), scl=Pin(1))
lcd = I2cLcd(i2c, 0x27, 2, 16)
# DHT22 Sensörü
dht = DHT22(Pin(2))
# Servo Ayarı
servo = PWM(Pin(16))
servo.freq(50)
# CD74HC4067 Çoklayıcı Ayarları
MUX_S0 = Pin(6, Pin.OUT)
MUX_S1 = Pin(7, Pin.OUT)
MUX_S2 = Pin(8, Pin.OUT)
MUX_S3 = Pin(9, Pin.OUT)
MUX_SIG = ADC(Pin(26)) # ADC0
# Değişkenler
manual_mode = False
led_override = False
current_channel = 0
screen_mode = 0 # 0: Temp/Hum, 1: Potansiyometre
input_value = 0 # 0: Potansiyometre, 1: LED
last_mode_button_time = 0
last_screen_button_time = 0
def servo_angle(angle):
duty = int(2000 + (angle / 180) * 6000)
servo.duty_u16(duty)
def update_display(temp, hum, mux_value, channel):
lcd.clear()
if screen_mode == 0:
lcd.putstr(f"T:{temp:.1f}C H:{hum:.1f}%")
lcd.move_to(0, 1)
lcd.putstr(f"Mode: {'MAN' if manual_mode else 'AUTO'}")
elif screen_mode == 1:
lcd.putstr(f"CH{channel}: {mux_value}")
lcd.move_to(0, 1)
lcd.putstr(f"{'MAN' if manual_mode else 'AUTO'}")
def read_mux(channel):
select_channel(channel)
utime.sleep_ms(2)
return MUX_SIG.read_u16()
def select_channel(channel):
global current_channel
MUX_S0.value(channel & 0b0001)
MUX_S1.value((channel & 0b0010) >> 1)
MUX_S2.value((channel & 0b0100) >> 2)
MUX_S3.value((channel & 0b1000) >> 3)
current_channel = channel
utime.sleep_ms(1)
def input_check():
global input_value
# Kullanıcı inputu almak için: Bu kısmı, buton veya başka bir input ile değiştirebilirsiniz.
# Örneğin, buton 1'i kullanarak input'u değiştirebiliriz (btn2 ile).
if screen_button.value() == 0:
input_value = 1 if input_value == 0 else 0 # 0 --> 1, 1 --> 0
def control_led_or_potentiometer():
global input_value
if input_value == 0:
# Potansiyometreyi kontrol et
mux_value = read_mux(current_channel)
update_display(temp, hum, mux_value, current_channel)
elif input_value == 1:
# LED kontrol et
led.value(1 if mux_value > 512 else 0) # LED'i açma/kapama
while True:
try:
now = utime.ticks_ms()
# Mod değiştirme butonu
if mode_button.value() == 0 and (now - last_mode_button_time) > 500:
last_mode_button_time = now
manual_mode = not manual_mode
led_override = manual_mode
if manual_mode:
led.value(0)
# Ekran geçiş butonu
if screen_button.value() == 0 and (now - last_screen_button_time) > 500:
last_screen_button_time = now
screen_mode = (screen_mode + 1) % 2
# Input kontrolü (potansiyometre mi, LED mi?)
input_check()
# Sensör okumaları
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# LCD güncelleme
update_display(temp, hum, mux_value, current_channel)
# Servo kontrolü
if temp > 50:
servo_angle(90)
elif temp > 30:
servo_angle(180)
else:
servo_angle(0)
# LED kontrolü
if not led_override:
control_led_or_potentiometer() # Yeni kontrol fonksiyonunu çağırıyoruz
except Exception as e:
print("Hata:", e)
lcd.clear()
lcd.putstr("Hata olustu")
lcd.move_to(0, 1)
lcd.putstr(str(e)[:16])
utime.sleep(0.1)
Loading
cd74hc4067
cd74hc4067