//Eye Blinking//
#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 an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Eye dimensions
const int eyeWidth = 40;
const int eyeHeight = 45;
const int eyeGap = 10;
const int eyeRadius = 10; // Radius for rounded corners
// Eye positions
const int leftEyeX = (SCREEN_WIDTH - eyeWidth * 2 - eyeGap) / 2;
const int rightEyeX = leftEyeX + eyeWidth + eyeGap;
const int eyeY = (SCREEN_HEIGHT - eyeHeight) / 2;
// Eye timing limits (in milliseconds)
const unsigned long minBlinkDelay = 1500; // Minimum blink delay (1.5 second)
const unsigned long maxBlinkDelay = 2000; // Maximum blink delay (2 seconds)
const unsigned long blinkTime = 200; // Blink time (0.2 seconds)
const int numDelays = 10; // Number of different delays
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
randomSeed(analogRead(0)); // Seed the random number generator
}
void drawEyes(bool open) {
display.clearDisplay();
if (open) {
// Draw open eyes with rounded corners
display.fillRoundRect(leftEyeX, eyeY, eyeWidth, eyeHeight, eyeRadius, WHITE);
display.fillRoundRect(rightEyeX, eyeY, eyeWidth, eyeHeight, eyeRadius, WHITE);
} else {
// Draw closed eyes (eyelids) with rounded corners
display.fillRoundRect(leftEyeX, eyeY + eyeHeight / 2 - 5, eyeWidth, 10, eyeRadius, WHITE);
display.fillRoundRect(rightEyeX, eyeY + eyeHeight / 2 - 5, eyeWidth, 10, eyeRadius, WHITE);
}
display.display();
}
void loop() {
drawEyes(true); // Open eyes
unsigned long blinkDelay = random(minBlinkDelay, maxBlinkDelay + 1); // Random blink delay between 1 and 2 seconds
delay(blinkDelay); // Delay for the open eye state
drawEyes(false); // Close eyes (blink)
delay(blinkTime); // Fixed delay for blink (0.2 seconds)
for (int i = 0; i < numDelays; i++) {
drawEyes(true); // Open eyes
blinkDelay = map(i, 0, numDelays - 1, minBlinkDelay, maxBlinkDelay); // Map the current index to a delay value between minBlinkDelay and maxBlinkDelay
delay(blinkDelay); // Delay for the open eye state
drawEyes(false); // Close eyes (blink)
delay(blinkTime); // Fixed delay for blink (0.2 seconds)
}
}