Replacing a string in a function with a string parameter

Overview:

I have 2 sets of string's (WHEATSOYBEAN & COTTON) which need to be interchangeable via a parameter passed into the automation function.

CROPNAME needs to be replaced with the parameter cropname which will either be "WHEATSOYBEAN" or "COTTON" depending on when the function is called.

Example: "CROPNAME_STAGE1 = COTTON_STAGE1" when "cropname = COTTON"

Any help or information on how to achieve this or if it possible would be greatly appreciated.

Thank you for your time, AP

Parts of the code have been removed for privacy reasons I know this is part of the rules; however, I believe I have posted enough code to explain my point*

//WHEATSOYBEAN______________________________________________________________________________________________________
const int WHEATSOYBEAN_STAGE1 = 20; //# of days from plantdate_day to end of stage1
const int WHEATSOYBEAN_STAGE2 = 40; //# of days from plantdate_day to end of stage2
const int WHEATSOYBEAN_STAGE3 = 60; //# of days from plantdate_day to end of stage3


//COTTON____________________________________________________________________________________________________________
const int COTTON_STAGE1 = 10; //# of days from plantdate_day to end of stage1
const int COTTON_STAGE2 = 20; //# of days from plantdate_day to end of stage2
const int COTTON_STAGE3 = 40; //# of days from plantdate_day to end of stage3





