reduce number of Serial.print

Hello!

I use multiple times Serial.print in my code like this:

      //Tijd
      Serial.println("");
      Serial.print("local time: ");
      Serial.print(localtimeA);
      Serial.print(":");
      if (localtimeB < 10 ) {
      Serial.print('0');}
      Serial.print(localtimeB);
      Serial.println("");
      
      //sensor1
      Serial.println("");
      Serial.print("Sensor1 ");       
      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.print(" %\t ");
      Serial.print("Temperature: ");
      Serial.print(temp_c);
      Serial.println(" *C");
      
      //sensor2    
      Serial.print("Sensor2 ");    
      Serial.print("Humidity: ");
      Serial.print(humidity2);
      Serial.print(" %\t ");
      Serial.print("Temperature: ");
      Serial.print(temp_c2);
      Serial.print(" *C");
      Serial.println("");
      
      //sensor3   
      Serial.print("Sensor3 ");    
      Serial.print("Humidity: ");
      Serial.print(humidity3);
      Serial.print(" %\t ");
      Serial.print("Temperature: ");
      Serial.print(temp_c3);
      Serial.print(" *C");
      Serial.println("");

Is there a way to shorten this code?

Thanks in advance!

Jan.

Is there a way to shorten this code?

Yes, but there are tradeoffs. Reducing the number of Serial.print() statements means printing more stuff in each call, which means that the stuff to print must be converted to strings and stored before printing.

Look at sprintf() if you want to go that route.

Keep in mind that sending the data as one string will not make the serial data get there any faster. So, the tradeoff is increased memory utilization [u]all the time[/u] to reduce the number of lines of code that you type [u]one time[/u].

Without seeing all the code, I don’t know if this is feasible, but you could store all the data in an array (rather than individual variables), and then use a “for” loop to read and print the data. Then you would only have to write the Serial.print() code once, and just step through the array.

PaulS is much better at this than I am, but I don’t think the array technique would have the drawbacks he mentioned. You’re already storing the data somewhere, this would just take numerous variables and put them into one array. It IS up to you to remember what data is stored where.

I.E.:

for(int row = 0 ; row < 3 ; row++)
{
      Serial.println("");
      Serial.print("Sensor ");
      Serial.print(row+1);      //This might not be the right syntax, but the idea is to print 
                                          //row+1, which is the sensor the reading is from       
      Serial.print("Humidity: ");
      Serial.print(array[row][0]);
      Serial.print(" %\t ");
      Serial.print("Temperature: ");
      Serial.print(array[row][1]);
      Serial.println(" *C");
      Serial.println("");
}

Think array’s is indeed the way to go .

BTW if you press ALT (keep it pressed) and 0176 you get the degree symbol °

for(int row = 0 ; row < 3 ; row++)
{
      Serial.println();
      Serial.print("Sensor ");
      Serial.print(row+1);
      Serial.print("Humidity: ");
      Serial.print(array[row][0]);
      Serial.print(" %\t ");
      Serial.print("Temperature: ");
      Serial.print(array[row][1]);
      Serial.println(" °C\n");          // \n gives that extra newline
}

iegj: Hello!

I use multiple times Serial.print in my code like this:

...

Is there a way to shorten this code?

Yes, use the Streaming library:

http://arduiniana.org/libraries/streaming/

Or best of all (I think) write a function! eg

you code

      //sensor1
      Serial.println("");
      Serial.print("Sensor1 ");       
      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.print(" %\t ");
      Serial.print("Temperature: ");
      Serial.print(temp_c);
      Serial.println(" *C");
      
      //sensor2    
      Serial.print("Sensor2 ");    
      Serial.print("Humidity: ");
      Serial.print(humidity2);
      Serial.print(" %\t ");
      Serial.print("Temperature: ");
      Serial.print(temp_c2);
      Serial.print(" *C");
      Serial.println("");
      
      //sensor3   
      Serial.print("Sensor3 ");    
      Serial.print("Humidity: ");
      Serial.print(humidity3);
      Serial.print(" %\t ");
      Serial.print("Temperature: ");
      Serial.print(temp_c3);
      Serial.print(" *C");
      Serial.println("");
