Percent (%) calculation

To display a bar, i want to calculate the percent of an operation

Sample time is stored in a variable "m2_sampleTime" in Minutes! When a new cycle is started, "m2_logStart" is filled with millis();

Since this updates a display and should not called over and over (flickering) i only call it per 2 seconds. Im calling this code, i added serial prints to debug.

    float m2_barCalc_float = (100/(m2_sampleTime*60000))*(millis()-m2_logStart);

    Serial.println(m2_barCalc_float);

    byte m2_barCalc = m2_barCalc_float;

    Serial.println(m2_barCalc);

In the normal operation this code would be followed by the display code, but thats not needed here.

I only need full % like 1% not 1.234% or so.

Serial Ouptut is always 0.00 followed by 0 for the byte?

Why and how to fix?

All your math is done as integers. You need to throw some floats in (60000.0, 100.0) to force the calculation to be performed as float.

What KeithRB said.

    float m2_barCalc_float = (100.0/(m2_sampleTime*60000.0))*(millis()-m2_logStart);
    Serial.println(m2_barCalc_float);
    byte m2_barCalc = m2_barCalc_float;
    Serial.println(m2_barCalc);

Rather than updating the display once per 2 seconds, update it when what it needs to display changes. Fir instance, if it shows a bar with 10 segments and an amount accurate to .1%, calculate how many bars and what you need to display, and don't update the display if that's the same as what's already there.

Also, don't clear and repaint the entire display. Only clear and repaint the rectangles you need.

PaulMurrayCbr: Rather than updating the display once per 2 seconds, update it when what it needs to display changes. Fir instance, if it shows a bar with 10 segments and an amount accurate to .1%, calculate how many bars and what you need to display, and don't update the display if that's the same as what's already there.

Also, don't clear and repaint the entire display. Only clear and repaint the rectangles you need.

Yes i know about this, i'm doing this on another part.

But in this specific part, i want to update each 2 seconds, because of other values that are displayed along with the bar.

Some of the values can be changed by 1 in 2 seconds, or by 1000 in 2 seconds, so a "updated when needed" would be very complex to implement.