void automation(String cropname){

  DateTime now = rtc.now(); //Updates Arduino Time with RTC Time
  
 //CROPNAME needs to be cropname (WHEATSOYBEAN or COTTON)
  
  if(CROPNAME_STAGE1 > automation_daysfrompd()) stage = STAGE1;
  else if(CROPNAME_STAGE2 > automation_daysfrompd()) stage = STAGE2;
  else if(CROPNAME_STAGE3 > automation_daysfrompd()) stage = STAGE3;
  else stage = FALLOW; 
  
  switch(stage){
    case STAGE1:

    break;
    case STAGE2:
     
    break;
    case STAGE3:
      
    break;
    case FALLOW:

    break;
    default:

    break;
  }

I have 2 sets of string's

Actually you don't. You have 2 Strings (uppercase S)

Why not just call the function with the required string ?

automation("WHEATSOYBEAN");

or

automation("COTTON");

Why does it need to be a (crappy) String at all?

enum : byte {WHEATSOYBEAN, COTTON} cropType;
...
void automation(cropType aCrop)
{
   switch (aCrop) {
      case WHEATSOYBEAN:
         Serial.println(F(“WHEATSOYBEAN”));
      break;

      case COTTON:
         Serial.println(F(“COTTON”));
      break;

      default:
         Serial.println(F(“UNKNOWN CROP”));
      break; // not really necessary, just for safety
   }
}
...

If you really need String, use c-strings

const char * wheatsoybean = “WHEAT-SOYBEAN”;
const char * cotton = “COTTON”;
...
void automation(const char * cropName)
{
    if (!strcmp(cropName, wheatsoybean)) {
       Serial.println(F(“WHEATSOYBEAN”));
    } else
    if (!strcmp(cropName, cotton)) {
      Serial.println(F(“COTTON”));
    } else {
      Serial.println(F(“UNKNOWN CROP”));
   }
}
...

It needs to be a “crappy” string because the const’s need to be manipulated easily & quickly without going through almost 3,000 lines of code.

These also need to have int types due to what is being calculated. They cannot be enums. Let me post more code.

I will also be combining the STAGES the same way if it is achievable.

//WHEATSOYBEAN______________________________________________________________________________________________________
const int WHEATSOYBEAN_STAGE1 = 20; //# of days from plantdate_day to end of stage1
const int WHEATSOYBEAN_STAGE2 = 40; //# of days from plantdate_day to end of stage2
const int WHEATSOYBEAN_STAGE3 = 60; //# of days from plantdate_day to end of stage3


//COTTON____________________________________________________________________________________________________________
const int COTTON_STAGE1 = 10; //# of days from plantdate_day to end of stage1
const int COTTON_STAGE2 = 20; //# of days from plantdate_day to end of stage2
const int COTTON_STAGE3 = 40; //# of days from plantdate_day to end of stage3


void automation(String cropname){

  DateTime now = rtc.now(); //Updates Ardunio Time with RTC Time
  
 //CROPNAME needs to be cropname
  
  if(CROPNAME_STAGE1 > automation_daysfrompd()) stage = STAGE1;
  else if(CROPNAME_STAGE2 > automation_daysfrompd()) stage = STAGE2;
  else if(CROPNAME_STAGE3 > automation_daysfrompd()) stage = STAGE3;
  else stage = FALLOW; 
  
  switch(stage){
    case STAGE1:
      if(automation_decision_forced){
        
        serial_printdateandtime();
        Serial.print(cropname);
        Serial.println("...Automation Decision...STAGE1...Successful");
        Serial.print("Days from Plant Date: ");
        Serial.println(daysawayfromPD);
        Serial.print("FWT Depth AVERAGE: ");
        Serial.println(field_watertable_depth_average, 4);
        
        if(field_watertable_depth_average <= CROPNAME_STARTDRAINAGE_STAGE1){
          if(field_watertable_depth <= CROPNAME_STARTDRAINAGE_STAGE1){
            serial_printdateandtime();
            Serial.print(cropname);
            Serial.println("...Automation Drianage...STAGE1...Successful START");
            automation_drainage = true;
            drainage_free();
          }
          else{
            Serial.print(cropname);
            Serial.println("...Automation Decision...STAGE1...DEFAULT of START DRAINAGE!");
            //ADD SDCARD WRITE TO ERROR FILE
            drainage_controlleddown();
            automation_drainage = false;
            automation_irrigation = false;
          }
        }
        else if(field_watertable_depth_average >= CROPNAME_STARTIRRIGATION_STAGE1){
           if((field_watertable_depth == observation_well_depth) | (field_watertable_depth >= CROPNAME_STARTIRRIGATION_STAGE1)){
            automation_irrigation = true;
            automation_irrigation_minute = now.minute();
          }
          else{
            Serial.print(cropname);
            Serial.println("...Automation Decision...STAGE1...DEFAULT of START IRRIGATION!");
            //ADD SDCARD WRITE TO ERROR FILE
            drainage_controlleddown();
            automation_drainage = false;
            automation_irrigation = false;          
          }
        }
        else{
          Serial.print(cropname);
          Serial.println("...Automation Decision...STAGE1...!FWT OPTIMAL!");
          drainage_controlleddown();
          //ADD SDCARD WRITE TO ERROR FILE
          automation_drainage = false;
          automation_irrigation = false;
        }
        field_watertable_depth_sum = 0;
        field_watertable_depth_count = 0;
        if(automation_decision_forced) automation_decision_forced = false;
        automation_decision_entered = true;
      }
      
      if(automation_drainage){
        if(field_watertable_depth == observation_well_depth){
          serial_printdateandtime();
          Serial.print(cropname);
          Serial.println("...Automation Drianage...STAGE1...FWT BAD STOP");
          drainage_controlleddown();
          automation_drainage = false;
          automation_irrigation = false;
          break;
        }
        if(field_watertable_depth >= CROPNAME_STOPDRAINAGE_STAGE1){
          serial_printdateandtime();
          Serial.print(cropname);
          Serial.println("...Automation Drianage...STAGE1...COMPLETE");
          drainage_controlleddown();
          automation_drainage = false;
          automation_irrigation = false;
        }
      }
      
      if(automation_irrigation){
        if(now.minute() == automation_irrigation_minute){
          serial_printdateandtime();
          Serial.print(cropname);
          Serial.println("...Automation Irrigation...STAGE1...Successful");
          automation_irrigation_minute += automation_irrigation_interval;
          ditch_readlevel(); //Read ditch level
          if(ditch_waterlevel >= DITCH_STOPIRRIGATION){
            irrigation_stop(); 
          }
          if(ditch_waterlevel <= DITCH_STARTIRRIGATION){
            irrigation_start();
          }
        }
        if(field_watertable_depth <= CROPNAME_STARTIRRIGATION_STAGE1){
          serial_printdateandtime();
          Serial.print(cropname);
          Serial.println("...Automation Irrigation...STAGE1...COMPLETE");
          drainage_controlleddown();
          automation_drainage = false;
          automation_irrigation = false;
        }
      }
    break;
    case STAGE2:
     
    break;
    case STAGE3:
      
    break;
    case FALLOW:
      Serial.print(cropname);
      Serial.println("...FALLOW...Successful");
      drainage_controlleddown();
    break;
    default:
      Serial.print(cropname);
      Serial.println(",DEFAULT,STAGE SWITCH");
      automation_decision_forced = true;
      automationready = true;
    break;
 }

What type of Arduino do you use?

It needs to be a “crappy” string because the const’s need to be manipulated easily & quickly without going through almost 3,000 lines of code.

“needs” ? “String” or “string”?

Serial.println("...Automation Decision...STAGE1...DEFAULT of START IRRIGATION!");

Have you read about using the F() macro?
You should.

Whandall: What type of Arduino do you use?

Mega 2560

AWOL: "needs" ? "String" or "string"?

Serial.println("...Automation Decision...STAGE1...DEFAULT of START IRRIGATION!");

Have you read about using the F() macro? You should.

I did not realize there was a difference in string & String... I will look into that.

I will also read into F() macro.

Thank you so much!

I would not use Strings on a Mega, its RAM is too small. Your program will be deterministic and more solid if you switch to strings (char arrays).

void automation(int stage1, int stage2, int stage3){
 if(stage1 > automation_daysfrompd()) stage = STAGE1;
  else if(stage2 > automation_daysfrompd()) stage = STAGE2;
  else if(stage3 > automation_daysfrompd()) stage = STAGE3;
  else stage = FALLOW;
automation(COTTON_STAGE1, COTTON_STAGE2, COTTON_STAGE3);
automation(WHEATSOYBEAN_STAGE1, WHEATSOYBEAN_STAGE2, WHEATSOYBEAN_STAGE3);

Whandall:
I would not use Strings on a Mega, its RAM is too small.
Your program will be deterministic and more solid if you switch to strings (char arrays).

Whandall:
I would not use Strings on a Mega, its RAM is too small.
Your program will be deterministic and more solid if you switch to strings (char arrays).

For debugging at the moment. Thanks for the tip though.

For debugging at the moment. Thanks for the tip though

To me that does not make sense.

You write the code using Strings and debug it
Then you change it, which mean that it must be debugged again.

Where is the sense in that ?