0

I am using the Arduino IDE to write code and am trying to understand the namespace stuff. My thought is, is there a way to shorten the many places (in my code) where I have things like:

Serial.print("a="); Serial.print(a); Serial.print(" b="); Serial.println(b);

to something shorter like:

S.print(...

or

sprint(...

Can it be done?

I tried using String concatenation but it is very limited and expensive. That is just adding one

String s;

to my code at the global level increased the download size by 1482 bytes. And you can't do something like:

Serial.print("a=" + a); Serial.println(" b=" + b);

because it cant handle starting a concatenation with a literal string.

Any thoughts welcome.

Harvey
  • 2,062
  • 2
  • 21
  • 38

2 Answers2

4

Arduino uses the C++ language. It is not considered good practice, but you could use a preprocessor macro:

#define sprint Serial.print

Community
  • 1
  • 1
joews
  • 29,767
  • 10
  • 79
  • 91
-1

You could use a pointer and member de-reference operator, like this:

HardwareSerial *my_device;

void setup()
{
  my_device->begin(9600);
  delay(100);
}

void loop()
{
  if (my_device->available())
  {
    int r = my_device->read();
    // etc.
  }
}
Renato Aloi
  • 300
  • 2
  • 9