here in this simulation, when you press the yellow button, the character keeps changing from 0 to 9 and after a delay of 1 second, it goes to the next position. so after entering the numbers, I want the displayed numbers(the displayed numbers are stored in variable text) to be stored in esp32 EEPROM and when I restart the Esp I want the numbers to be displayed as what I entered previously.
please anyone help me. Thank you
What have you tried so far ?
How about a sketch to write a string to EEPROM, load it back and display it ?
EEPROM on the ESP32 has been depreciated about 2 CORE versions ago.
Use Preferences instead.
you can use the Preferences library on ESP32 to save your data
But if i save the data permanently then if i again press chose button and if enter an another new number , will that new number be save again?
it can be overwritten. It's your code. you decide what happens
Did you read the link?
Did you notice preferences.clear();
and preferences.remove(key);
?
no i didn't see that
Might want to reread the link again.
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <array>
#include <Adafruit_GFX.h>
#include <Preferences.h>
Preferences preferences;
#define TYPING_BUTTON 15
#define BACKSPACE_BUTTON 4
#define POSITION_BUTTON 17
#define BUTTON_DELAY 250
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char text[] = "0000000000";
unsigned int pos = 0;
unsigned long next_pos_time;
const byte maxPos = sizeof text - 1;
bool typing = false;
const char* text1 = text;
void displayText(const char* c) {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 50);
oled.setTextSize(2);
oled.print(c);
oled.display();
}
bool is_button_pressed(uint8_t pin) {
return digitalRead(pin) == LOW;
}
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(TYPING_BUTTON, INPUT_PULLUP);
pinMode(BACKSPACE_BUTTON, INPUT_PULLUP);
pinMode(POSITION_BUTTON, INPUT_PULLUP);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
return;
}
Serial.println();
preferences.begin("credentials", false);
preferences.putString("text", text1);
preferences.end();
oled.clearDisplay();
oled.display();
}
int i;
void loop() {
oled.setCursor(0, 0);
displayText(text);
if (typing && next_pos_time <= millis()) {
typing = false;
pos++;
}
if (is_button_pressed(BACKSPACE_BUTTON)) {
if (typing) {
typing = false;
text[pos] = '/';
} else if (pos > 0) {
pos--;
}
text[pos] = '/';
delay(BUTTON_DELAY);
}
if (is_button_pressed(TYPING_BUTTON)) {
if (text[pos] == '9') text[pos] = '/';
text[pos]++;
next_pos_time = millis() + 1250;
typing = true;
delay(BUTTON_DELAY);
}
}
this is my code and when I enter the numbers and again if I restart, I am not able to see the previously entered numbers also I actually uploading the code to the actual board
What does that mean?
What do you expect from this in the setup?
In this video, you can see the final number entered is 1520000000
So after i restart the board it's again getting back to default
What i want is
Even after restarting the board i want the displayed number to be printed is 1520000000
post the code where the number was stored.
i have already posted it
anyway let me do it again
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <array>
#include <Adafruit_GFX.h>
#include <Preferences.h>
Preferences preferences;
#define TYPING_BUTTON 15
#define BACKSPACE_BUTTON 4
#define POSITION_BUTTON 17
#define BUTTON_DELAY 250
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char text[] = "0000000000";
unsigned int pos = 0;
unsigned long next_pos_time;
const byte maxPos = sizeof text - 1;
bool typing = false;
const char* text1 = text;
void displayText(const char* c) {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(0, 50);
oled.setTextSize(2);
oled.print(c);
oled.display();
}
bool is_button_pressed(uint8_t pin) {
return digitalRead(pin) == LOW;
}
void setup() {
Serial.begin(115200);
delay(2000);
pinMode(TYPING_BUTTON, INPUT_PULLUP);
pinMode(BACKSPACE_BUTTON, INPUT_PULLUP);
pinMode(POSITION_BUTTON, INPUT_PULLUP);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
return;
}
Serial.println();
preferences.begin("credentials", false);
preferences.putString("text", text1);
preferences.end();
oled.clearDisplay();
oled.display();
}
int i;
void loop() {
oled.setCursor(0, 0);
displayText(text);
if (typing && next_pos_time <= millis()) {
typing = false;
pos++;
}
if (is_button_pressed(BACKSPACE_BUTTON)) {
if (typing) {
typing = false;
text[pos] = '/';
} else if (pos > 0) {
pos--;
}
text[pos] = '/';
delay(BUTTON_DELAY);
}
if (is_button_pressed(TYPING_BUTTON)) {
if (text[pos] == '9') text[pos] = '/';
text[pos]++;
next_pos_time = millis() + 1250;
typing = true;
delay(BUTTON_DELAY);
}
}
in this code in the variable "text" the number is stored
so i want that to be stored even after restart
Because the first thing you do to preferences is to store the string "0000000000". You. have to read the previous value from preferences if you want to use the previous value.
You also have to store the new value in preferences every time you change it. If you don't store the new value there will be no way to read it when power returns.
that number does not have a 150 in it.
how do i do this in the programming part
can you please help me
it won't have 152 in this but
when i press the button its changes
for. eg what i did in the video is
i pressed the button for 1 time and waited for 1sec and then pressed the button for 5 times and wanted for 1sec and at last, just pressed for 2 times. so the data is getting stored in the variable text but that's not getting stored permanently.
Where is your code to store the updated number?