/*
Forum: https://forum.arduino.cc/t/display-hinzufugen-status-an-oder-aus/1347226/1
Wokwi: https://wokwi.com/projects/421174693653771265
*/
#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 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);
int LED_pin = 8; // built-in LED
int BUTTON_ON_pin = 7; // Pin 7
int BUTTON_OFF_pin = 4; // Pin 4
// Serial Tx: Pin 20
// Serial Rx: Pin 21
unsigned char command_on[] = { 0x68, 0x40, 0xBF, 0x68, 0x04, 0x03, 0xD3, 0x3D, 0x3C, 0x22, 0x16 };
unsigned char command_off[] = { 0x68, 0x40, 0xBF, 0x68, 0x04, 0x03, 0xD3, 0x3D, 0x35, 0x1B, 0x16 };
void setup()
{
Serial.begin(9600, SERIAL_8E1); // 9600 Baud Even Parity
pinMode(LED_pin, OUTPUT);
digitalWrite(LED_pin, LOW); // LED off
pinMode(BUTTON_ON_pin, INPUT_PULLUP);
pinMode(BUTTON_OFF_pin, INPUT_PULLUP);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
Display("Taste ...");
}
void loop()
{
if (digitalRead(BUTTON_ON_pin) == LOW)
{
digitalWrite(LED_pin, HIGH); // LED on
Display("Sende ON");
Serial.write(command_on, sizeof(command_on));
delay(1000); // 1000 ms (just for debounce and sending only once)
Display("Taste ...");
digitalWrite(LED_pin, LOW); // LED off
}
else if (digitalRead(BUTTON_OFF_pin) == LOW)
{
digitalWrite(LED_pin, HIGH); // LED on
Display("Sende OFF");
Serial.write(command_off, sizeof(command_off));
delay(1000); // 1000 ms (just for debounce and sending only once)
Display("Taste ...");
digitalWrite(LED_pin, LOW); // LED off
}
}
void Display(char * text) {
display.clearDisplay();
display.setCursor(8, 24); // Start at top-left corner
display.println(text);
display.display();
}