Loading configuration data into sketch

I have a sketch that uses a Capacitance Soil Moisture Sensor. I have hard coded the raw input to output in a class so that the readings are 0-100% moisture. I would like to keep the configuration data in a configuration file that gets loaded when the program starts. How do I load this file to included the (raw_low=850 and raw_high=340) data which then is used in my class library? Do I #include moisture_config.h file that has the definitions?

I don't want to update my custom library file every time I recalibrate. I just want to update a small config file with the specific calibration data.

Thank you all in advance.

Does your unnamed processor have EEPROM?

Sorry. I am using an Arduino UNO.

Then put the data in EEPROM. People typically have the calibration sketch do that. https://docs.arduino.cc/learn/built-in-libraries/eeprom

1 Like
//
// EEPROM image - we need pointers, not variables
//
const int promTempFC        =  0;   // Flag - EEPROM BASE - ONLY ADD AT END
const int promAlarmSet      =  1;   // Flag
const int promMatrixBright  =  2;   // Int, 0-15

#include <EEPROM.h>       // EEPROM support for tables

// in setup()

  clockMode12       = EEPROM.read(promClockMode12 );  // Default to 12 hr time, false for 24,
  AlarmSet          = EEPROM.read(promAlarmSet    );  // Alarm Set/not, save in EEPROM
  tempFC            = EEPROM.read(promTempFC      );  // Farenheit/not celcius


//
// Updates given EEPROM *BYTE* address with value, ints > 255 need TWO!
//
void logProm(int Addr, int Valu)
{
  EEPROM.update(Addr, Valu);
}

1 Like
//
// Updates given EEPROM *BYTE* address with value, ints > 255 need TWO!
//
void logProm(int Addr, int Valu)
{
  EEPROM.update(Addr, Valu);
}

This function would benefit from using EEPROM.put() as otherwise writing any value larger than a byte will be unnecessarily complicated

1 Like

It does say BYTE ...

It also says need TWO
You might like to explain how to save and load an int using this function

If it's a flag (0/1) or a value under 255 it works just fine. No need to store an MSH that is always fixed.

When giving examples I like to show the simplest possible case instead of more complex examples.

Since it's ONE LINE even showing it as a function is a stretch.

But what if it is not a value that will fit into a byte ?

In fact, from the original post we know that the values are larger than 255

Only a fool argues with an idiot and I'm neither. I gave the simplest example. You want more complexity, POST CODE instead of kvetching.

1 Like

You posted an example of how to save a byte, which is not what is required. Unfortunately the function that you posted takes an int as the parameter for the value to be saved but only saves one byte of it. The function has a comment indicating that it will not save an int but you have not explained how to do this

You want code that saves an int ?

void logProm(int Addr, int Valu)
{
  EEPROM.put(Addr, Valu);
}

which is what I suggested in post #6

Thank you all for your input. I will use my Arduino Uno EEPROM to save my configuration data.

Best regards

I have run into a problem using EEPROM. The data that I EEPROM.get is not the same data that I EEPROM.put. Can anyone explain why? For example, I am writing (put) the int value of 312 to address 0 and when I use get to retrieve the data from address 0, I get 56. I am a bit confused on why this is happening.

Are you writing a value to EEPROM address 1 too by any chance ?

Please post your complete sketch

I figured it out. Each EEPROM address is using two bytes. All working now.
Thank you all.

It seems that you got my clue

An int requires 2 bytes of storage, as you have discovered, hence ints cannot be written to adjacent EEPROM locations

Thanks for the education. I really appreciate it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.