Serial Printing Floats

The following code is a typical flow-meter, and more or less what I have been using without question for years. FlowRate is a float. What I don't understand is that the code shows the value broken into an int and a frac and printed as such with a decimal point in between, i.e. a math procedure followed by three separate Serial.print operations.

What's the value in doing this?

My DS18B20s return floats, and I simply print them as floats.

//Pro Mini flow rate feed for Mega
byte sensorInterrupt = 0;  // 0 = pin 2; 1 = pin 3
byte sensorPin       = 2; //
volatile byte pulseCount;
float calibrationFactor = 6.82;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;

void setup()
{
  Serial.begin(115200);
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);
  pulseCount          = 0;
  flowRate            = 0.0;
  flowMilliLitres          = 0;
  totalMilliLitres         = 0;
  oldTime             = 0;
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

void loop()
{
  if ((millis() - oldTime) > 1000)   // Only process counters once per second
  {
    detachInterrupt(sensorInterrupt);
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    oldTime = millis();
    flowMilliLitres = (flowRate / 60) * 1000;
    totalMilliLitres += flowMilliLitres;
    unsigned int frac;
   frac = (flowRate - int(flowRate)) * 10;
    Serial.print((int)flowRate);   // Print the integer part of the variable
    Serial.print('.');             // Print the decimal point
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
    Serial.print(int(totalMilliLitres / 1000));
    pulseCount = 0;
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
}

void pulseCounter()
{
  pulseCount++;
}

Maybe the writer only wanted 1 decimal place and didn’t know ( Serial.print(val,1):wink: I wrote something similar for the reason of trimming code size by eliminating floats & FP math. Was just a trivial experiment though.

/*
 LM35 thermometer, no floats, no delays
  http://www.ti.com/lit/gpn/lm35
*/
const byte sampleBin = 8, // number of samples for smoothing
           aInPin = A0;
const int fudge = 170; // adjust for calibration 
const int kAref = 1090, // analog ref voltage * 1000
          kSampleBin = sampleBin * 1000,
          tEnd = 5000; // update time in mS
int tempC,
    tempF;
    
uint32_t total,  // sum of samples
         tStart; // timer start
void setup()
{
  Serial.begin(9600);
  analogReference(INTERNAL); // use 1.1V internal ref
  analogRead(aInPin);
  for(int i = 0;i < sampleBin;i++) // for smoothing, fill total
    total += analogRead(aInPin);   // with sampleBin * current
                                   // reading
}
void loop()
{
  if(millis() - tStart > tEnd)
  {
    tStart = millis(); // reset timer 
    total -= (total / sampleBin); // make room for new reading
    total += analogRead(aInPin); // add new reading
    tempC = total * kAref / (kSampleBin + fudge);
    tempF = (tempC * 18 + 3200) / 10;
    Serial.print(analogRead(aInPin));
    Serial.print("\t");
    Serial.print(total); // sum of samples
    Serial.print("\t");
    prntTemp(tempC);
    prntTemp(tempF);
    Serial.println();
  }
}
void prntTemp(int temp){
  Serial.print(temp / 10); // whole degrees
  Serial.print(".");
  Serial.print(temp % 10); // tenths
  Serial.print("\t");
}

Ah, that might explain it. While the meter is accurate for quantity, reading the rate was a futile exercise and I eliminated it. Now I want it. I will see if I can found out what the display was actually showing. Maybe the code is archaic...

To get a stable reading of rate you may have to do an average of the last 8 or 10 readings, of course that will LP filter fast changes in rate.

Yes. I intend to do that for display, even if I use the data as it comes for power calculations.

Sounds like a hydro power project, I love it. :)

Not quite! It's waste water heat recovery.