How to make custom formulas as variables?

Hello,
I need to figue out a way to implement custom math at the end of custom variable, here is example.
message += String("result= " + String((256 * float(inByte[5]) + float(inByte[6])) *0.01-100) ) ;
variable = (a+b)*0.1-100
so part *0.1-100 is dynamic, like variable = (a+b)*dynamicFormula, dynamic formula always starts with multiplication and after multiplication there is addition

Why not make the parts of the "dynamic formula" variables ?

i need just formula to be dynamic, each formula has its own static variables.
i have like 100 different formulas each has its own name. All formulas is *variable1+variable2

    int a = 10;
    int b = 2;
    float x = 0.1;
    int y = 100;
    float result = (a + b) * x - 100;

make a function?

yes maybe, can you show example using this line?
message += String("result= " + String((256 * float(inByte[5]) + float(inByte[6])) *0.01-100) ) ;

In other words, you have one formula, with different parameters.

don't show me just one, share 10 of them so that we actually get what you mean

yes, my mistake, one formula with different variables in this formlula which makes this formula unique

Column D is name of formula, column E is variables in formula.

Hi,

Can you tell us the application/project that uses all these 100 functions?
What are you trying to attain with your code?
What model Arduino?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

may be something like this would give you an idea

enum Config : byte {PRES_CARB_T, PRES_CARB_P, PRES_Temperatur_AT_J};
struct Params {
  const char * presetName;
  float a;
  float b;
};

Params configParams[] = {
  {"PRES_CARB_T",           1.0,      -40.0},     // PRES_CARB_T
  {"PRES_CARB_P",           0.392,    0.0},       // PRES_CARB_P
  {"PRES_Temperatur_AT_J",  0.1,      -273.14},   // PRES_Temperatur_AT_J
  // ...
};

float applyConfig(float x, Config c) {
  float result = x * configParams[c].a + configParams[c].b;
  Serial.print(configParams[c].presetName);
  Serial.print(": ");
  Serial.print(x);
  Serial.print(" * ");
  Serial.print(configParams[c].a);
  Serial.print(" + ");
  Serial.print(configParams[c].b);
  Serial.print(" = ");
  Serial.println(result);
  return result;
}

void setup() {
  Serial.begin(115200); Serial.println();
  applyConfig(12, PRES_CARB_T);
  applyConfig(22, PRES_CARB_P);
  applyConfig(42, PRES_Temperatur_AT_J);
}

void loop() {}
3 Likes

i am using esp32, project is reading live data from car ecu via serial connection, i need to make request, and i am getting answer, and then i need to convert 1byte or 2byte data using formulas to get real data. I have more than 100 formulas in total, specific for each type of data, pressure, temperature, speed ect. I know which formula to use by formula name.

amazing, this works just 1:1 what i asked. Its amazing that you can generate my idea to code so fast and it works at first try!
So end result with your code works this way
message += String("DT_30_Kraftstof = " + String(applyConfig(((256 * inByte[27]) + (inByte[28])), PRES_CARB_T))) ;

yes if you really need to use Strings :slight_smile:

yes there is more stuff, i am sending data via websockets so i am seeing live data in web browser without manual refresh and html data gets there via string, i am not sure if its correct way but it works great.
Thanks for suggestions and help!

it would be more readable (and less messy with the String) as

message += "DT_30_Kraftstof = ";
message += applyConfig(256.0 * inByte[27] + inByte[28], PRES_CARB_T); // with full decimals

or

message += "DT_30_Kraftstof = ";
message += String(applyConfig(256.0 * inByte[27] + inByte[28], PRES_CARB_T), 2); // with 2 decimals

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