// Uses Monocrome OLEDs based on SSD1306 driver
// Uses Adafruit GX Grafics library
// Uses Adaruit SSD1306 library
#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)
// The pins for I2C are defined by the Wire-library.
// On an arduino Nano: A4(SDA), A5(SCL)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x32, 0x3C for 128x64
#define MEM_DEVICE_ADDRESS 0x50 //If Adress pinnns tied to Vdd, 0x54 if tied to Vss.
// https://www.arduino.cc/reference/en/libraries/i2c_eeprom/
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int upButton = 10;
int downButton = 11;
int selectButton = 12;
int menu = 1;
void setup() {
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
updateMenu();
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 0);
oled.println(">Read Mem");
oled.setCursor(0, 16);
oled.println(" Write Mem");
oled.display(); // show on OLED
}
void loop() {
if (!digitalRead(downButton)){
menu++;
updateMenu();
delay(100);
while (!digitalRead(downButton));
}
if (!digitalRead(upButton)){
menu--;
updateMenu();
delay(100);
while(!digitalRead(upButton));
}
if (!digitalRead(selectButton)){
executeAction();
updateMenu();
delay(100);
while (!digitalRead(selectButton));
}
}
void eeprom_i2c_write(byte address, byte from_addr, byte data) {
Wire.beginTransmission(address);
Wire.send(from_addr);
Wire.send(data);
Wire.endTransmission();
}
byte eeprom_i2c_read(int MEM_DEVICE_ADDRESS, int from_addr) {
Wire.beginTransmission(address);
Wire.send(from_addr);
Wire.endTransmission();
Wire.requestFrom(address, 1);
if(Wire.available())
return Wire.receive();
else
return 0xFF;
}
void updateMenu() {
switch (menu) {
case 0:
menu = 1;
break;
case 1:
oled.clearDisplay();
oled.setCursor(0, 0);
oled.println(">Read Mem");
oled.setCursor(0, 16);
oled.println(" Write Mem");
oled.display(); // show on OLED
break;
case 2:
oled.clearDisplay();
oled.setCursor(0, 0);
oled.println(" Read Mem");
oled.setCursor(0, 16);
oled.println(">Write Mem");
oled.display(); // show on OLED
break;
case 3:
oled.clearDisplay();
oled.setCursor(0, 0);
oled.println(">MenuItem3");
oled.setCursor(0, 16);
oled.println(" MenuItem4");
oled.display(); // show on OLED
break;
case 4:
oled.clearDisplay();
oled.setCursor(0, 0);
oled.println(" MenuItem3");
oled.setCursor(0, 16);
oled.println(">MenuItem4");
oled.display(); // show on OLED
break;
case 5:
menu = 4;
break;
}
}
void executeAction() {
switch (menu) {
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
case 4:
action4();
break;
}
}
void action1() {
int data = 0;
data = eeprom_i2c_read(int address, int from_addr);
oled.clearDisplay();
oled.println(">Executing #1");
oled.display(); // show on OLED
delay(1500);
}
void action2() {
oled.clearDisplay();
oled.println(">Executing #2");
oled.display(); // show on OLED
delay(1500);
}
void action3() {
oled.clearDisplay();
oled.println(">Executing #3");
oled.display(); // show on OLED
delay(1500);
}
void action4() {
oled.clearDisplay();
oled.println(">Executing #4");
oled.display(); // show on OLED
delay(1500);
}