from machine import Pin,ADC,Timer,PWM
from machine import Pin,Timer
import time
ldr1=ADC(Pin(25,Pin.OUT))
ldr2=ADC(Pin(26,Pin.OUT))
ldr3=ADC(Pin(27,Pin.OUT))
pir1=Pin(33,Pin.OUT,Pin.PULL_UP)
R=PWM(Pin(17),freq=900,duty=0)
G=PWM(Pin(16),freq=900,duty=0)
B=PWM(Pin(4),freq=900,duty=0)
Servo=PWM(Pin(5),freq=50,duty=0)
running=False
brightness=False
GAMMA = 0.7
RL10 = 50
def ldr_adc(sens):
global GAMMA
global RL10
analogValue = sens.read()
voltage = analogValue / 4096. * 5
resistance = 2000 * voltage / (1 - voltage / 5)
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA))
return lux
def degree_read(sens):
tmp1=sens.read()
degree=int((tmp1/4096)*180)
return degree
while 1:
tmp=ldr_adc(ldr2)
if tmp<1000:
brightness=True
if pir1.value()==0 and brightness==True:
R.duty(1023)
G.duty(0)
B.duty(0)
Servo.duty(int((degree_read(ldr1)/90+0.5)/20*1023))
print (degree_read(ldr1))
elif pir1.value()==1 or brightness==False:
R.duty(1023)
G.duty(1023)
B.duty(0)
else :
R.duty(1023)
G.duty(1023)
B.duty(0)
# Servo.duty(int(ldr1.read()/4))
# ldr1.read()/4 #0-1023
# (ldr1.read()/4096)*180 #0-180
# Servo.duty(int(((ldr1.read()/4)/4/90+0.5)/20*1023)) #((x/90+0.5)/20*1023)/4))
# def ldr_ctrl(sens,element):
# tim=Timer(0)
# tim.init(period=250,mode = Timer.PERIODIC,callback=ldr1_ctrl)