#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Configuración de la pantalla OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Pines de los LEDs
int boton = 2;

void setup() {
  // Inicializar comunicación serial
  Serial.begin(9600);

  // Inicializar la pantalla OLED
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("No se encontró pantalla OLED"));
    while (true);
  }
  oled.clearDisplay();
  
  // Configurar los pines de los LEDs como salida
  pinMode(boton, INPUT);

}

void loop() {
  int sensorValue = analogRead(A0); // Leer el valor del ADC (0 - 1023)
  float voltage = sensorValue * (3.3 / 1023.0); // Convertir a voltaje (máx 3.3V)

  // Mostrar en Serial Monitor
  Serial.print("Voltaje: ");
  Serial.println(voltage);

  // Mostrar en pantalla OLED
  if(digitalRead(boton) == LOW){
  oled.clearDisplay();
  oled.display();
}
else{
  oled.clearDisplay();
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
  oled.setCursor(0, 10);
  oled.print("V: ");
  oled.print(voltage);
  oled.display();
}
  delay(500); // Pequeño retardo para evitar lecturas inestables

}