#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "EEPROM.h"
// Size Address
#define EEPROM_SIZE 4096
// Display Resolution
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_ADDRESS 0x3c
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
// Define Switch
#define SW 5
// Define GPIO of POT and LED
#define potPin 34
#define LED 25
// Initial Value
int potValue = 0;
int lastTime1000ms = 0;
int Value_0x000, Value_0x3CA;
int Now_ADDR = 0;
void setup() {
// Begin
Serial.begin(115200);
Wire.begin();
// Set PIN MODE
pinMode(LED, OUTPUT);
pinMode(SW, INPUT_PULLUP);
// Setup EEPROM
if (!EEPROM.begin(EEPROM_SIZE)) {
Serial.println("Failed init EEPROM");
delay(1000);
ESP.restart();
}
Serial.println("Success init EEPROM");
// Setup Initial Value of ADDR: 0x000, 0x3CA
EEPROM.writeUChar(0x000, 0);
EEPROM.commit();
EEPROM.writeUChar(0x3CA, potValue);
EEPROM.commit();
// Read Value From EEPROM
Value_0x000 = EEPROM.readUChar(0x000);
Value_0x3CA = EEPROM.readUChar(0x3CA);
potValue = analogRead(potPin);
// Setup Display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
}
display.display();
display.clearDisplay();
// Test Draw Char
display.setTextSize(1.5);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.printf("EEPROM ADC : %d", potValue*255/4095);
display.setCursor(0, 30);
display.printf("Address : 0000(0x000)");
display.setCursor(0, 45);
display.printf("Data : %d", Value_0x000);
display.display();
// setup LED
analogWrite(LED, Value_0x000);
}
void loop() {
// Reading Value
potValue = analogRead(potPin);
Value_0x000 = EEPROM.readUChar(0x000);
Value_0x3CA = EEPROM.readUChar(0x3CA);
// Show Value every 1 sec
if(millis() - lastTime1000ms >= 1000) {
lastTime1000ms = millis();
Serial.printf("POT : %d CONV : %d \n", potValue, potValue*255/4095);
}
// Switch Display from SW Mode
if(Now_ADDR == 0) {
display.clearDisplay();
// Test Draw Char
display.setTextSize(1.5);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.printf("EEPROM ADC : %d", potValue*255/4095);
display.setCursor(0, 30);
display.printf("Address : 0000(0x000)");
display.setCursor(0, 45);
display.printf("Data : %d", Value_0x000);
display.display();
} else {
display.clearDisplay();
// Test Draw Char
display.setTextSize(1.5);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.printf("EEPROM ADC : %d", potValue*255/4095);
display.setCursor(0, 30);
display.printf("Address : 0970(0x3CA)");
display.setCursor(0, 45);
display.printf("Data : %d", Value_0x3CA);
display.display();
}
// Check SW State
if (digitalRead(SW1) == LOW) {
Serial.println("SW1 : DOWN");
analogWrite(LED, Value_0x000);
Now_ADDR = 0;
} else if (digitalRead(SW2) == LOW) {
Serial.println("SW2 : DOWN");
analogWrite(LED, Value_0x3CA);
Now_ADDR = 1;
} else if (digitalRead(SW3) == LOW) {
Serial.println("SW3 : DOWN");
if(Now_ADDR == 0) {
EEPROM.writeUChar(0x000, potValue*255/4095);
EEPROM.commit();
analogWrite(LED, potValue*255/4095);
} else {
EEPROM.writeUChar(0x3CA, potValue*255/4095);
EEPROM.commit();
analogWrite(LED, potValue*255/4095);
}
}