user47766: I am using the Preferences.h library in my Arduino code. I initialize some values. I save them with the preference library. I then modify all the values, then read back the original saved values.
All the values, except one, are restored with the associated get instruction. The one still shows the modified value. I can't see why it is wrong.
Can someone point out my error with calibratedWeight? And if you see some other general errors with the code, please point them out. (I'm not a programmer)
#include <Preferences.h>

Preferences preferences;

int currentFile = 1;
int outputRate = 10;           // milliSeconds
float outputGain = 1.23;
int zeroWeight = 900;          // dac value to produce Zero on scale
int calibratedWeight = 2800;   // dac value equal to 100lb calibration weight.
float DACmultiplier = 20.1;  

//---------------------------------------
void savePreferences() {
    preferences.begin("ScaleSetup", false);      // false for read/write
    preferences.putInt("currentFile", currentFile);
    preferences.putInt("outputRate", outputRate);
    preferences.putFloat("outputGain", outputGain);
    preferences.putInt("zeroWeight", zeroWeight);
    preferences.putInt("calibratedWeight", calibratedWeight);  // ???
    preferences.putFloat("DACmultiplier", DACmultiplier);
    preferences.end();
}

//---------------------------------------
void getPreferences() {
    preferences.begin("ScaleSetup", false);        // false for read/write

    currentFile = preferences.getInt("currentFile", currentFile);
    outputRate = preferences.getInt("outputRate", outputRate);
    outputGain = preferences.getFloat("outputGain", outputGain);
    zeroWeight = preferences.getInt("zeroWeight", zeroWeight);
    calibratedWeight = preferences.getInt("calibratedWeight", calibratedWeight);  // ???
    DACmultiplier = preferences.getFloat("DACmultiplier", DACmultiplier);
    preferences.end();
}

//============================================
void setup() {
  Serial.begin(115200);
  Serial.println();

  preferences.begin("ScaleSetup", false);        // false for read/write
  preferences.clear();
  preferences.end();
  delay(90);

  savePreferences();
  Serial.println("\n save preferences");
  Serial.println("\n Original asigned values. \n");

  Serial.printf("currentFile: %i\n", currentFile);
  Serial.printf("outputRate: %i\n", outputRate);
  Serial.printf("outputGain: %f\n", outputGain);
  Serial.printf("zeroWeight: %i\n", zeroWeight);
  Serial.printf("calibratedWeight: %i\n", calibratedWeight);  // ???
  Serial.printf("DACmultiplier: %f\n", DACmultiplier);

  Serial.println("\n modify values. \n");

  currentFile = 5;
  outputRate = 50;           // milliSeconds
  outputGain = 0.45;
  zeroWeight = 1333;         // dac value to produce Zero on scale
  calibratedWeight = 2200;   // dac value equal to 100lb calibration weight.
  DACmultiplier = 12.34;

  Serial.printf("currentFile: %i\n", currentFile);
  Serial.printf("outputRate: %i\n", outputRate);
  Serial.printf("outputGain: %f\n", outputGain);
  Serial.printf("zeroWeight: %i\n", zeroWeight);
  Serial.printf("calibratedWeight: %i\n", calibratedWeight);  // ???
  Serial.printf("DACmultiplier: %f\n", DACmultiplier);

  Serial.println("\n Get original saved preferences. \n");
  getPreferences();

  Serial.printf("currentFile: %i\n", currentFile);
  Serial.printf("outputRate: %i\n", outputRate);
  Serial.printf("outputGain: %f\n", outputGain);
  Serial.printf("zeroWeight: %i\n", zeroWeight);
  Serial.printf("calibratedWeight: %i\n", calibratedWeight);  // ???
  Serial.printf("DACmultiplier: %f\n", DACmultiplier);

//  Serial.println("Restarting in 10 seconds...");
  delay(10000);
//  ESP.restart();
}

//============================================
void loop() {}

The following is the result.
 save preferences

 Original asigned values. 

currentFile: 1
outputRate: 10
outputGain: 1.230000
zeroWeight: 900
calibratedWeight: 2800
DACmultiplier: 20.100000

 modify values. 

currentFile: 5
outputRate: 50
outputGain: 0.450000
zeroWeight: 1333
calibratedWeight: 2200
DACmultiplier: 12.340000

 Get original saved preferences. 

currentFile: 1
outputRate: 10
outputGain: 1.230000
zeroWeight: 900
calibratedWeight: 2200
DACmultiplier: 20.100000


user47766: At the end of the comments above I said I thought the problem was with the variable name length.
Today I tried the debug option and then ran the code. And the following error message was included in the com output.
[E][Preferences.cpp:163] putInt(): nvs_set_i32 fail: calibratedWeight KEY_TOO_LONG
So I guess I was right. I didn't know anything about the debug option before.

47766: <p>I am using the Preferences.h library in my Arduino code. I initialize some values. I save them with the preference library. I then modify all the values, then read back the original saved values.</p>
<p>All the values, <strong>except one</strong>, are restored with the associated get instruction. The one still shows the modified value. I can't see why it is wrong.</p>
<p>Can someone point out my error with calibratedWeight? And if you see some other general errors with the code, please point them out. (I'm not a programmer)</p>
<pre><code>#include &lt;Preferences.h&gt;

Preferences preferences;

int currentFile = 1;
int outputRate = 10;           // milliSeconds
float outputGain = 1.23;
int zeroWeight = 900;          // dac value to produce Zero on scale
int calibratedWeight = 2800;   // dac value equal to 100lb calibration weight.
float DACmultiplier = 20.1;  

//---------------------------------------
void savePreferences() {
    preferences.begin(&quot;ScaleSetup&quot;, false);      // false for read/write
    preferences.putInt(&quot;currentFile&quot;, currentFile);
    preferences.putInt(&quot;outputRate&quot;, outputRate);
    preferences.putFloat(&quot;outputGain&quot;, outputGain);
    preferences.putInt(&quot;zeroWeight&quot;, zeroWeight);
    preferences.putInt(&quot;calibratedWeight&quot;, calibratedWeight);  // ???
    preferences.putFloat(&quot;DACmultiplier&quot;, DACmultiplier);
    preferences.end();
}

//---------------------------------------
void getPreferences() {
    preferences.begin(&quot;ScaleSetup&quot;, false);        // false for read/write

    currentFile = preferences.getInt(&quot;currentFile&quot;, currentFile);
    outputRate = preferences.getInt(&quot;outputRate&quot;, outputRate);
    outputGain = preferences.getFloat(&quot;outputGain&quot;, outputGain);
    zeroWeight = preferences.getInt(&quot;zeroWeight&quot;, zeroWeight);
    calibratedWeight = preferences.getInt(&quot;calibratedWeight&quot;, calibratedWeight);  // ???
    DACmultiplier = preferences.getFloat(&quot;DACmultiplier&quot;, DACmultiplier);
    preferences.end();
}

//============================================
void setup() {
  Serial.begin(115200);
  Serial.println();

  preferences.begin(&quot;ScaleSetup&quot;, false);        // false for read/write
  preferences.clear();
  preferences.end();
  delay(90);

  savePreferences();
  Serial.println(&quot;\n save preferences&quot;);
  Serial.println(&quot;\n Original asigned values. \n&quot;);

  Serial.printf(&quot;currentFile: %i\n&quot;, currentFile);
  Serial.printf(&quot;outputRate: %i\n&quot;, outputRate);
  Serial.printf(&quot;outputGain: %f\n&quot;, outputGain);
  Serial.printf(&quot;zeroWeight: %i\n&quot;, zeroWeight);
  Serial.printf(&quot;calibratedWeight: %i\n&quot;, calibratedWeight);  // ???
  Serial.printf(&quot;DACmultiplier: %f\n&quot;, DACmultiplier);

  Serial.println(&quot;\n modify values. \n&quot;);

  currentFile = 5;
  outputRate = 50;           // milliSeconds
  outputGain = 0.45;
  zeroWeight = 1333;         // dac value to produce Zero on scale
  calibratedWeight = 2200;   // dac value equal to 100lb calibration weight.
  DACmultiplier = 12.34;

  Serial.printf(&quot;currentFile: %i\n&quot;, currentFile);
  Serial.printf(&quot;outputRate: %i\n&quot;, outputRate);
  Serial.printf(&quot;outputGain: %f\n&quot;, outputGain);
  Serial.printf(&quot;zeroWeight: %i\n&quot;, zeroWeight);
  Serial.printf(&quot;calibratedWeight: %i\n&quot;, calibratedWeight);  // ???
  Serial.printf(&quot;DACmultiplier: %f\n&quot;, DACmultiplier);

  Serial.println(&quot;\n Get original saved preferences. \n&quot;);
  getPreferences();

  Serial.printf(&quot;currentFile: %i\n&quot;, currentFile);
  Serial.printf(&quot;outputRate: %i\n&quot;, outputRate);
  Serial.printf(&quot;outputGain: %f\n&quot;, outputGain);
  Serial.printf(&quot;zeroWeight: %i\n&quot;, zeroWeight);
  Serial.printf(&quot;calibratedWeight: %i\n&quot;, calibratedWeight);  // ???
  Serial.printf(&quot;DACmultiplier: %f\n&quot;, DACmultiplier);

//  Serial.println(&quot;Restarting in 10 seconds...&quot;);
  delay(10000);
//  ESP.restart();
}

//============================================
void loop() {}
</code></pre>
<p>The following is the result.</p>
<pre><code> save preferences

 Original asigned values. 

currentFile: 1
outputRate: 10
outputGain: 1.230000
zeroWeight: 900
calibratedWeight: 2800
DACmultiplier: 20.100000

 modify values. 

currentFile: 5
outputRate: 50
outputGain: 0.450000
zeroWeight: 1333
calibratedWeight: 2200
DACmultiplier: 12.340000

 Get original saved preferences. 

currentFile: 1
outputRate: 10
outputGain: 1.230000
zeroWeight: 900
calibratedWeight: 2200
DACmultiplier: 20.100000
</code></pre>
 47766: <p>At the end of the comments above I said I thought the problem was with the variable name length.</p>
<p>Today I tried the debug option and then ran the code. And the following error message was included in the com output.</p>
<p>[E][Preferences.cpp:163] putInt(): nvs_set_i32 fail: calibratedWeight <strong>KEY_TOO_LONG</strong></p>
<p>So I guess I was right. I didn't know anything about the debug option before.</p>
