/*
    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.setCursor(12,5);
  oled.print("Digital Voltmeter");

  oled.setCursor(5,25);
  oled.print("A1:");
  
  oled.setCursor(5,46);
  oled.print("A2:");

  oled.setCursor(35,27);    // print out the value you read:
  oled.setFont(&FreeSansBold9pt7b);
  int V_input1=analogRead(A1);    // measurment voltage A1 input reading
  float voltage1 = (V_input1 * v_ref) / 1.023;   // converting that reading to voltage, which is based off the reference voltage
  oled.print(voltage1); 
  oled.println(" V");

  oled.setCursor(35,54);    // print out the value you read:
  oled.setFont(&FreeSansBold9pt7b);
  int V_input2=analogRead(A2);    // measurment voltage A1 input reading
  float voltage2 = (V_input2 * v_ref) / 1023;   // converting that reading to voltage, which is based off the reference voltage
  oled.print(voltage2*1000); 
  oled.println(" V");

  oled.display();
  oled.setFont();   // set font default

}