Not sure if there is a better way to do this but this is how i wrote to eeprom on calibration:
Here is just sections so its not so hard to find. The reason i used the array also when naming the sensor min array is just so its full proof to those slots on the eeprom 1,11,21,31 and so on otherwise if i had a number wrong there (or change it later) id have to update it manually
// Naming some variables
// Height sensors ( values, Pins, Min values, and Max values)
const int SensorMinSlot[4] = { 1, 11, 21, 31};
const int SensorMaxSlot[4] = { 2, 12, 22, 32};
const int HeightSensorPin[4] = { A0, A1, A2, A3 };
int HeightSensorValue[4];
int SensorMin[4] = { EEPROM.read(SensorMinSlot[0]), EEPROM.read(SensorMinSlot[1]), EEPROM.read(SensorMinSlot[2]), EEPROM.read(SensorMinSlot[3])};
int SensorMax[4] = { EEPROM.read(SensorMaxSlot[0]), EEPROM.read(SensorMaxSlot[1]), EEPROM.read(SensorMaxSlot[2]), EEPROM.read(SensorMaxSlot[3])};
int SensorRange[4] = { (SensorMax[0] - SensorMin[0]), (SensorMax[1] - SensorMin[1]), (SensorMax[2] - SensorMin[2]), (SensorMax[3] - SensorMin[3]) };
Then when calibration is on ( set to true when a pin is low during setup only) it does some auto airing out and inflating the car to get to certain heights then saves these values to eeprom as new max and min values
for(int Wheels = 0; Wheels < 4; Wheels++){
HeightSensorValue[Wheels] = analogRead(HeightSensorPin[Wheels])/4;
EEPROM.write(SensorMaxSlot[Wheels],HeightSensorValue[Wheels]);
Serial.print(EEPROM.read(SensorMaxSlot[Wheels]));
Serial.print(" ");
} // end for
I will be tightening things up as right now i have multiple for loops in the calibration but its working flawlessly at the moment so pretty happy with it. Heres the full calibration code if wanting to have a look anyway
// Calibration Start
if (Calibration==0){
Serial.println("Calibrating Initiated Please Wait");
// Flash Built in LED
digitalWrite(LED_BUILTIN, 1);
delay(1000);
digitalWrite(LED_BUILTIN, 0);
delay(1000);
digitalWrite(LED_BUILTIN, 1);
delay(2000);
digitalWrite(LED_BUILTIN, 0);
// Calibrate Min Sensor Values
Serial.println("Airing out Please Wait");
for(int Wheels = 0; Wheels < 4; Wheels++){
digitalWrite (DeflateRelays[Wheels], 1);
} // end for
delay(10000);
for(int Wheels = 0; Wheels < 4; Wheels++){
digitalWrite (DeflateRelays[Wheels], 0);
} // end for
Serial.println("Saving Minimum Sensor Values to EEPROM");
for(int Wheels = 0; Wheels < 4; Wheels++){
HeightSensorValue[Wheels] = analogRead(HeightSensorPin[Wheels])/4;
EEPROM.write(SensorMinSlot[Wheels],HeightSensorValue[Wheels]);
Serial.print(EEPROM.read(SensorMinSlot[Wheels]));
Serial.print(" ");
} // end for
Serial.println(" Minimum Sensor Values stored:");
// Calibrate Max Sensor Values
Serial.println("Inflating Please Wait");
for(int Wheels = 0; Wheels < 4; Wheels++){
digitalWrite (InflateRelays[Wheels], 1);
} // end for
delay(10000);
for(int Wheels = 0; Wheels < 4; Wheels++){
digitalWrite (InflateRelays[Wheels], 0);
} // end for
Serial.println("Saving Minimum Sensor Values to EEPROM");
for(int Wheels = 0; Wheels < 4; Wheels++){
HeightSensorValue[Wheels] = analogRead(HeightSensorPin[Wheels])/4;
EEPROM.write(SensorMaxSlot[Wheels],HeightSensorValue[Wheels]);
Serial.print(EEPROM.read(SensorMaxSlot[Wheels]));
Serial.print(" ");
} // end for
Serial.println(" Maximum Sensor Values stored:");
} // End Calibration if