#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display parameters
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Pin definitions for buttons
const int buttonModePin = 2; // Pin for mode selection button
const int buttonUpPin = 3; // Pin for range up button
const int buttonDownPin = 4; // Pin for range down button
// Variables to store button states
volatile bool modeChanged = false;
volatile bool upPressed = false;
volatile bool downPressed = false;
int mode = 0; // 0 for capacitance, 1 for inductance
int capacitanceRange = 0;
int inductanceRange = 0;
int currentRange = 0; // Holds current range (based on mode)
// Range descriptions
const char* capacitanceRanges[4] = {"1-999 pF", "1-999 nF", "1-999 uF", "1-10 mF"};
const char* inductanceRanges[3] = {"1-999 uH", "1-999 mH", "1-10 H"};
void setup() {
// Initialize serial communication (for debugging purposes)
Serial.begin(9600);
// Initialize buttons
pinMode(buttonModePin, INPUT_PULLUP); // Mode button with internal pull-up resistor
pinMode(buttonUpPin, INPUT_PULLUP); // Up button with internal pull-up resistor
pinMode(buttonDownPin, INPUT_PULLUP); // Down button with internal pull-up resistor
// Attach interrupts for button presses
attachInterrupt(digitalPinToInterrupt(buttonModePin), modeButtonPressed, FALLING);
attachInterrupt(digitalPinToInterrupt(buttonUpPin), upButtonPressed, FALLING);
attachInterrupt(digitalPinToInterrupt(buttonDownPin), downButtonPressed, FALLING);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
updateDisplay();
}
void loop() {
// Handle mode change
if (modeChanged) {
modeChanged = false;
mode = !mode; // Toggle mode (0 for capacitance, 1 for inductance)
currentRange = (mode == 0) ? capacitanceRange : inductanceRange; // Set range for the new mode
updateDisplay();
}
// Handle range increase
if (upPressed) {
upPressed = false;
if (mode == 0) {
capacitanceRange = (capacitanceRange + 1) % 4; // 4 ranges for capacitance (0-3)
currentRange = capacitanceRange;
} else {
inductanceRange = (inductanceRange + 1) % 3; // 3 ranges for inductance (0-2)
currentRange = inductanceRange;
}
updateDisplay();
}
// Handle range decrease
if (downPressed) {
downPressed = false;
if (mode == 0) {
capacitanceRange = (capacitanceRange - 1 + 4 ) % 4; // Ensure range wraps around (0-3)
currentRange = capacitanceRange;
} else {
inductanceRange = (inductanceRange - 1 + 4 ) % 3; // Ensure range wraps around (0-2)
currentRange = inductanceRange;
}
updateDisplay();
}
}
// Interrupt service routine for mode button
void modeButtonPressed() {
modeChanged = true;
}
// Interrupt service routine for up button
void upButtonPressed() {
upPressed = true;
}
// Interrupt service routine for down button
void downButtonPressed() {
downPressed = true;
}
// Function to update the OLED display
void updateDisplay() {
display.clearDisplay();
// Display mode
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
if (mode == 0) {
display.setCursor(0, 0);
display.println(F("Capacitance"));
} else {
display.setCursor(0, 0);
display.println(F("Inductance"));
}
// Display current range
display.setTextSize(1);
display.setCursor(0, 40);
display.print(F("Range: "));
if (mode == 0) {
display.println(capacitanceRanges[currentRange]);
} else {
display.println(inductanceRanges[currentRange]);
}
display.display();
}