Cannot pass a string to a function

Dear all,

I don't understand why this is not working:

void readSensor(int iMCP, int vccPin, String strTag) {
  digitalWrite(vccPin, HIGH);
  
  [...]
  
  if (Serial.available() > 0)
  {
    Serial.print(" [*]     ID    : ");
    Serial.println(iMCP);
    Serial.print(" [*]     TAG   : ");
    Serial.println(strTag);
    Serial.print(" [*]     VccPin: ");
    Serial.println(vccPin);
  }
}

void loop() {
  [...]
  readSensor(MCPA, vccPin1, "A1");
  [...]
}

basically Serial.println(strTag); does not print "A1" or better, it was, than compiled again my code and now it does not. I tried to use readSensor(MCPA, vccPin1, strMyString); but the result is the same.

What am I doing wrong? Thank you, dk

Because print and println expect a character array as argument, not a String object. You can use the toCharArray() method to convert, but using the String class is a very inefficient, and memory-intensive, way of handling character strings on a processor with limited memory. Much better to just use good old-fashioned c character arrays.

Regards, Ray L.

RayLivingston: Because print and println expect a character array as argument, not a String object.

From Print.h size_t print(const String &);

@OP:post your code.

  if (Serial.available() > 0)
  {
    Serial.print(" [*]     ID    : ");
    Serial.println(iMCP);
    Serial.print(" [*]     TAG   : ");
    Serial.println(strTag);
    Serial.print(" [*]     VccPin: ");
    Serial.println(vccPin);
  }

Why would you print this data only if there is serial data to be read? Why don't you then read the data?

RayLivingston: Because print and println expect a character array as argument, not a String object. You can use the toCharArray() method to convert, but using the String class is a very inefficient, and memory-intensive, way of handling character strings on a processor with limited memory. Much better to just use good old-fashioned c character arrays.

Regards, Ray L.

Thank you, I solved passing a char and a int so I can also optimize the code! ;)

PaulS:

  if (Serial.available() > 0)

{
    Serial.print(" []    ID    : “);
    Serial.println(iMCP);
    Serial.print(” [
]    TAG  : “);
    Serial.println(strTag);
    Serial.print(” [*]    VccPin: ");
    Serial.println(vccPin);
  }



Why would you print this data only if there is serial data to be read? Why don't you then read the data?

Hum… actually this is just a piece I’m reading it as well…
Anyway is there a way to make the arduino do the Serial.print only if there is a serial connection?

RayLivingston: Because print and println expect a character array as argument, not a String object. You can use the toCharArray() method to convert, but using the String class is a very inefficient, and memory-intensive, way of handling character strings on a processor with limited memory. Much better to just use good old-fashioned c character arrays.

Regards, Ray L.

BTW why sometimes Serial.print(myString) it works and prints the string? Thank you, dk

Anyway is there a way to make the arduino do the Serial.print only if there is a serial connection?

No. The chip can't tell if anything is actually listening to the serial port. If nothing is, the data is simply shifted out onto the floor. Occasionally, you should vacuum up the spilled bits.

Or put a "bit bucket" under it.

I'm not joking: http://en.wikipedia.org/wiki/Bit_bucket

There was an OS that had a device "BB:" - can't remember what it was - RSX11?