#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 an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define the LED pins
const int RED = 32;
const int BLUE = 33;
const int GREEN = 27;
const int buttonPin1 = 16;
const int buttonPin2 = 17;
const int buttonPin3 = 18;
int lastButtonState1 = LOW;
int lastButtonState2 = LOW;
int lastButtonState3 = LOW;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long debounceDelay = 300;
// Variables to hold the current time and the last time the LED state was changed
unsigned long previousMillisRED = 0;
unsigned long previousMillisBLUE = 0;
unsigned long previousMillisGREEN = 0;
unsigned long intervalRED = 1000; // Interval at which to blink RED LED (milliseconds)
unsigned long intervalBLUE = 2000; // Interval at which to blink BLUE LED (milliseconds)
unsigned long intervalGREEN = 3000; // Interval at which to blink GREEN LED (milliseconds)
void setup() {
// Set the LED pins as output
pinMode(RED, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(buttonPin1, INPUT_PULLDOWN);
pinMode(buttonPin2, INPUT_PULLDOWN);
pinMode(buttonPin3, INPUT_PULLDOWN);
// Start the serial communication
Serial.begin(115200);
// 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
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Hello, world!");
display.display();
}
void loop() {
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
// Check if it's time to blink the RED LED
if (millis() - previousMillisRED > intervalRED) {
previousMillisRED = millis();
digitalWrite(RED, !digitalRead(RED));
}
// Check if it's time to blink the BLUE LED
if (millis() - previousMillisBLUE > intervalBLUE) {
previousMillisBLUE = millis();
digitalWrite(BLUE, !digitalRead(BLUE));
}
// Check if it's time to blink the GREEN LED
if (millis() - previousMillisGREEN > intervalGREEN) {
previousMillisGREEN = millis();
digitalWrite(GREEN, !digitalRead(GREEN));
}
// If the button state has changed and is HIGH, and the debounce delay has passed, print a message to the Serial Monitor
if (buttonState1 != lastButtonState1 && buttonState1 == HIGH && millis() - lastDebounceTime1 > debounceDelay) {
lastDebounceTime1 = millis();
Serial.println("Button 1 is pressed");
}
if (buttonState2 != lastButtonState2 && buttonState2 == HIGH && millis() - lastDebounceTime2 > debounceDelay) {
lastDebounceTime2 = millis();
Serial.println("Button 2 is pressed");
}
if (buttonState3 != lastButtonState3 && buttonState3 == HIGH && millis() - lastDebounceTime3 > debounceDelay) {
lastDebounceTime3 = millis();
Serial.println("Button 3 is pressed");
}
// Update the last button states
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;
lastButtonState3 = buttonState3;
}