Is there a way to pass which "level" of structure is desired to a formula in (Arduino) C++?

  arduino, arduino-c++, arduino-uno, c++

I have a lot of variables (3×12) that I have set up under a structure:

struct Tracking 
{
  String Title;
  BoolArray n24hr;
  bool State;
  unsigned char Days, Weeks;
  uint16_t Minutes, TotalMinutes, Daily, Weekly, Monthly, n7d[7], n4w[4];
} Components[3];

I also have code that performs basically the same thing 3 times but on different "levels", e.g. daily, weekly, monthly. I would love to be able to pass some info to a formula that would perform the code on the structure’s variables as indicated, something like:

void formula(uint8_t counter, Tracking& level) //counter chooses which Component, level designates whether Daily, Weekly, Monthly, which then affects whether minutes, days, weeks are used in the formula.
{
    Components[counter].level -= Components[counter].n7d[Components[counter].Days]; //remove original value in position
    Components[counter].n7d[Components[counter].Days] = Components[counter].Daily; //adds the new value to the position
    Components[counter].level += Components[counter].n7d[Components[counter].Days]; //adds new value in position
 }

I tried passing the level using a string, but it didn’t work:

formula(i, "Daily"); //sending data to formula
//formula defined as:
void formula(uint8_t counter, string level) 
    {Components[counter].level +-= //etc...

I tried passing the structure & variable itself, but that didn’t work either:

formula(i, Components[i].Daily); //sending data to formula
//formula defined as:
void formula(uint8_t counter, Tracking& level) 
    {level -= //etc...

I couldn’t find any google searches to help me, and I trialed-and-errored a bunch of ways, but I couldn’t figure out how to do that in C++, let alone on the Arduino platform. In Excel VBA, I would just have the variable passed as a string to the formula, which would substitute the word and then treat it like the variable that it is, but I couldn’t make that happen in C++. Also to note, I am going to try and define this a separate file/tab so that my massive code file is easier to read/edit, in case that makes a difference. Is there a way to do this on C++?

Source: Windows Questions C++

LEAVE A COMMENT