#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// constants won't change
const int buttonPin = 18; // ESP32connected to button's pin
const int LEDPin = 2; // ESP32 pin connected to LED's pin
// variables will change:
int ledState = LOW; // the current state of LED
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
Serial.begin(9600); // initialize serial
pinMode(buttonPin, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
pinMode(LEDPin, OUTPUT); // set ESP32 pin to output mode
currentButtonState = digitalRead(buttonPin);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("INTI"); // set text
oled.setTextSize(1); // set text size
oled.println("");
oled.println("International"); // set text
oled.println("");
oled.println("University"); // set text
oled.display(); // display on OLED
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(buttonPin); // read new state
// Delay for stability
delay(100);
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("Button is pressed");
//Display in the screen
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(4, 2); // set position to display (x,y)
oled.println("Button is pressed"); // set text
oled.display(); // display on OLED
// toggle state of LED
ledState = !ledState;
// control LED arccoding to the toggled state
digitalWrite(LEDPin, ledState);
//delay for showing buttin is pressed
delay(1000);
oled.clearDisplay(); // clear display
oled.setTextSize(2); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("INTI"); // set text
oled.setTextSize(1); // set text size
oled.println("");
oled.println("International"); // set text
oled.println("");
oled.println("University"); // set text
oled.display();
}
delay(100);
}