Go Down

Topic: Slow serial input response (Read 1 time) previous topic - next topic

Dan02

Jun 09, 2019, 04:19 pm Last Edit: Jun 09, 2019, 04:24 pm by Dan02
Hello,
I am facing a performance problem with my Arduino Mega 2560. My loop executes for 1 second roughly. This means that my response to the request sent by the PC would be as slow as 1 second or more. Note that I am not using the Delay() function. Could be interrupt used in this case? This is a part of my code:
Code: [Select]

void communication ()
{
    String serialInput = Serial.readString();
    serialInput.trim();
  
    if(serialInput == "Data")
    {
        String unit = "";
        if(thermometerC1)
        {
          unit = "C";
          if(sensVal11)
          {
            Serial.println(unit + "#" + temp + "#" + moistureLevel1 + "#" + !errorSignal5);
          }
          else
          {
            Serial.println(unit + "#" + temp + "#Off#" + !errorSignal5);
          }
        }
        else
        {
          unit = "F";
          if(sensVal11)
          {
            Serial.println(unit + "#" + temp2 + "#" + moistureLevel1 + "#" + !errorSignal5);
          }
          else
          {
            Serial.println(unit + "#" + temp2 + "#Off#" + !errorSignal5);
          }
        }
     }
     else if(charIndex(serialInput, '#') != -1) // Maybe here is the problem?===================
     {
        String inputArr[6];
        int valuesCount = 0;
        for(int i = 0; i < serialInput.length(); i++)
        {
          if(serialInput[i] != '#')
          {
            inputArr[valuesCount] += serialInput[i];
          }
          else
          {
            valuesCount++;
          }
        }
      
       ch11menuitem = inputArr[0].toInt();
       ch12menuitem = inputArr[1].toInt();
       timesValue = inputArr[2].toInt();
      
       if(inputArr[3] == "C")
       {
        thermometerC1 = true;
       }
       else
       {
        thermometerC1 = false;  
       }      

       wateringTimeValue = inputArr[4].toInt();
       timeoutBWatValue = inputArr[5].toInt();

       settingsSave();
     }
     else if(serialInput == "ResetDefaults")
     {
       resetDefaults();
     }
  }

  int charIndex (String input, char charLookup)
    {
      for(int i = 0; i < input.length(); i++)
      {
        if(input[i] == charLookup)
        {
          return i;
        }
      }
      return -1;
    }

    void serialEvent()
    {
      if (Serial.available()) {
        communication();
      }
    }


Every assistance would be appreciated,
Yordan Yordanov

sterretje

#1
Jun 09, 2019, 04:57 pm Last Edit: Jun 09, 2019, 04:57 pm by sterretje
readString blocks for one second to detetmine if you finished 'typing' ;)

You can get ideas from serial input basics.

It also frees you from the String class that can cause memory fragmentation.

Use strcmp to compare strings when using c-strings as in the provided link.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Dan02

Thank you @sterretje, the serial basics solved my problem. I appreciate your help.

Yordan

Go Up