from machine import Pin, PWM, ADC, I2C
import ssd1306
import dht
import time
import leds
import random
import Button

sensor = dht.DHT22(Pin(14))
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
photosensor = ADC(Pin(34))
button1 = Button.Button(27)
button2 = Button.Button(26)

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
show_initial_message=0
ledoff=0
while True:
    oled.fill(0)#Como un clear
    sensor.measure()
    temp = sensor.temperature()
    humidity = sensor.humidity()
    lightvalue = photosensor.read()
    voltage = lightvalue/4095*3.3
    resistance = 2000*voltage/(1-voltage/3.3)
    light = pow(33*1000*pow(10, 0.7)/resistance,(1/0.7))

    oled.text('Temp: {:.1f}C'.format(temp), 0, 0)
    oled.text('Humidity: {:.1f}%'.format(humidity), 0, 20)
    oled.text('Light: {:.1f}lux'.format(light), 0, 40)
    oled.show()
    time.sleep(1)
    oled.fill(0)

    if 15<temp<20 and 50<humidity<70 and 2000<light<5000:
        leds.blue()
        oled.text('Turn on Blue Light', 0, 20)
        oled.text('Promotes initial growth', 0, 30)
        oled.text('growth', 0, 40)
        oled.show()
        time.sleep(1.5)
        oled.fill(0)
    elif temp>25 and 40<humidity<60 and light>5000:
        leds.red()
        oled.text('Turn on Red Light', 0, 20)
        oled.text('Promotes flower', 0, 30)
        oled.text('and fruit', 0, 40)
        oled.show()
        time.sleep(1.5)
        oled.fill(0)
    elif 20<temp<25 and 40<humidity<70 and 2000<light<5000:
        leds.white()
        oled.text('Turn on White Light', 0, 20)
        oled.text('Balanced spectrum ', 0, 30)
        oled.text('for continuous growth', 0, 40)
        oled.show()
        time.sleep(1.5)
        oled.fill(0)
    elif 15<temp<25 and humidity<50 and light>5000:
        leds.uv()
        oled.text('Turn on UV Light', 0, 20)
        oled.text('Antioxidants Production', 0, 30)
        oled.text('Stress Resistance', 0, 40)
        oled.show()
        time.sleep(1.5)
        oled.fill(0)
    elif 15<temp<30 and 70<humidity<90 and light>2000:
        oled.text('No extra light', 0, 20)
        oled.show()
        time.sleep(1.5)
        oled.fill(0)
    else:
        npressbutton1 = button1.get_presses()
        npressbutton2 = button2.get_presses()
        if show_initial_message==0 or ledoff==1:
            oled.text('Click the button', 0, 20)
            oled.text('to find out', 0, 30)
            oled.text('which plant its:', 0, 40)
            oled.show()
            time.sleep(3)  # Espera para leer el botón
            oled.fill(0)
            show_initial_message = 1

        if npressbutton1 == 0:
            time.sleep(3)
            oled.fill(0)
        elif npressbutton1 == 1:
            while npressbutton2 == 0:
                oled.fill(0)
                leds.yellow()
                oled.text('Small Plant', 0, 0)
                oled.text('Turn on Yellow', 0, 10)
                oled.text('Promotes initial', 0, 20)
                oled.text('& control growth', 0, 30)
                oled.show()
                time.sleep(3)
                oled.fill(0)
                npressbutton2 = button2.get_presses()
            leds.off()
            ledoff=1
        elif npressbutton1 == 2:
            while npressbutton2 == 0:
                oled.fill(0)
                leds.green()
                oled.text('Medium Plant', 0, 0)
                oled.text('Turn on Green', 0, 10)
                oled.text('Supports lower', 0, 20)
                oled.text('plant parts', 0, 30)
                oled.show()
                time.sleep(3)
                oled.fill(0)
                npressbutton2 = button2.get_presses()
            leds.off()
            ledoff=1
        else:
            while npressbutton2 == 0:
                oled.fill(0)
                leds.red()
                oled.text('Big/Mature Plant', 0, 0)
                oled.text('Turn on Red', 0, 10)
                oled.text('Promotes flower', 0, 20)
                oled.text('Energy efficiency', 0, 30)
                oled.show()
                time.sleep(1.5)
                oled.fill(0)
                npressbutton2 = button2.get_presses()
            leds.off()
            ledoff=1