#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
#include "hardware/uart.h"
#define LED_PIN 15
#define TX_PIN 0
#define RX_PIN 1
#define ADC_PIN 26
#define UART_ID 0
static const uint32_t BAUD_RATE = 9600;
int main() {
    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
    // Configure UART
    uart_init(UART_ID, BAUD_RATE);
    gpio_set_function(TX_PIN, GPIO_FUNC_UART);
    gpio_set_function(RX_PIN, GPIO_FUNC_UART);
    // Configure ADC
    adc_init();
    adc_gpio_init(ADC_PIN);
    adc_select_input(ADC_PIN);
    float voltage;
    char buffer[20];
    while (true) {
        voltage = adc_read() * (3.3f / (float) ADC_MAX_READING); // Scaling formula
        sprintf(buffer, "Voltage: %.2f V", voltage);
        uart_puts(UART_ID, buffer);
        uart_putc(UART_ID, '\n');
        sleep_ms(1000);
        // Check for user input
        if (uart_is_readable(UART_ID)) {
            char c = uart_getc(UART_ID);
            if (c == 'a') {
                gpio_put(LED_PIN, 1);
            } else if (c == 's') {
                gpio_put(LED_PIN, 0);
            }
        }
    }
    return 0;
}