#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 64 // OLED display height
#define OLED_RESET -1 // Reset pin (or -1 if sharing Arduino reset)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Pin definitions
#define ANALOG_PIN_1 A0 // Shared voltage for Pin 1
#define ANALOG_PIN_2 A1 // Shared voltage for Pin 2
#define LOW_LEVEL_PIN 6 // Digital pin for low-level input (Pin 5)
void setup() {
// Initialize the OLED display
if (!display.begin(SSD1306_PAGEADDR, 0x3C)) { // Address 0x3C for 128x64
Serial.begin(9600); // For debugging (optional)
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Halt program if initialization fails
}
// Set up pins
pinMode(ANALOG_PIN_1, INPUT);
pinMode(ANALOG_PIN_2, INPUT);
pinMode(LOW_LEVEL_PIN, INPUT_PULLUP); // Low-level input with pull-up resistor
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
// Call the loading screen
loadingScreen();
}
void loop() {
// Update the display with pin labels and values
displayPins();
delay(500); // Refresh every 500 ms
}
// Function to display a loading screen
void loadingScreen() {
display.clearDisplay();
display.setCursor((SCREEN_WIDTH - 60) / 2, SCREEN_HEIGHT / 2 - 8);
display.print(F("Loading..."));
// Draw a progress bar
int barWidth = 100;
int barHeight = 10;
int barX = (SCREEN_WIDTH - barWidth) / 2;
int barY = SCREEN_HEIGHT / 2 + 8;
for (int i = 0; i <= barWidth; i++) {
display.drawRect(barX, barY, barWidth, barHeight, SSD1306_WHITE); // Draw border
display.fillRect(barX, barY, i, barHeight, SSD1306_WHITE); // Fill progress
display.display();
delay(20);
}
delay(500);
}
// Function to display pin labels and values
void displayPins() {
display.clearDisplay();
// Top labels
display.setCursor(0, 0);
display.print(F("<----- POWER"));
display.setCursor(SCREEN_WIDTH - 36, 0);
display.print(F("----->"));
// Display pins for both sides
for (int i = 1; i <= 5; i++) {
displayPinInfo(i, 0, true); // Left side
displayPinInfo(i, SCREEN_WIDTH - 51, false); // Right side
}
display.display();
}
// Function to display a single pin's label and value
void displayPinInfo(int pinIndex, int x, bool isLeft) {
int y = 10 + (pinIndex - 1) * 9; // Calculate vertical position
float voltage;
// Handle different pins
switch (pinIndex) {
case 1: // Pin 1 - Voltage from A0
voltage = analogRead(ANALOG_PIN_1) * (5.0 / 1023.0);
display.setCursor(x, y);
display.print(pinIndex);
display.print(F("-"));
display.setCursor(x + 15, y);
display.print(voltage * 5, 2); // Multiply by 5 if using a voltage divider for 24V
display.print(F("V"));
break;
case 2: // Pin 2 - Voltage from A1
voltage = analogRead(ANALOG_PIN_2) * (5.0 / 1023.0);
display.setCursor(x, y);
display.print(pinIndex);
display.print(F("-"));
display.setCursor(x + 15, y);
display.print(voltage * 5, 2); // Multiply by 5 if using a voltage divider for 24V
display.print(F("V"));
break;
case 3: // Pin 3 - GROUND
case 4: // Pin 4 - GROUND
display.setCursor(x, y);
display.print(pinIndex);
display.print(F("-"));
display.setCursor(x + 15, y);
display.print(F("GROUND"));
break;
case 5: // Pin 5 - Low-Level Input
display.setCursor(x, y);
display.print(pinIndex);
display.print(F("-"));
display.setCursor(x + 15, y);
if (digitalRead(LOW_LEVEL_PIN) == LOW) {
display.print(F("LOW"));
} else {
display.print(F("HIGH"));
}
break;
}
}