#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int b1 = 3; // button 1
int b2 = 4; // button 2
const int debounceDelay = 50; // Debounce delay (ms))
unsigned long lastDebounceTime1 = 0; // Last press time of button 1
unsigned long lastDebounceTime2 = 0; // Last press time of button 2
bool b1State = false; // Current status of button 1 (is it pressed?)
bool b2State = false; // Current status of button 2 (is it pressed?)
unsigned long displayEndTime1 = 0; // Video end time for button 1
unsigned long displayEndTime2 = 0; // Video end time for button 2
void setup() {
Serial.begin(115200);
// OLED initialization
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 initialization failed"));
for (;;); // 初期化失敗時は停止
}
display.clearDisplay();
display.display();
// Set button pin as input
pinMode(b1, INPUT_PULLUP);
pinMode(b2, INPUT_PULLUP);
}
void loop() {
unsigned long currentMillis = millis();
// Check button 1
bool reading1 = digitalRead(b1) == LOW; // Button 2 is pressed?
if (reading1 && !b1State && (currentMillis - lastDebounceTime1 > debounceDelay)) {
lastDebounceTime1 = currentMillis;
b1State = true; // Record that button 1 was pressed
// Displays video when button 1 is pressed
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(3);
display.setCursor(0, 0);
display.println("Made By Ryohga");
display.display();
// Set the end time of the video (after 1 seconds)
displayEndTime1 = currentMillis + 1000;
} else if (!reading1) {
b1State = false; // Record that button 1 was released.
}
// Erase the image on button 1
if (displayEndTime1 > 0 && currentMillis > displayEndTime1) {
display.clearDisplay();
display.display();
displayEndTime1 = 0; // reset
}
// Check button 2
bool reading2 = digitalRead(b2) == LOW; //Button 2 is pressed?
if (reading2 && !b2State && (currentMillis - lastDebounceTime2 > debounceDelay)) {
lastDebounceTime2 = currentMillis;
b2State = true; // Record that button 2 was pressed.
// Displays video when button 2 is pressed
display.clearDisplay();
display.fillCircle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 10, WHITE);
display.display();
// Set the end time of the video (after 1 seconds)
displayEndTime2 = currentMillis + 1000;
} else if (!reading2) {
b2State = false; // Record that button 2 was released.
}
// Erase the image on button 2
if (displayEndTime2 > 0 && currentMillis > displayEndTime2) {
display.clearDisplay();
display.display();
displayEndTime2 = 0; // reset
}
}