• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

if/else problem

Q&A Forum › if/else problem
0 Vote Up Vote Down
Jean Pierre Daviau asked 5 years ago

Hi,  

Why the else option is never choosen?
 


/*
ESP32 eeprom_class example with EEPROM library
This simple example demonstrates using EEPROM library to store different data in
ESP32 Flash memory in a multiple user-defined EEPROM class objects. Created for arduino-esp32 on 25 Dec, 2017
by Elochukwu Ifediora (fedy0)
converted to nvs by lbernstone - 06/22/2019
*/
#include "EEPROM.h"
#include "Preferences.h"
#define uS_TO_S_FACTOR 1000000ULL
// Instantiate eeprom objects with parameter/argument names and sizes
EEPROMClass NAMES("eeprom0", 0x500);
EEPROMClass HEIGHT("eeprom1", 0x200);
EEPROMClass AGE("eeprom2", 0x100);
RTC_DATA_ATTR uint32_t check2 = 0;
uint32_t age = 0; void setup() {
Serial.begin(115200);
Serial.println("Testing EEPROMClass\n");
if (!NAMES.begin(NAMES.length())) {
Serial.println("Failed to initialise NAMES");
Serial.println("Restarting...");
//ESP.restart();
}
if (!HEIGHT.begin(HEIGHT.length())) {
Serial.println("Failed to initialise HEIGHT");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}
if (!AGE.begin(AGE.length())) {
Serial.println("Failed to initialise AGE");
Serial.println("Restarting...");
delay(1000);
ESP.restart();
}  
Serial.println(age);
Serial.println(check2);
AGE.get(0, age);
if (age != 47) {
const char* name = "Teo Swee Ann";
double height = 5.8;
uint32_t age = 47;
check2 += 1;
Serial.print("In check: "); Serial.println(check);
Serial.println("----------------- WRITE -------------------\n");
NAMES.put(0, name);
HEIGHT.put(0, height);
AGE.put(0, 47); // Write: Variables ---> EEPROM stores
Serial.print("name: "); Serial.println(name);
Serial.print("height: "); Serial.println(height);
Serial.print("age: "); Serial.println(age); Serial.println("----------------- CLEAR -------------------\n");
// Clear variables
Serial.printf("name: %s", name);
name = '\0';
Serial.println(name);
height = 0;
age = 0;
Serial.print("name: "); Serial.println(name);
Serial.print("height: "); Serial.println(height);
Serial.print("age: "); Serial.println(age); Serial.println("----------------- READ -------------------\n");
// Read: Variables <--- EEPROM stores NAMES.get(0, name);
HEIGHT.get(0, height);
AGE.get(0, age);
Serial.print("name: "); Serial.println( name);
Serial.print("height: "); Serial.println(height);
Serial.print("age: "); Serial.println(age);
uint32_t age3 = 47;
AGE.put(0, age3);
Serial.println("----------------- Going to sleep.");
delay(1000);
esp_sleep_enable_timer_wakeup(30 * uS_TO_S_FACTOR);
esp_deep_sleep_start();
//ESP.restart();
} else {
Serial.println("In else");
const char rname[24] = {};
NAMES.put(0, rname[0]);
double height2;
HEIGHT.put(0, height2);
uint32_t age2;
AGE.put(0, age2);
Serial.print("name: "); Serial.println( rname);
Serial.print("height: "); Serial.println(height2);
Serial.print("age2: "); Serial.println(age2);
} Serial.println("\n----------------- Done!");
}
void loop() {
delay(0xFFFFFFFF);
}

 
REgard
 
JPDaviau

Question Tags: eeprom, if/else, preferences.h
7 Answers
0 Vote Up Vote Down
Steve Mercer answered 5 years ago

One thing I see is you declaring the age variable twice. Once before setup. Then again inside if(age != 47). 

0 Vote Up Vote Down
Jean Pierre Daviau answered 5 years ago

AGE.get(0, age); // 42837455
if(age != 47) // first time age = 0;
age = 47
// AGE.put(0, age2); //AGE = 47
sleep
//second turn
AGE.get(0, age); // should be 47 even after deep sleep.

0 Vote Up Vote Down
Steve Mercer answered 5 years ago

Age is just a standard variable so after deep sleep will not be what you think it is. Either save it to EEPROM or RTC variable.

0 Vote Up Vote Down
Jean Pierre Daviau answered 5 years ago

Hi,
Does the value in :  
EEPROMClass AGE(“eeprom2”, 0x100);    will survive  a deep sleep?  Will it survive  a power cut ?  This is the answer I am after.

RTC_DATA_ATTR uint32_t age = 0;
EEPROMClass NAMES("eeprom0", 0x500);
EEPROMClass HEIGHT("eeprom1", 0x200);
EEPROMClass AGE("eeprom2", 0x100);

void setup() {
......................
  AGE.get(0, age);  // get the value from eeprom and put it in the variable age
  
  if (age != 47) {

........
0 Vote Up Vote Down
Steve Mercer answered 5 years ago

Where did you get EEPROMClass from? I see EEPROM from Arduino. The only reference I can find is for a Sony Spresence board. What does the documentation say?

0 Vote Up Vote Down
Jean Pierre Daviau answered 5 years ago

Library:  https://github.com/espressif/arduino-esp32/tree/master/libraries/Preferences

related post :  https://rntlab.com/question/eeprom-update-on-esp32/#answer-70346

0 Vote Up Vote Down
Jean Pierre Daviau answered 5 years ago
see thread  esp restart()  kills eeprom memory

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.