from machine import Pin, I2C, ADC, Timer
from i2c_lcd import I2cLcd
from time import sleep
import random
sda=Pin(0)
scl=Pin(1)
adc_pot = ADC(26)
i2c=I2C(0,sda=sda,scl=scl, freq=400000)
devices=i2c.scan()
idcaddr=devices[0]
lcd=I2cLcd(i2c,idcaddr,2,16)
def update_lcd(timer):
lcd.clear()
adc_pot_value = adc_pot.read_u16()
volt = round((3.3/65535)*adc_pot_value,2)
'percent = int(adc_pot_value/65535*90)'
percent = 90 - int(adc_pot_value / 65535 * 90)
print("Volt: "+str(volt)+" | Read value: "+str(adc_pot_value)+" | percent: "+str(percent)+"%")
gate_opened=(percent)
print("Gate opened by:"+str(gate_opened))
lcd.putstr(str(gate_opened))
sleep(1)
timer = Timer()
timer.init(period=1000, mode=Timer.PERIODIC, callback=update_lcd)