SInce EEPROM can only be written a finite number of times (typically 100,000), we compare the contents before writing and only write if something has changed. This functionality is implemented in the following helper functions:
// ************************************************ // Save any parameter changes to EEPROM // ************************************************ void SaveParameters() { if (Setpoint != EEPROM_readDouble(SpAddress)) { EEPROM_writeDouble(SpAddress, Setpoint); } if (Kp != EEPROM_readDouble(KpAddress)) { EEPROM_writeDouble(KpAddress, Kp); } if (Ki != EEPROM_readDouble(KiAddress)) { EEPROM_writeDouble(KiAddress, Ki); } if (Kd != EEPROM_readDouble(KdAddress)) { EEPROM_writeDouble(KdAddress, Kd); } } // ************************************************ // Load parameters from EEPROM // ************************************************ void LoadParameters() { // Load from EEPROM Setpoint = EEPROM_readDouble(SpAddress); Kp = EEPROM_readDouble(KpAddress); Ki = EEPROM_readDouble(KiAddress); Kd = EEPROM_readDouble(KdAddress); // Use defaults if EEPROM values are invalid if (isnan(Setpoint)) { Setpoint = 60; } if (isnan(Kp)) { Kp = 500; } if (isnan(Ki)) { Ki = 0.5; } if (isnan(Kd)) { Kd = 0.1; } } // ************************************************ // Write floating point values to EEPROM // ************************************************ void EEPROM_writeDouble(int address, double value) { byte* p = (byte*)(void*)&value; for (int i = 0; i < sizeof(value); i++) { EEPROM.write(address++, *p++); } } // ************************************************ // Read floating point values from EEPROM // ************************************************ double EEPROM_readDouble(int address) { double value = 0.0; byte* p = (byte*)(void*)&value; for (int i = 0; i < sizeof(value); i++) { *p++ = EEPROM.read(address++); } return value; }