from machine import Pin,I2C, ADC, PWM
from i2c_lcd import I2cLcd # import lib
import dht
import time

#The PCF8574 has a jumper selectable address: 0x20 - 0x27
DEFAULT_I2C_ADDR = 0x27
sensor = dht.DHT22(Pin(16))
led = Pin(2,Pin.OUT)
vert = Pin(3, Pin.IN)
rouge = Pin(14, Pin.IN)


pwm_led = PWM(Pin(2,mode=Pin.OUT))
pwm_led.freq(1_000)
#pwm.duty_u16(32767)
pot_pin = Pin(28, mode=Pin.IN)
pot = ADC(pot_pin)
a=0
duty=0



"""Test function for verifying basic functionality."""
print("Running test_main")
i2c = I2C(0,scl=Pin(9), sda=Pin(8), freq=400000) # création de l'objet i2C
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16) # création de l'objet LCD i2c

while True:
    #temp = sensor.temperature()
    #hum = sensor.humidity()
    #logic_state = push_button.value()
    #led.value(1)
    lcd.move_to(0,0)
    lcd.putstr("Devoir m.python")
    lcd.move_to(0,1)
    lcd.putstr("BT rouge: suivan")
    time.sleep(1)
    lcd.clear()
    lcd.move_to(0,0)
    lcd.putstr("Devoir m.python")
    lcd.move_to(0,1)
    lcd.putstr("BT vert: sortir")
    time.sleep(1)
    #valpot=pot.read_u16()
    
    
    
    while(a==0):
        if(rouge.value()==1):
            a=1
        while(a==1):
            if(rouge.value()==1):
                a=0
                led.value(0)
            sensor.measure()
            temp = sensor.temperature()
            hum = sensor.humidity()
            valpot=pot.read_u16()
            valpot=valpot * (100/65535)
            pwm_led.duty_u16(int(valpot))
            if(valpot >= hum):
                led.value(1)
            if(valpot <= hum): 
                led.value(0)
            lcd.move_to(0,0)
            lcd.putstr("Pot :   "+str(valpot))
            lcd.move_to(0,1)
            lcd.putstr("humid:    "+str(hum)) 
            time.sleep(1)
            lcd.clear()
            if(vert.value()==1):
                a=1
                break
            
   
    #val = adc.read_u16()
    #pwm_led.duty_u16(val)
   
    #duty=duty+(255*str(hum))
            #pwm_led.duty_u16(duty)
            #if duty >= 65535 : 
            #val = pot.read_u16()
            #valpot=pot.read_u16()
            #duty_u16=(duty_u7+1)*128-1
            #value = pot.read_u16()
            #pwm_led.duty_u16(int(value))

            #for duty in range(101):
                #pwm_led.duty_u16(int((duty/100)*100))
                #valpot=pot.read_u16()
    
  #if (valpot<=valpot):
                #lcd.move_to(0,0)
                #lcd.putstr("Pot :   "+str(valpot))
                #lcd.move_to(0,1)
                #lcd.putstr("humid:    "+str(hum))
                #led.value(0)

            #if(valpot>65535/2 and valpot!=65535):
                #lcd.move_to(0,0)
                #lcd.putstr("Pot :   "+str(valpot))
                #lcd.move_to(0,1)
                #lcd.putstr("humid:    "+str(hum))
                #pwm_led.duty_u16(valpot*2-65535)
            
            #if(valpot==65535):
                #led.value(1)
                #lcd.move_to(0,0)
                #lcd.putstr("Pot :   "+str(valpot))
                #lcd.move_to(0,1)
                #lcd.putstr("humid:    "+str(hum))
#pwm_led.duty_u16(int(valpot))
            #if duty>= int(valpot) : led.value(1)
            #if duty <= int(valpot) : led.value(0) 
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT