Light and Motion sensor control of Relay

Python code for MicroPython v1.19.1 using Thonny

# Light and Motion sensor control of Relay

from machine import ADC, Pin
from time import sleep
import tm1637
mydisplay = tm1637.TM1637(clk=Pin(26), dio=Pin(27))
mydisplay.brightness(0)

# Microcontrollerslab.com - LED button
# peppe8o.com/how-to-use-a-photoresistor-with-raspberry-pi-pico - light sensor

mot_delay  = 100. # tenths of a second (100 = 10 seconds)

0  # 600 twenths of a second = 2 min 
push_delay = 100 # 9000 tenths of a second = 10 min 
daylight = 48    # 16 Garage, 20 basement
loop_delay = 0.1
display_cnt = 0

# Light sensor pin
photoPIN = 28
def readLight(photoGP):
    photoRes = ADC(Pin(photoPIN))
    light = photoRes.read_u16()    
    light = int(light/65535*100)  
    return light

mydisplay.show("    ")
motion_sensor = Pin(22, Pin.IN)  # 13 number pin is input
push_button   = Pin(21, Pin.IN)  # 13 number pin is input
led_red       = Pin(16, Pin.OUT)    # 14 number in is Out
led_green     = Pin(17, Pin.OUT)    # 14 number in is Output
push_time = 0
mot_time  = 0
push_led  = 0
mot_led   = 0

sleep(.1)
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
 
while True:

    reading = sensor_temp.read_u16() * conversion_factor 
    celsius_degrees = 27 - (reading - 0.706)/0.001721
    fahrenheit_degrees = int(celsius_degrees * 9 / 5 + 32)
#    print (fahrenheit_degrees)

    push_logic_state = push_button.value()
    darkness = readLight(photoPIN)
    darkness_str = '%0*d' % (2, darkness)
    darkness_digit_str = '%0*d' % (1, darkness/10)
#    print(darkness_str)
    if push_logic_state == True:     # if push_button pressed
        sleep(.05)
        if push_time != 0:
            push_time = 0
        else:
            push_time = push_delay  

    mot_logic_state = motion_sensor.value()
    if mot_logic_state == True and darkness < daylight:     # if push_button pressed
        mot_time = mot_delay
    
    sleep(loop_delay) # set a delay between readings
    if push_time > 0:
        push_time = push_time - 1
        push_led = 1                 # led will turn ON
    else:                           # if push_button not pressed
        push_led = 0                 # led will turn OFF

    if mot_led == 1:
        led_red.value(1)            # led will turn ON
    else:                           # if push_button not pressed
        if push_led == 1:
            led_red.value(1)        # led will turn ON
        else:                       # if push_button not pressed
            led_red.value(0)             # led will turn OFF


    if mot_time > 0:
        mot_time = mot_time - 1
        mot_led = 1             # led will turn ON
    else:                       # if push_button not pressed
        mot_led = 0             # led will turn OFF


    if display_cnt > 19:
        display_str = darkness_str + str(int(mot_time/(mot_delay/10))) + str(int(push_time/(push_delay/10)))
        print("light: " + darkness_str +"%, button: " + str(push_logic_state) + ",  motion: " + str(mot_logic_state) + " push "+ str(push_time) + " mot_secs: "+ str(int(mot_time*loop_delay))+ " temp: "+ str(fahrenheit_degrees) + " display: "+display_str)
        display_cnt =0
        if mot_time == 0 and push_time == 0:
            mydisplay.show(darkness_digit_str +"-"+str(fahrenheit_degrees))
        else:        
            mydisplay.show(display_str)
    else:
        display_cnt = display_cnt + 1