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 ------------------------
}