#include <Adafruit_SSD1306.h> // Include the library for controlling the OLED display
#define BUTTON_PIN 3 // Define pin 3 as the pin connected to the button
#define SYMBOL "\x7F" // non-alphanumeric symbol to display is defined here
// Create an Adafruit_SSD1306 object to control the display
Adafruit_SSD1306 display(128, 32, &Wire, -1);
// a volatile bool variable is created to monitor whether the button has been pressed
volatile bool buttonPressed = false;
// when button is pressed interrupt is set to true
void buttonInterrupt() {
buttonPressed = true;
}
void setup() {
Serial.begin(9600); // serial communication is initialized
pinMode(BUTTON_PIN, INPUT_PULLUP); // the button pin is set as an input with a pull-up resistor
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonInterrupt, FALLING);
//attach the interrupt to the button pin
display.begin(SSD1306_SWITCHCAPVCC); // Initialize the OLED display
display.clearDisplay(); // Clear the display
}
void loop() {
if (buttonPressed) {
// this is to Check if the button has been pressed
display.clearDisplay();
// Clear the display
display.setTextSize(3);
//the text sixze and color is set
display.setTextColor(WHITE);
// Set the cursor position to be on top of the OLED display
display.setCursor(0, 0);
display.print(SYMBOL); // Display the non-alphanumeric symbol
display.display(); // Update the display
buttonPressed = false; // Reset the buttonPressed flag
}
}