#include <U8g2lib.h>
// Create an array of U8G2 objects to manage multiple screens
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2Screens[2] = {
U8G2_SSD1306_128X64_NONAME_F_SW_I2C(U8G2_R0, /* clock=*/ A3, /* data=*/ A2, /* reset=*/ U8X8_PIN_NONE),
U8G2_SSD1306_128X64_NONAME_F_SW_I2C(U8G2_R0, /* clock=*/ A5, /* data=*/ A4, /* reset=*/ U8X8_PIN_NONE)
};
const int numScreens = 2;
const int numSegments = 7;
void setup() {
for (int i = 0; i < numScreens; i++) {
u8g2Screens[i].begin();
u8g2Screens[i].setFont(u8g2_font_profont22_tf);
}
}
void drawGauge(U8G2 &u8g2, float gaugePosition, float barReading, int screenNumber) {
u8g2.drawFrame(0, 5, 128, 10);
// Invert the gauge's left-to-right direction for screen 2
int invertedPosition = (screenNumber == 1) ? 128 - gaugePosition : gaugePosition;
u8g2.drawBox(0, 5, invertedPosition, 10);
int segmentWidth = 128 / numSegments;
for (int i = 1; i < numSegments; i++) {
int x = i * segmentWidth;
u8g2.drawVLine(x, 0, 5);
u8g2.drawVLine(x, 15, 5);
}
int startX = 128 - (segmentWidth * 2);
int startY = 5;
for (int x = startX; x < 128; x++) {
for (int y = startY; y < 15; y++) {
if (((x - startX) + (y - startY)) % 2 == 0) {
u8g2.drawPixel(x, y);
}
}
}
u8g2.setFont(u8g2_font_profont22_tf);
u8g2.setCursor(30, 58);
u8g2.print(barReading, 2);
}
void loop() {
for (int i = 0; i < numScreens; i++) {
u8g2Screens[i].firstPage();
do {
int sensorValue = (i == 0) ? analogRead(A0) : analogRead(A1);
float gaugePosition = map(sensorValue, 0, 1023, 0, 128);
float barReading = (i == 0) ? -1.0 + (gaugePosition / 128.0) * 3.5 : (gaugePosition / 128.0) * 5.0;
drawGauge(u8g2Screens[i], gaugePosition, barReading, i);
} while (u8g2Screens[i].nextPage());
}
delay(50);
}