You don't see u.float_variable
increase because you assign a value to it every time your program starts. When the CPU exits deep sleep, the CPU restarts and runs setup()
again, which always sets u.float_variable
to 2.1.
You could solve this problem by initializing the union as you did cpt
:
RTC_DATA_ATTR union {
float float_variable;
byte temp_array[4];
} u = { .float_variable = 2.1 };
int sleepTime =5;
RTC_DATA_ATTR int cpt = 0;
void setup() {
Serial.begin(115200);
esp_sleep_enable_timer_wakeup(sleepTime * 1000000);
// u.float_variable=2.1; THIS FORCES u.float_variable to stay at 2.1
}
void loop() {
Serial.println("wake up number: " + String(cpt) + " u.float_variable is: " + String(u.float_variable));
cpt++;
u.float_variable+=cpt;
esp_deep_sleep_start();
}
or if union initializations get you down you could only initialize u.float_variable
on your first run through:
RTC_DATA_ATTR union {
float float_variable;
byte temp_array[4];
} u;
int sleepTime =5;
RTC_DATA_ATTR int cpt = 0;
void setup() {
Serial.begin(115200);
esp_sleep_enable_timer_wakeup(sleepTime * 1000000);
if(cpt == 0)
u.float_variable=2.1;
}
void loop() {
Serial.println("wake up number: " + String(cpt) + " u.float_variable is: " + String(u.float_variable));
cpt++;
u.float_variable+=cpt;
esp_deep_sleep_start();
}
The first form - initializing the union - is better C/C++. The second form gives you a bit more flexibility for more complicated behaviors than this program needs.