converting var containing mins to hours and mins?

Just a quick question I have a variable that stores a number of minutes that the user has input. This is fine as I need to use the stored minutes for my calculations. However I have the minutes displayed on a lcd screen and I'd like to have the minutes displayed in hours and minutes format which I have completely forgotten how to do. I can just divide by 60 to get the hours but the minutes would still be in 10s ?

https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/modulo/

Divide by 60 for hours. Modulo for the remaining minutes.

groundFungus:
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/modulo/

Divide by 60 for hours. Modulo for the remaining minutes.

Thanks for the reply I don't know what Modulo does any chance of showing me ?

moderategamer:
Thanks for the reply I don't know what Modulo does any chance of showing me ?

You didn't even do the effort of reading the link he provided?

moderategamer:
Thanks for the reply I don't know what Modulo does any chance of showing me ?

https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/modulo/

PieterP:
You didn't even do the effort of reading the link he provided?

I did read the link and I tried it. I can't get it to work because it doesn't work with floats. I can get it to work with integers but it doesn't display the minutes because it's an int. hence why I asked for a working example.

moderategamer:
I did read the link and I tried it. I can't get it to work because it doesn't work with floats. I can get it to work with integers but it doesn't display the minutes because it's an int. hence why I asked for a working example.

Well, floats are not an appropriate representation for HMS time. When would it be 10.224 : 0.33 ?

Also, I believe the examples given with the link are "working". :slight_smile:

aarg:
Well, floats are not an appropriate representation for HMS time. When would it be 10.224 : 0.33 ?

Also, I believe the examples given with the link are "working". :slight_smile:

You can't have decimal places with Integers which surely I will need for say example 1.30 Hrs if I can't have decimals then there would be no mins no?

like I said I don't know how to use it in this context to get what I want so was looking for some help.

So, you have float minutes that you want to separate into integer hours and the remaining float minutes?

moderategamer:
You can't have decimal places with Integers which surely I will need for say example 1.30 Hrs if I can't have decimals then there would be no mins no?

like I said I don't know how to use it in this context to get what I want so was looking for some help.

By 1.30 hours you mean 1 hour 30 minutes or 1 hours 18 minutes?

groundFungus:
So, you have float minutes that you want to separate into integer hours and the remaining float minutes?

No I originally had an int which stored my minutes I only mentioned the floats as I thought I would need to use it to have the decimal point but at this point I don't know. All I want is to be able to have my minutes be displayed as hours and minutes through whatever means possible.

I only got my Arduino last week so it's all very new to me, I'm not aware of any way to remove the remainder left from the modulo out of an int to get my minutes so I assumed I would need to use a float to do the trick.

Just looking for some friendly help not to be shouted at or made fun of as you can see by my profile I'm new to this so a bit of slack would be nice. I'm just here to learn. This isn't directed at you BTW I'm just speaking generally to the thread.

arduino_new:
By 1.30 hours you mean 1 hour 30 minutes or 1 hours 18 minutes?

1hr 30mins

Perhaps you should start by posting the code you have (using code tags). Then explain what it does and what you want it to do differently.

moderategamer:
1hr 30mins

how about this:

float var = 1.30;

int hour = (int)var;

int minute = (var - hour) * 100;

This code will extract a float value to hour and minute. Of course, it does not account for seconds.

There's 209 lines of code so that could be difficult but I'll extract the key things and post them below.

it's simple as could be all I am doing is using a button to set the number of minutes stored in a variable.

int Total_time = 0;



void Buttons(){


int x;
x = analogRead (0);
  
    if (x < 80){
          
          delay(200);
         // lcd.print("right key");

         if (MenuNo == 1){
            Total_time = Total_time + multipliers;
            MenuItems();
          }
             
      
   }


   lcd.print(Total_time / 60);

these are the main elements that pertain to what I am looking to do obviously they would be inside functions and what not but I spared you the details A because the code is in very early development stage and is all over the place and B it should be enough for you to understand what is going on.

I don't know how to extract the numbers beyond the decimal place if I were using a float and I don't know hoe to get an int to display the time without decimals unless I combine 2 ints in my lcd print which would be fine ut I would still need to extract the mins from the float to do that so that's the problem I am facing.

You would think that there would be an easy way to convert to hours and mins without having to create a function to extract certain parts of the variable and then combine them.

arduino_new:
how about this:

float var = 1.30;

int hour = (int)var;

int minute = (var - hour) * 100;





This code will extract a float value to hour and minute. Of course, it does not account for seconds.

Thanks I'll give that a try.

Using float is totally the wrong way. I don't understand why you're not doing what @groundFungus suggested way back in Reply #1.

int Total_time = 0;
int hours, minutes;

hours = Total_time / 60;
minutes = Total_time % 60;
lcd.print(hours);
lcd.print(":")
lcd.print(minutes);
1 Like

gfvalvo:
Using float is totally the wrong way. I don't understand why you're not doing what @groundFungus suggested way back in Reply #1.

int Total_time = 0;

int hours, minutes;

hours = Total_time / 60;
minutes = Total_time % 60;
lcd.print(hours);
lcd.print(":")
lcd.print(minutes);

Thanks for the solution but I don't know why everyone in this forum has to be so up themselves. Obviously I didn't understand how to implement what he was saying which is why I asked for clarification. As you can see I'm very new to this and was only looking for some help not a lecture how much more simple would it have been to just write this post at the beginning and maybe explain how and why it works so that I can learn than all this attitude that's being directed at me. Aren't forums supposed to be about the community helping each other out, I'm a member of many forums but never have I seen so much snark in posts. I help out people all the time that are new to things which I know about and I never try to make them look silly I just try to help and maybe teach them a thing or two along the way.

once again this isn't directed at you but the thread in general I seemed to have been getting a lot of snark but only 2 of you actually had the solution.

In all fairness, your original problem statement wasn't really a model of clarity. You threw in a Red Herring early on talking about floats which didn't get clarified until Reply #10. Not until Reply #14 did we even see a little piece of your code, and only because I asked.

In general folks on this board are pretty patient and helpful with beginners. Nothing wrong with being a beginner, everyone was at one time. But, even beginners should be able to pose their questions in a clear, complete, and concise way. It's very frustrating to go through half a dozen back-and-forth replies before getting to the real issue.

BTW, you haven't seen snark and pedantry until you've headed over to the StackExchange forums.

gfvalvo:
In all fairness, your original problem statement wasn't really a model of clarity. You threw in a Red Herring early on talking about floats which didn't get clarified until Reply #10. Not until Reply #14 did we even see a little piece of your code, and only because I asked.

In general folks on this board are pretty patient and helpful with beginners. Nothing wrong with being a beginner, everyone was at one time. But, even beginners should be able to pose their questions in a clear, complete, and concise way. It's very frustrating to go through half a dozen back-and-forth replies before getting to the real issue.

BTW, you haven't seen snark and pedantry until you've headed over to the StackExchange forums.

Fair enough I could have been more clear by adding sample code, I just thought it was so basic it would be obvious by my description. But I get now that I should have added some code.

Regardless thanks for the example it helped a lot now I can get back to doing the more important work :slight_smile: