 # Basic math equations

Okay, I have two variables in my program.... runSize, which is the total number of cycles the program WILL run, and cycleCount, which is the total number it HAS run. I know that it runs 9 cycles per minute (close enough anyway) so I wrote this to estimate the minutes remaining:

``````int t = ((runSize - CycleCount) / 9);
``````

This gave me an approximate minutes remaining to complete the run. The only thing I didn't like was that, because it only displayed whole minutes, it went to 0 when there were still 8 cycles remaining. Not a big deal for what I'm using it for but I decided to change the code to this:

``````int t = ((runSize - CycleCount) / 9 + 1);
``````

This worked the same but it added one minute to the estimate so now the display reads 1 minutes remaining during the entire last minute then goes to zero as the run completes. So then I decided to try breaking the minutes down into hours and minutes using the following:

``````int t = ((runSize - CycleCount) / 9 + 1);
int h = (t / 60);
int m  = (t - (h * 60));
``````

My assumption was that because the first equation had returned a whole number that would stay true so h = (t / 60) would be a whole number, which of course would be the hours. Then I could use that number multiplied by 60 with the result subtracted from t to give the the "leftover" minutes.

But what I'm getting is the result of int t = ((runSize - CycleCount) / 9 + 1); showing up as the hours and the minutes are always zero. I thought I was using sound logic but I think I'm assuming something that isn't correct. Can you tell me where my flaw is?

try:

``````m = t % 60;
``````

…or force float.: ( (runSize - CycleCount) / 9.0);

From a user interface perspective, neither “minutes remaining = 1”, nor “minutes remaining = 0”, is very clear when there are actually 30 seconds remaining.

In a text interface, anyway, the best display would substitute “less than 1 minute remaining” for “minutes remaining = 0”. Easy to implement by testing for (t>0 and m<1).

But I don’t know if your display method can handle that.

aarg:
From a user interface perspective, neither “minutes remaining = 1”, nor “minutes remaining = 0”, is very clear when there are actually 30 seconds remaining.

In a text interface, anyway, the best display would substitute “less than 1 minute remaining” for “minutes remaining = 0”. Easy to implement by testing for (t>0 and m<1).

But I don’t know if your display method can handle that.

I agree but again for my purposes either 1 or 0 is acceptable. I’m displaying on a 4x20 lcd with other info so I am limited.

Just to be clear, in case I wasn’t previously, displaying 1 or 0 when it’s less than 1 minute isn’t my problem. Mentioning that probably just muddied the waters but I wanted to explain how I got to where I am. It’s converting a large (lets use 400 for an example) number of minutes into hours and minutes. I thought the additional code I wrote would A) divide 400 by 60 resulting in 6.66666, and then discard the .66666 leaving h=6. Then the next equation would subtract h (6) times 60 (result 360) from t (400) leaving 40 to plug into the minutes column. But those equations don’t appear to work as I would expect and I don’t know why. I would prefer not to use float as I don’t need the accuracy and don’t want to slow down the processor with unnecessary calculations.

But what I'm getting is the result of int t = ((runSize - CycleCount) / 9 + 1); showing up as the hours and the minutes are always zero. I thought I was using sound logic but I think I'm assuming something that isn't correct. Can you tell me where my flaw is?

Mark

???

``````void setup()
{
Serial.begin(9600);
int num=400;
Serial.print(num/60);
Serial.print(" mins ");
Serial.print(num%60);
Serial.print(" secs ");
}

void loop(){}
``````

My apologies for wasting all of your time. Mark was right, kind of.......but it wasn't the print statements either. I hadn't added int h = 0; int m = 0; to the top of my code. I fixed that and it works just as I hoped it would. Can someone explain what the % would do though? This doesn't make sense to me:

``````void setup()
{
Serial.begin(9600);
int num=400;
Serial.print(num/60);
Serial.print(" mins ");
Serial.print(num%60);
Serial.print(" secs ");
}

void loop(){}
``````

How about looking up "%" in the reference? It's there. It's the modulus operator (remainder after division).

Thanks aarg, both for the answer and the "figure it out yourself" way in which you delivered it. :-) Honestly I was just stuck on "it's a percent sign so how does 60% of the minutes help me?". I need to remember that things aren't always what they seem in the coding world. LOL