# Write a micropython script to control the toggling rate of an led(connenected to gpio4)with respect of knob position of a potential meter (connenected to gpio34) of esp32
from machine import Pin, ADC
import time
# Define GPIO pins
led_pin = Pin(4, Pin.OUT)
potentiometer_pin = Pin(34)
# Initialize ADC for potentiometer
adc = ADC(pot_pin)
adc.atten(ADC.ATTN_11DB)  # Set attenuation level to 11dB for ESP32
# Function to map potentiometer values to toggling rates
def map_potentiometer_value(value, in_min, in_max, out_min, out_max):
    return (value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
while True:
        # Read potentiometer value
    pot_value = adc.read() 
    toggle_rate_ms = map_potentiometer_value(pot_value, 0, 4095, 100, 1000)                            
    led_pin.value(not led_pin.value())
    time.sleep_ms(toggle_rate_ms)
# from machine import Pin,ADC
# from time import sleep
# led_pin=Pin(4,Pin.OUT)
# pot_pin=ADC(Pin(34))
# pot.atten(ADC.ATTN_6DB)
# while True:
#     pot_value=pot.read()
#     pot_value=pot_value/1000;
#     print("Blinking Rate",pot_value)
#     led.on()
#     sleep(pot_value)
# else:
#     led.off()
#     sleep(1)