Printing line numbers from serial output

Hi everyone

I have a question about count loops. Can anyone share some ideas on how I can “count” each line of data that is printed through the serial output?

While using a digital temperature sensor (TMP102), what I am trying to achieve is to get something like this via the serial output:
Line 1 Celsius: 27.12
Line 2 Celsius: 27.14
Line 3 Celsius: 27.11
etc. etc…

Any thoughts? I always have such a difficult time wrapping my head around counter loops…

Thanks so much! Below is the code that I have so far:

#include <Wire.h>
int tmp102Address = 0x48;

void setup(){
  Serial.begin(9600);
  Wire.begin();
}

void loop(){
  //print line number, and then in the same line the temperature reading
  serial.print("Line ");
  serial.print("X" );    //This is where the "X" would be replaced by the line number
  
  //to print the temperature reading:
  float celsius = getTemperature();
  Serial.print("Celsius: ");
  Serial.println(celsius);

  //Ideally, I would like to have output like:
  //  Line 1 Celsius: 27.12
  //  Line 2 Celsius: 27.14
  //  Line 3 Celsius: 27.11 etc. etc

}


float getTemperature(){
  Wire.requestFrom(tmp102Address,2); 

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4; 

  float celsius = TemperatureSum*0.0625;
  return celsius;
}
#include <Wire.h>
int tmp102Address = 0x48;
long X;

Hi AWOL

thanks for your response. However, I think that your response got somehow truncated, since it only showed the addition of a "long X;" in your suggested code.

Could you please re-try when you get a chance?

Thanks!!

It wasn't truncated, it was a kick start. If you remove the quotes around the X you posted, and add the characters ++ after it, see what happens.

(BTW, "Serial" is spelt with a capital "S")

Thanks AWOL! Your tip helped me figure out how to make the counter work. Here’s the working code that I got :slight_smile:

Thanks again!

#include <Wire.h>
int tmp102Address = 0x48;
int counter = 0;

void setup(){
  Serial.begin(9600);
  Wire.begin(); 
}

void loop(){
  //print line number, and then in the same line the temperature reading
  Serial.print("Line ");
  counter = counter + 1;
  Serial.print(counter);
  Serial.print(" " );   
  
  //to print the temperature reading:
  float celsius = getTemperature();
  Serial.print("Celsius: ");
  Serial.println(celsius);
}


float getTemperature(){
  Wire.requestFrom(tmp102Address,2); 

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4; 

  float celsius = TemperatureSum*0.0625;
  return celsius;
}

to get a nice layout you could consider using tabs:

  Serial.print("Line:\t");
  counter = counter + 1;
  Serial.print(counter);
  Serial.print("\t" );

Just a simple change:

Instead of:

  Serial.print("Line:\t");
  counter = counter + 1;
  Serial.print(counter);
  Serial.print("\t" );

try:

  Serial.print("Line:\t");
  Serial.print(++counter);
  Serial.print("\t" );