#include <Arduino.h>
// Include the correct library for OLED display (U8g2 for Wokwi)
#include <u8g2.h>
#define sensorPin 34 // Analog pin for voltage sensor
const float maxVoltage = 28.8;
const float minVoltage = 23.9;
float adc_voltage = 0.0;
float in_voltage = 0.0;
float R1 = 30000.0 + 412912.91;
float R2 = 7500.0;
float ref_voltage = 3.3; // ESP-Wroom-32 ADC reference voltage
int adc_value = 0;
u8g2 display(128, 64, U8X8_SCA, U8X8_SCL); // Replace pins if needed
class RGBLed {
public:
RGBLed(int redPin, int greenPin, int bluePin) :
_redpin(redPin), _greenpin(greenPin), _bluepin(bluePin) {}
void write(float red, float green, float blue) {
float mappedRed = map(red, 0.0, 1.0, 0.0, 255);
float mappedGreen = map(green, 0.0, 1.0, 0.0, 255);
float mappedBlue = map(blue, 0.0, 1.0, 0.0, 255);
analogWrite(_redpin, mappedRed);
analogWrite(_greenpin, mappedGreen);
analogWrite(_bluepin, mappedBlue);
}
private:
int _redpin;
int _greenpin;
int _bluepin;
};
// Replace with actual ESP32 GPIO pins for RGB LED connections
RGBLed myRGBled(18, 19, 23);
void setup() {
Serial.begin(9600);
// Initialize U8g2 display
delay(1000);;
display.setFont(u8g2_font_proportional_4x6); // Set font
display.clearDisplay();
display.setCursor(0, 0);
display.println("Voltage:");
// Update display (U8g2 specific function)
display.sendBuffer();
}
void loop() {
adc_value = analogRead(sensorPin);
adc_voltage = (adc_value * ref_voltage) / 4095.0; // ESP32 ADC has 12 bits
in_voltage = adc_voltage / (R2 / (R1 + R2));
float percentage = map(in_voltage, minVoltage, maxVoltage, 0, 100);
// ... rest of your code for RGB LED control
display.setCursor(0, 10);
display.println(in_voltage, 2); // Display voltage with 2 decimal places
display.sendBuffer(); // Update display
delay(1000);
}