OLED DC Voltmeter

Hi
This is a DC voltmeter program with a progress bar with 0 on the left. How do I modify it to have a voltmeter with 0 (zero) in center. Actually 0 corresponds to 1/2 Vcc = 3.3/2 = 1.65 V (stm32f103), so when the voltage is above 1.65 V the bar increases from the center to the right, when it is lower than 1.65 V, increasing from center to left.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
//``````Adafruit_SSD1306 display(OLED_RESET);

TwoWire Wire2 (2, I2C_FAST_MODE); // PB10, PB11
#define Wire Wire2
#define OLED_RESET     4
Adafruit_SSD1306 display(128, 32, &Wire, 4);

void setup() {

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  Serial.begin(115200);
}
void loop() {
  //-------------------------  bar --------------------------------
  // Clear the buffer.
  display.clearDisplay();
  ///////////
  display.setCursor(60, 0);
  display.print("I");
  //////////

  drawPercentbar( 0, 20, 128, 15, analogRead(PA5) / 40);
  display.display();
  delay(100);
}

void drawPercentbar(int x, int y, int width, int height, int progress)
{

  progress = progress < 0 ? 0 : progress;
  //float bar = ((float)(width - 4) / 100) * progress;
  float bar = ((float)(width - 4) / 105) * progress;

  display.fillRect(x + 2, y + 2, bar , height - 4, WHITE);

  // Display progress text
  if ( height >= 10) {
    //display.setCursor((width / 2) - 3, y + 5 );
    display.setCursor((width / 2) + 20, 0);
    display.setTextSize(2);
    display.setTextColor(WHITE);

    if ( progress >= 50)
      display.setTextColor(BLACK, WHITE); // 'inverted' text
    // display.print((progress) * 3.3, 0);
    display.print((progress) * 22.0, 0);

  }
  // ---------------------------bar end ------------------------
}

how can the analog input value be < 0? and why divide by 40?

if it is an 12-bit (4096) analog-to-digital converter (datasheet pg 20), why not divide by 4096 to make the value from 0-1 or 100/4096 to make it 0-100?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.