Displaying rounded irrational numbers

I'm not very experienced with Arduino, so I'm a little stupid, please bear with me

I'm trying to create a pulse counter which takes the digital pulses from a flow meter and converts it to gallons per minute and displays the flow rate on an LCD. I have been able to get it to detect when and count pulses, but I've realized that when I try to convert my pulses per second into gal/min, any time I have an irrational number, the LCD and serial monitor just display the first integer and then zeroes after the decimal point.

I have included my code and a screenshot of the test circuit I'm using to prototype it on TinkerCAD.

#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //Initialize the variables for LCD
int count = 0; //Pulse Count
float gpm;
unsigned long start; //Start and end times to determine when 1 sec has passed
unsigned long end;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup(){
  Serial.begin(9600);
  pinMode(7,INPUT); //Identify Digital Input
  pinMode(8,OUTPUT);

  
  lcd.begin(16,2); //Set up the LCD begin to display static text
  lcd.print("Flow");
  lcd.setCursor(5,1);
  lcd.print("gal/min");
}

void loop(){
  digitalWrite(8,HIGH);
  start = millis(); //sets start time equal to the current clock time
  end = start;
  
  while ((end-start) < 1000){//Only allows the loop to run for a second
    if (digitalRead(7) == HIGH){ //Checks to see if the input has changed from HIGH to LOW
      delay(2);
      if (digitalRead(7) == LOW){
        count = count + 1; //Adds to count for every pulse detected
      }
    }
    end = millis();
  }
  gpm = (count * 60) / 180;
  Serial.println(count);
  lcd.setCursor(0,1);
  Serial.println(gpm,7);
  //lcd.print(gpm);
  count = 0;//Resets count to 0, use pulses/sec to convert to gal/min
}

your formula contains only integers, according to C rules it calculated as integer regardless the fact, that the final variable is float

Force the controller to calculate whole line as float by changing integer 60 to float 60.0:

And, since they are integers, the result of that division can't possibly be Irrational, by definition.

1 Like

to add to @b707, I would also suggest simplifying the formula from:

gpm = (count * 60) / 180;

to

gpm = count / 3.0;

hope that helps...

Thank you, that worked!

I would, but 180 is just a test value. The value I will actually need is 1417, so it won't be reducible. Thank you for the suggestion though!

the compiler optimizes all constant expressions.

it's better to express all your constants and allow the compiler to factor it. then it's not a mystery how the values are derived. how did you arrive at the magic number 1417?

It's the number defined as the number of pulses that represents a gallon from the sensor I'm using. It's not defined by me, it's what the manufacturer chose.

then you should assign it to a named constant like pulsesPerGallon.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.