from machine import I2C, Pin, ADC
from i2c_lcd import I2cLcd
from utime import sleep,sleep_ms
DEFAULT_I2C_ADDR = 0x27 # LCD 1602 I2C address
i2c = I2C(0,sda=Pin(0),scl=Pin(1),freq=400000)
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR,2, 16) # Initialize(device address, backlight settings)
Potentiometer=ADC(0)
maxSleep=2
noofSpace=3 #no. of spaces between text
txt='Pico LCD'
space=" "*noofSpace
txt=txt+space
tmp=txt
cPtr=0 #character pointer
sPtr=0 #screen pointer
fcPtr=0 #character pointer at the first digit of the LCD
dire=1 #direction indicator 1=scroll to left, -1=scroll to right, 0=still
adcR=0 #analog to digital converter reading min=0 to max=65535
sec=1 #maximum seconds
while True:
adcR=Potentiometer.read_u16()
if adcR<20000: #define direction and seconds according to the reading from the potentiometer (0 to 20000)
dire=1
sec=maxSleep*adcR/20000 #relation between sleep and potentiometer input are calculated by slope equation
elif adcR>40000: #define direction and seconds according to the reading from the potentiometer (40000 to 65535)
dire=-1
sec=-(maxSleep*adcR/(65535-40000))+((1+(40000/(65535-40000)))*maxSleep)+0.1 #slope equation
else: #stop region (20000 to 40000)
dire=0
if abs(cPtr)>=len(txt): #character looping after last character
cPtr=0
if sPtr==0: #store the pointing number of the character at the first position of the LCD
fcPtr=cPtr
tmp=txt[cPtr]
lcd.move_to(sPtr,0)
lcd.putstr(tmp) #put character on the LCD one by one
sPtr=sPtr+1
cPtr=cPtr+1
if sPtr>=16: #the pointer at the LCD display returns to the first position after the last position
sPtr=0
cPtr=fcPtr+dire
sleep(sec)
lcd.clear()