Last active
April 14, 2020 15:57
-
-
Save sagiii/fc9c08159f1a7bf84ad9c7042f44d8fb to your computer and use it in GitHub Desktop.
Arduino code idea to leave run duration log in EEPROM. (not tested)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <EEPROM.h> | |
#define CLEAR_BUTTON (2) | |
#define MAX_LOG_N (120) | |
struct run_log_t { | |
uint8_t last_index; | |
uint16_t duration[MAX_LOG_N]; | |
}; | |
uint8_t index; | |
bool is_full = false; | |
run_log_t run_log; | |
void setup() { | |
if ( digitalRead(CLEAR_BUTTON) ) { | |
memset(&run_log, 0xff, sizeof(run_log)); | |
EEPROM.put(0, run_log); | |
} | |
EEPROM.get(0, run_log); | |
if (run_log.last_index == 0xff) { // not initialized | |
index = 0; | |
} else if (run_log.last_index == MAX_LOG_N) { | |
is_full = true; | |
} else if (run_log.last_index < MAX_LOG_N) { | |
index = run_log.last_index + 1; | |
} else { // invalid | |
is_full = true; | |
} | |
if (!is_full) { | |
run_log.last_index = index; | |
run_log.duration[index] = 0; | |
} | |
} | |
void loop() { | |
// update duration per step time (like 1 min.) | |
#define LOG_STEP (60000) // [ms] | |
if (!is_full) { | |
static unsigned long t1 = LOG_STEP; | |
if (millis() > t1) { | |
t1 += LOG_STEP; | |
run_log.duration[index] ++; | |
EEPROM.put(0, run_log); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment