from machine import Pin, PWM, ADC

adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_10BIT)
pwm0 = PWM(Pin(2))

def values(frequency, duty):

  pulse_result = "The frequency is {:4d} Hz with a duty cycle of {:3d} %"

  print(pulse_result.format(frequency, duty))

def pulse(frequency):

  pwm0.freq(frequency)

def LED(volts):

  if volts > 0 and volts < 1:
    
    frequency = 2000
    pulse(frequency)
    pwm0.duty(255)
    duty = 25
    #values(frequency, duty)
  
  elif volts > 1 and volts < 2:

    frequency = 5000
    pulse(frequency)
    pwm0.duty(767)
    duty = 75
    #values(frequency, duty)
  
  elif volts > 2 and volts < 3:

    frequency = 60000
    pulse(frequency)
    pwm0.duty(511)
    duty = 50
    #values(frequency, duty)
  
  elif volts > 3:

    frequency = 5
    pulse(frequency)
    pwm0.duty(205)
    duty = 20
    #values(frequency, duty)

while True:

  adc_value = adc.read()
  
  volts = (adc_value*3.3)/1023

  voltage_result = "The input voltage is {:0.2f} volts "

  print(voltage_result.format(volts), end = "\r")

  LED(volts)