void printHumidity(int sensor,humidity,temp){
      Serial.print("Sensor");
      Serial.print(sensor);// my addition
then prints for the humidity temp etc

}

When ever you have code which is repeated use a function it is what they where invented for and it reduces the number of bugs you get.

Mark

Here is a short tutorial for sprintf:

http://liudr.wordpress.com/2012/01/16/sprintf/

Essentially you are choosing between format as you go (separate serial.print and using stream library) and deciding the formatting and get everything formatted in one shot (sprintf). The only significance is that sprintf lets you precisely control the number of digits for numbers and spaces so the output looks better in situations like time and other stuff. You don't want 12:0:0 then use sprintf to get 12:00:00

Thanks for the reactions! it was very useful. :smiley:

I decided to use the streaming library because it is super easy to use!

btw i have deleted the Serial.prints but i needed it also for some dataprints to SD

 File dataFile = SD.open(fileName, FILE_WRITE);
  if (dataFile) {  
    if (localtimeB < 10 ) {
        dataFile << "" << "Localtime: " << localtimeA << ":" << "0" << localtimeB << "\n" << endl;}
      else{
        dataFile << "" << "Localtime: " << localtimeA << ":" << localtimeB << "\n" << endl;}  
     dataFile << "" << "Sensor1 Humidity: " << humidity << " %\t " << "Temperature: " << temp_c << " *C\n" << endl; 
     dataFile << "" << "Sensor2 Humidity: " << humidity2 << " %\t " << "Temperature: " << temp_c2 << " *C\n" << endl; 
     dataFile << "" << "Sensor3 Humidity: " << humidity3 << " %\t " << "Temperature: " << temp_c3 << " *C\n" << endl;       
     dataFile.close();
  }

I couldn’t figure out how the sprintf works for the time to ad a 0, but i solved is with a if.

Jan

dataFile << “” << "Localtime: " << localtimeA << “:” << “0” << localtimeB << “\n” << endl;
What’s that highlighted part doing?

I couldn’t figure out how the sprintf works for the time to ad a 0, but i solved is with a if.

%d is the format specifier for an int. %2d says print the int using no less than 2 characters - the leading one can be a space. %02d says print the int using no less than 2 characters - the leading one will be a 0 if the value is less than 10.

I guess you didn't read my tutorial then. It specifically deals with the zeros. 8)

<< "\n" << endl;

The endl part adds a newline (endl = End Line). You don’t need to use that and “\n”.

 if (localtimeB < 10 ) {
        dataFile << "" << "Localtime: " << localtimeA << ":" << "0" << localtimeB << "\n" << endl;}
      else{
        dataFile << "" << "Localtime: " << localtimeA << ":" << localtimeB << "\n" << endl;}

think this if can be embedded in the stream ??? (never tried) ?? reducing ~~50% of the code

dataFile << "" << "Localtime: " << localtimeA << ":" <<  (localtimeB < 10 )?"0":"" << localtimeB << "\n" << endl;

update , probably needs () brackets around the if

dataFile << "" << "Localtime: " << localtimeA << ":" <<  ((localtimeB < 10 )?"0":"") << localtimeB << "\n" << endl;
        dataFile << "" << "Localtime: " << localtimeA << ":" << "0" << localtimeB << "\n" << endl;

Save RAM by using the F macro:

        dataFile << "" << F("Localtime: ") << localtimeA << F(":") << F("0") << localtimeB << endl;

Slightly off topiic BUT the UTFT lib does the float print w/leading zeros... myGLCD.printNumF(float,x,y,separator,numDig,fill char); or myGLCD.print(data,0,0,2,0) Will print a float at 0 , 0 with 2 places after the decimal and a 0 as a fill char...

Bob