import time
from machine import Pin, ADC, PWM, I2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# Paramètres pour l'écran LCD I2C
I2C_ADDR = 0x27 # Adresse I2C du LCD (à vérifier avec un scanner I2C si nécessaire)
LCD_ROWS = 2
LCD_COLS = 16
# Initialisation de l'I2C pour le LCD
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
lcd = I2cLcd(i2c, I2C_ADDR, LCD_ROWS, LCD_COLS)
# Initialisation des broches
NTC = ADC(Pin(32))
IN1 = Pin(25, Pin.OUT, value=0) # Direction 1
IN2 = Pin(23, Pin.OUT, value=0) # Direction 2
ENA = Pin(13, Pin.OUT, value=1) # PWM pour moteur A
IN3 = Pin(18, Pin.OUT, value=0) # Ventilateur
motor_pwm = PWM(ENA)
motor_pwm.freq(1000)
pot = ADC(Pin(33))
button_marche = Pin(34, Pin.IN)
button_stop = Pin(35, Pin.IN)
ALARM_TONE_DURATION = 500
ALARM_TONE1 = 1000
ALARM_TONE2 = 1500
buzzer = PWM(Pin(19), freq=ALARM_TONE1, duty=0)
motor_running = False
motor_direction = "ASP"
previous_marche_state = 0
previous_stop_state = 0
def play_tone(frequency, duration):
buzzer.freq(frequency)
buzzer.duty(512)
time.sleep_ms(duration)
buzzer.duty(0)
time.sleep_ms(1)
def play_alarm():
play_tone(ALARM_TONE1, ALARM_TONE_DURATION)
play_tone(ALARM_TONE2, ALARM_TONE_DURATION)
def stop_alarm():
buzzer.duty(0)
def start_motor():
if motor_direction == "ASP":
IN1.on()
IN2.off()
else:
IN1.off()
IN2.on()
def stop_motor():
IN1.off()
IN2.off()
def temperature(ntc_value):
return 80 - ((ntc_value / 4095) * 104)
while True:
current_marche_state = button_marche.value()
current_stop_state = button_stop.value()
ntc_value = NTC.read()
temperature_en_C = temperature(ntc_value)
pot_value = pot.read()
motor_speed = int((pot_value / 4095) * 65535)
motor_speed_percent = int((pot_value / 4095) * 100)
if current_marche_state == 1 and previous_marche_state == 0:
motor_running = True
start_motor()
if current_stop_state == 1 and previous_stop_state == 0:
motor_running = False
stop_motor()
previous_marche_state = current_marche_state
previous_stop_state = current_stop_state
if temperature_en_C >= 60:
motor_running = False
stop_motor()
motor_pwm.duty_u16(0)
IN3.on()
play_alarm()
else:
IN3.off()
stop_alarm()
if motor_running and temperature_en_C < 60:
motor_pwm.duty_u16(motor_speed)
else:
motor_pwm.duty_u16(0)
# Affichage sur LCD
lcd.clear()
lcd.putstr('Temp: {:.2f}C\n'.format(temperature_en_C))
lcd.putstr('Vit: {}% Mot: {}'.format(motor_speed_percent, 'On' if motor_running else 'Off'))
time.sleep(2)