/*
    Voltmeter with Arduino by Steve Barth
*/ 

#include <SPI.h>
#include <Wire.h>
//#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold9pt7b.h>

#define OLED_RESET 4
Adafruit_SSD1306 oled(128,64,&Wire,OLED_RESET);

#define v_ref 1.1  // external reference voltage LM4040 2.048V (2.048V / 1024 = 0,002V)

void setup() {
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3D); // Initialize with the I2C addr 0x3D if not working use 0x3C (for the 128x64)
  oled.setTextColor(WHITE);
  delay(1); 
}

void loop(){
  oled.clearDisplay();
  oled.setTextSize(1);
  oled.setCursor(12,5);
  oled.print("Digital Voltmeter");
  oled.setCursor(17,20);
  oled.print("Analog input: A1");
  oled.setCursor(24,47); // print out the value you read:
  oled.setFont(&FreeSansBold9pt7b);
  int V_input=analogRead(1); // measurment voltage A1 input reading
  float voltage = (V_input * v_ref) / 102.3; // converting that reading to voltage, which is based off the reference voltage
  char string[8];                                   // string max 6 digit (0000.0)
  dtostrf(voltage * 100, 6, 1, string);            // convert data to string (0000), dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER)
  oled.print(string);                               // screen data
  oled.println(" V");
  oled.display();
  oled.setFont(); // set font default

}