#include "EEPROM.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define EEPROM_SIZE 1024
#define sw14 16
#define sw15 17
#define sw10 5
#define potpin 34
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
int potValue = 0;
int address =0;
int ledPot = 0;
int ledPot14 = 0;
int ledPot15 = 0;
int ledPotformat = 0;
const int ledPin = 25;
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
char tempstring[100];
int onetime =1;
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
}
pinMode(sw14,INPUT_PULLUP);
pinMode(sw15,INPUT_PULLUP);
pinMode(sw10,INPUT_PULLUP);
ledcSetup(ledChannel,freq,resolution);
ledcAttachPin(ledPin,ledChannel);
if (!EEPROM.begin(EEPROM_SIZE)) {
Serial.println("Failed init EEPROM");
delay(1000);
ESP.restart();
}
Serial.println("Success init EEPROM");
display.display();
delay(2000);
display.clearDisplay();
// Test Draw Char
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(F("HELLO"));
display.setCursor(0, 15);
display.println(F("ESP32"));
display.display();
delay(2000);
}
void loop() {
potValue = analogRead(potpin);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.display();
sprintf(tempstring, "EEPROM ADC: %d",potValue);
display.print(tempstring);
display.setCursor(0, 10);
sprintf(tempstring, "Address: %d (%x)",address,address);
display.print(tempstring);
display.setCursor(0, 20);
sprintf(tempstring, "Data: %d",ledPotformat/16);
display.print(tempstring);
display.setCursor(0, 30);
if(digitalRead(sw15)== LOW){
ledPotformat=ledPot15;
}
if(digitalRead(sw14)== LOW){
ledPotformat=ledPot14;
}
display.display();
if(digitalRead(sw15)== LOW){
address = 0;
Serial.println(EEPROM.readUChar(0));
ledcWrite(ledChannel,ledPot15);
}
else if(digitalRead(sw14)== LOW){
address = 154;
Serial.println(EEPROM.readUChar(154));
ledcWrite(ledChannel,ledPot14);
}
if (digitalRead(sw10) == LOW){
EEPROM.writeUChar(address,potValue);
EEPROM.commit();
ledPot = potValue;
if(address==154){
ledPot14 = potValue;
}
if(address==0){
ledPot15 = potValue;
}
Serial.println(EEPROM.readUChar(address));
}
}