Go Down

Topic: Cannot pass a string to a function (Read 2153 times) previous topic - next topic

d82k

Jan 25, 2015, 04:06 pm Last Edit: Jan 25, 2015, 04:08 pm by d82k
Dear all,

I don't understand why this is not working:

Code: [Select]

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

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.

AWOL

Because print and println expect a character array as argument, not a String object. 
From Print.h
Code: [Select]
size_t print(const String &);


@OP:post your code.

PaulS

Code: [Select]
  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?

d82k

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! ;)

d82k

Code: [Select]
  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?

d82k

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

PaulS

Quote
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.

nickgammon

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

AWOL

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

Go Up