#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include <TinyDebug.h>
#include "HUNTER_font.h"
#include "bubblesstandard_font.h"


#define ADC_PIN A2 // Analog pin for voltage measurement
#define VCC_REF 5.0 // Voltage reference of ATtiny85 (5V or calibrated value)
#define ADC_RESOLUTION 1024.0 // 10-bit ADC resolution

uint8_t width = 128;
uint8_t height = 64;

void setup() {
  Debug.begin();
  // Initialize OLED
  oled.begin(width, height, sizeof(tiny4koled_init_128x64r), tiny4koled_init_128x64r);

  // Two fonts are supplied with this library, FONT8X16 and FONT6X8
  // Other fonts are available from the TinyOLED-Fonts library
  // oled.setFont(FONTHUNTER);
  oled.setFont(FONT8X16);
  
  oled.clear(); // Clear the memory before turning on the display
  oled.on(); // Turn on the display
  
  // Display initialization message
  oled.setCursor(0, 0);
  oled.print(F("Voltage Meter"));
  delay(2000);
  oled.clear();

  pinMode(ADC_PIN, INPUT); // Configure ADC pin

}



void loop() {
  float voltage = readVoltage();
  displayVoltage(voltage);
  delay(500); // Update every .5 second
}

// - - - - - FUNCTIONS - - - - -

// Take multiple ADC readings for stability
float readVoltage() {
  uint16_t adcValue = 0;
  for (uint8_t i = 0; i < 10; i++) {
    adcValue += analogRead(ADC_PIN);
  }
  adcValue /= 10;

  // Convert ADC reading to voltage
  float voltage = (adcValue * VCC_REF) / ADC_RESOLUTION;
  return voltage;
}

void displayVoltage(float voltage) {
  // oled.clear();
  oled.setCursor(0, 0);
  oled.print("Voltage:");
  oled.setCursor(0, 2);
  oled.print(voltage, 2); // Print voltage with 2 decimal places
  oled.print(" V");
  oled.setCursor(0, 4);
  oled.print("Line 4");
  oled.setCursor(0, 6);
  oled.print("Line 6");
}
ATTINY8520PU
Loading
ssd1306