from machine import SPI, ADC, Pin
import time
from utime import sleep
import math
print(1)
light = ADC(Pin(26))
iso = 90
gamma = 0.7
rl10 = 50
max_value = 65535
f_stop = 8.0
print_button = machine.Pin(17, machine.Pin.IN, machine.Pin.PULL_UP)
read_button = machine.Pin(27, machine.Pin.IN, machine.Pin.PULL_UP)
serial = SPI(0, 10000000, sck=Pin(2,Pin.OUT), mosi=Pin(3,Pin.OUT))
latch = Pin(1, Pin.OUT)
# gradeColors = [ {'R': 1, 'G': 0.97, 'B': 0.325, 'Grade': '00'}, {'R': 1, 'G': 0.94, 'B': 0.475, 'Grade': '0'}, {'R': 1, 'G': 0.945, 'B': 0.615, 'Grade': '0.5'}, {'R': 1, 'G': 0.915, 'B': 0.665, 'Grade': '1'}, {'R': 1, 'G': 0.86, 'B': 0.74, 'Grade': '1.5'}, {'R': 1, 'G': 0.785, 'B': 0.805, 'Grade': '2'}, {'R': 1, 'G': 0.745, 'B': 0.84, 'Grade': '2.5'}, {'R': 1, 'G': 0.69, 'B': 0.885, 'Grade': '3'}, {'R': 1, 'G': 0.605, 'B': 0.93, 'Grade': '3.5'}, {'R': 1, 'G': 0.525, 'B': 0.95, 'Grade': '4'}, {'R': 1, 'G': 0.23, 'B': 0.925, 'Grade': '4.5'}, {'R': 1, 'G': 0, 'B': 1, 'Grade': '5'} ]
def calculate_lux(analogValue):
voltage = analogValue / max_value * 5
resistance = 2000 * voltage / (1 - voltage / 5)
return math.pow(rl10 * 1000 * math.pow(10, gamma) / resistance, (1 / gamma))
def calculate_ev_iso_100(lux):
return math.log2(lux / 2.5)
def iso_stops(iso):
return math.log2(iso/100)
def ev_to_seconds(ev):
return (100 * f_stop**2)/(100 * 2**ev)
led = Pin(9, Pin.OUT)
highlight_value = 7
middle_value = 5
seconds = 0
while True:
if read_button.value() == 0:
analogValue = light.read_u16()
lux = calculate_lux(analogValue)
ev_iso_100 = calculate_ev_iso_100(lux)
stops_off = iso_stops(iso)
seconds = ev_to_seconds(ev_iso_100 + stops_off + (highlight_value - middle_value))
print(lux)
print(seconds)
sleep(0.1)
if print_button.value() == 0:
led.on()
sleep(seconds)
led.off()
latch.off()
serial.write(b"0b10110111")
latch.on()
sleep(0.5)