I am encountering a math problem with my particle, I need to divide pulses by 88 to get liters on my flow meter. if I use the inter value it reports to the console the pulses, but when i try to divide by 88 it just reports a 0.000.
what could be the problem.
int LEDpin = D7;
unsigned long publish_delay = 20000;
unsigned long LastPublish = 0;
int state = LOW;
unsigned long WaterPulseCount = 0;
void setup()
{
pinMode(LEDpin, OUTPUT);
pinMode(A1,INPUT_PULLUP);
attachInterrupt(A1,WaterInterupt, CHANGE);
}
void loop() {
unsigned long now = millis();
digitalWrite(LEDpin,state);
if((now - LastPublish) < publish_delay)
{
double liters = WaterPulseCount / 88;
Particle.publish("data",String(liters));
LastPublish = millis();
delay(1000);
WaterPulseCount = 0;
}
}
void WaterInterupt()
{
state = !state;
WaterPulseCount++;
}
When both operands in a division are integers then the result will also be an integer, irrespective of the target variable which should receive the result after the fact.
You can do this to force a floating point division
This is also a reason why I rarely put “magic” constants in executed lines of code but instead as their own constant variables. This way you can better enforce your expectations for how that value will be used.
Alternatively, in a more generic case, and especially when sharing code amongst other people, there is a danger that if someone changes the magic constant number, that they might forget the .0. For that reason I would also have a slight preference for an explicit typecast, such as: