# 6.8 Project 5: Frequency generator with LCD and potentiometer
# Matheus Manfio - 30/11/23
# -----------------------------------------------------------
from machine import I2C, Pin, PWM, ADC
from time import sleep
import utime
from x import I2cLcd
i2c_lcd = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c_lcd, 0x27, 2, 16)
Pot = ADC(0) # Pot at channel 0
ch = PWM(Pin(16)) # PWM at GP16
ch.freq(100) # Default freq
lcd.clear()
while True:
lcd.move_to(0, 0)
lcd.putstr("Frequency(Hz)")
frequency = Pot.read_u16() # Read pot data
lcd.move_to(0, 1)
lcd.putstr(str(frequency))
ch.duty_u16(32767)
ch.freq(frequency) # Change the freuency
utime.sleep(1)
lcd.clear()