#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // See datasheet for Address; 0x3D for 128x64, 0x3C for others
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int numSensors = 12; // Total number of sensors
const int sensorPins[numSensors] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11}; // Sensor pins
double voltages[numSensors]; // To store voltage readings
const int Sensors_max = 2500; // Maximum value after mapping
const int offset = 20; // Correction offset
void setup() {
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
}
void loop() {
readAndConvertVoltages();
displayVoltagesOnOLED();
delay(100); // Delay before the next loop iteration
}
void readAndConvertVoltages() {
for (int i = 0; i < numSensors; i++) {
int readValue = analogRead(sensorPins[i]);
// Convert and map 0-1023 to 0-Sensors_max, then adjust with offset and convert to volts
voltages[i] = (map(readValue, 0, 1023, 0, Sensors_max) + offset) / 100.0;
}
}
void displayVoltagesOnOLED() {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
int colWidth = SCREEN_WIDTH / 2; // Two columns
int rowHeight = SCREEN_HEIGHT / 6; // Six rows
for (int i = 0; i < numSensors; i++) {
int col = i / 6; // 0 for the first six sensors, 1 for the second set of six
int row = i % 6; // Row number within each column
int x = col * colWidth;
int y = row * rowHeight;
display.setCursor(x, y);
// Using 'A' + i to get the ASCII character after 'A' for each sensor
display.print(char('A' + i));
display.print(": ");
display.print(voltages[i], 2);
display.print("V "); // Added space and unit
}
display.display(); // Actually draw everything to the screen
}