#include <SPI.h>
#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
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BLINK_INTERVAL 1000 // Blink interval in milliseconds
// Button pin
#define BUTTON_PIN 2
// States for eye blinking
enum EyeState {
OPEN,
CLOSED
};
EyeState eyeState = OPEN;
unsigned long lastBlinkTime = 500;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Initialize the OLED object
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the buffer.
display.clearDisplay();
display.display();
delay(2000);
}
void loop() {
// Check if it's time to blink the eyes
if (millis() - lastBlinkTime >= BLINK_INTERVAL) {
if (eyeState == OPEN) {
// Close eyes
display.clearDisplay();
display.fillRect(20, 22, 31, 10, WHITE);
display.fillRect(75, 22, 31, 10, WHITE);
display.display();
eyeState = CLOSED;
} else {
// Open eyes
display.clearDisplay();
display.fillRect(20, 14, 31, 24, WHITE);
display.fillRect(75, 14, 31, 24, WHITE);
display.display();
eyeState = OPEN;
}
lastBlinkTime = millis();
}
// Check button press to trigger happy face
if (digitalRead(BUTTON_PIN) == LOW) {
display.clearDisplay();
// Draw happy face or any other pattern you want
// Example: display.drawCircle(64, 32, 30, WHITE);
// Example: display.fillCircle(48, 32, 10, WHITE);
// Example: display.fillCircle(80, 32, 10, WHITE);
// Example: display.drawPixel(58, 40, WHITE);
// Example: display.drawPixel(70, 40, WHITE);
// Example: display.drawLine(54, 46, 74, 46, WHITE);
display.display();
delay(2000); // Display happy face for 2 seconds
// After displaying, clear the display to return to normal
display.clearDisplay();
lastBlinkTime = millis(); // Reset blink timer to maintain blinking pattern
}
}