#include <LiquidCrystal.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_WIDTH 128 // OLED display width, in pixels
#define OLED_HEIGHT 64 // OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
int buzzer = 6;
const int buttonPin = 8;
const int ledPin = 13;
int buttonState = 0;
const int buzzerPin = 9;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buzzer, OUTPUT);
lcd.begin(16, 2);
lcd.print("Button state");
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(1000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 1); // position to display
oled.println("Button state"); // text to display
oled.display();
}
void loop() {
lcd.setCursor(0, 1);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
lcd.print("ON ");
tone(buzzer, 10000);
oled.clearDisplay();
oled.setCursor(0, 1);
oled.println("Button state");
oled.setCursor(0, 15);
oled.println(("ON "));
oled.display();
} else if (buttonState == LOW) {
digitalWrite(ledPin, LOW);
lcd.print("OFF");
noTone(buzzer);
oled.setCursor(0, 15);
oled.clearDisplay();
oled.setCursor(0, 1);
oled.println("Button state");
oled.setCursor(0, 15);
oled.println(("OFF"));
oled.display();
}
}
FPS: 0
Power: 0.00W
Power: 0.00W