Need help with output

void setup() {
  Serial.begin(9600);
  
 // SWIM TIMES

  Serial.println("Enter Swim Seconds");
  
 int totalSwimSeconds;
 while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }
 totalSwimSeconds = Serial.parseInt();

  Serial.println("Enter Swim Minutes");
 
 int totalSwimMinutes;
 while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }
 totalSwimMinutes = Serial.parseInt();

  Serial.println("Enter Swim Hours");
 
  int totalSwimHours;
 while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }
 totalSwimHours = Serial.parseInt();
 
 Serial.println();
 
int swimResult;
swimResult = totalSwimSeconds + totalSwimMinutes * 60 + totalSwimHours * 3600;

 Serial.print (" Total Time In Seconds: "); 
 Serial.println(swimResult);

int hourSwimResult;
hourSwimResult = totalSwimSeconds + totalSwimMinutes * 60 + totalSwimHours * 3600;
 
 Serial.print (" Hours: ");
 Serial.println(hourSwimResult / 3600 );

int minuteSwimResult;
minuteSwimResult = totalSwimSeconds + totalSwimMinutes * 60 + totalSwimHours * 3600;

 Serial.print (" Minutes: ");
 Serial.println(minuteSwimResult % 3600 / 60 );

int secondSwimResult;
secondSwimResult = totalSwimSeconds + totalSwimMinutes * 60 + totalSwimHours * 3600;

 Serial.print (" Seconds: ");
 Serial.println(secondSwimResult % 60);

 Serial.println();
  
// BIKE TIMES

 Serial.println("Enter Bike Seconds");
  
 int totalBikeSeconds;
 while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }
 totalBikeSeconds = Serial.parseInt();

  Serial.println("Enter Bike Minutes");

 int totalBikeMinutes;
 while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }
 totalBikeMinutes = Serial.parseInt();

  Serial.println("Enter bike Hours");
 
 int totalBikeHours;
 while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }
 totalBikeHours = Serial.parseInt();
 
  Serial.println();

int bikeResult;
bikeResult = totalBikeSeconds + totalBikeMinutes * 60 + totalBikeHours * 3600;

 Serial.print (" Total Time In Seconds: "); 
 Serial.println(bikeResult);

int hourBikeResult;
hourBikeResult = totalBikeSeconds + totalBikeMinutes * 60 + totalBikeHours * 3600;
 
 Serial.print (" Hours: ");
 Serial.println(hourBikeResult / 3600 );

int minuteBikeResult;
minuteBikeResult = totalBikeSeconds + totalBikeMinutes * 60 + totalBikeHours * 3600;

 Serial.print (" Minutes: ");
 Serial.println(minuteBikeResult % 3600 / 60 );

int secondBikeResult;
secondBikeResult = totalBikeSeconds + totalBikeMinutes * 60 + totalBikeHours * 3600;

 Serial.print (" Seconds: ");
 Serial.println(secondBikeResult % 60);

 Serial.println();


// RUN TIMES

 Serial.println("Enter Run Seconds");
  
 int totalRunSeconds;
 while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }
 totalRunSeconds = Serial.parseInt();

  Serial.println("Enter Run Minutes");
 
 int totalRunMinutes;
 while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }
 totalRunMinutes = Serial.parseInt();

  Serial.println("Enter Run Hours");
 
 int totalRunHours;
 while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }
 totalRunHours = Serial.parseInt();

 Serial.println();
 
int runResult;
runResult = totalRunSeconds + totalRunMinutes * 60 + totalRunHours * 3600;

 Serial.print (" Total Time In Seconds: "); 
 Serial.println(runResult);

int hourRunResult;
hourRunResult = totalRunSeconds + totalRunMinutes * 60 + totalRunHours * 3600;
 
 Serial.print (" Hours: ");
 Serial.println(hourRunResult / 3600 );

int minuteRunResult;
minuteRunResult = totalRunSeconds + totalRunMinutes * 60 + totalRunHours * 3600;

 Serial.print (" Minutes: ");
 Serial.println(hourRunResult % 3600 / 60 );
 

int secondRunResult;
secondRunResult = totalRunSeconds + totalRunMinutes * 60 + totalRunHours * 3600;

 Serial.print (" Seconds: ");
 Serial.println(secondRunResult % 60);

 Serial.println(); 

  
 Serial.println ( "Total Result: ");
 
 Serial.println ();
 

 int totalHours;
 int totalMinutes;
 int totalSeconds;

 
 totalHours = totalMinutes % 60 + totalSwimHours + totalBikeHours + totalRunHours;

 Serial.print("Hours: ");
 Serial.println(totalHours);
 


 totalMinutes = totalSeconds % 60 + totalSwimMinutes + totalBikeMinutes + totalRunMinutes / 60;

 Serial.print("Minutes: "); 
 Serial.println (totalMinutes);


 
 totalSeconds = totalSwimSeconds + totalBikeSeconds + totalRunSeconds / 60;
 
 Serial.print ("Seconds: ");
 Serial.println (totalSeconds);
 


// input these numbers
//  Sec/   Min/   Hour 
//  59    50     0 
//  59    170    0 
//  59    110    0

 
}








void loop() {
  // put your main code here, to run repeatedly:

}

I have been given an assignment to create a code/algorithm that enables the user to enter a time in Hours, Minutes and Seconds for 3 different events, Run,Swim,Bike.

My problem is I can’t seem to get the final output to show correctly, It’s showing more like this

Hours : 0
Minutes : 190
Seconds : 125

Instead of this

Hours : 3
Minutes : 12
Seconds : 05

If someone could help me figure this out and tell me what the solution was along the way that would be great.

Bare in mind a lot of code has been repeated and badly done, because as part of the assignment we are not allowed to use. loops,if, etc .

Thank you in advance.

-Lucky

OutPut_Problem.ino (4.59 KB)

As someone pointed out elsewhere on this site, the art of a good answer begins with a good question, which yours really is not. You should always give a sample of the input data and then give the actual output data, followed by what the output data should look like. Otherwise, it's more difficult to understand the nature of the problem.

Also, it would seem to me that you should be using loop() in the program rather than having everything in setup().

What part of the question don’t you understand?

I agree I have badly worded this question, but you have obviously not read the whole thing as I have clearly started what the output is and what it should be and right at the end I have very clearly stated as part of the assignment we are not allowed to use loops. Please give it a re-read and run the code yourself and let me know what you think.

I have been given an assignment

What part of that didn't you understand?

while (!Serial.available()) {
 //wait for a the user to enter a value
 ;
 }

A loop?
Fail.

It wasnt really a question in the assignment to be fair it was more like here you go make an algorithm.

How can I explain this?

Basically we are given three sets of times and a total.

Swim 0h 50m 59s
Run 0h 110m 59s
Bike 0h 170m 59s
Total 5h 32m 57s

The user needs to be able to input these times into the console and have the output look something like this

Hour : 5
Minutes : 32
Seconds : 57

However my seconds aren’t carrying over to minutes and my minutes aren’t carrying over to hours, so it’s outputting like this

Hour : 0
Minutes : 330
Seconds : 234

I don’t know how I would accomplish this because my knowledge is very limited on this subject and it is one of our first assignments.

Again I can NOT use the loop function hence the repeated code in some areas.

Why aren't the academic staff helping you?
What are you paying them for?

Why don't you cut your code down to a single function to return hours/minutes/seconds

Let’s just say they aren’t the most approachable types of people.

But I mean I would have assumed someone in the arduino community could offer some help.

And what do you mean by that?

Just have the code output the total instead of everything that’s inputted?

Let's just say they aren't the most approachable types of people.

Let's just say to them "why don't I take my business elsewhere?" ?

Well, that’s not really an option I’m afraid. I chose to study this now I’m stuck with it, I’m new to this language, so I’m not going to know everything. I don’t really talk to anyone I’m class and those that I do talk to are useless. Trying to get hold of a lecturer or a straight answer from one is almost impossible.

So, I’m really counting on the arduino community to help me out here and actually help me learn something instead of throwing ridicule my way, because from the way people who study computer science act it makes me and many other not want to go near the subject.

Why don't you concentrate on one set of values, and print some intermediate results?

LuckyCloud:
What part of the question don’t you understand?

I agree I have badly worded this question, but you have obviously not read the whole thing as I have clearly started what the output is and what it should be and right at the end I have very clearly stated as part of the assignment we are not allowed to use loops. Please give it a re-read and run the code yourself and let me know what you think.

I did read what you wrote and you showed me two outputs, but in neither case did you show the inputs that produced it. It does me no good to run your code if I don't know what inputs to enter. Learn the lesson: State the inputs and show the outputs and tell us what the output should be for those inputs.

Think about AWOL's suggestion:

Why don't you cut your code down to a single function to return hours/minutes/seconds

Pass in the total number of minutes and pointers to hours, minutes, and seconds and have a single function work it out.

Serial.println ( "Total Result: ");
 
 Serial.println ();
 
 int totalHours;
 int totalMinutes;
 int totalSeconds;
 
 totalHours = totalMinutes % 60 + totalSwimHours + totalBikeHours + totalRunHours;

 Serial.print("Hours: ");
 Serial.println(totalHours);
 
 totalMinutes = totalSeconds % 60 + totalSwimMinutes + totalBikeMinutes + totalRunMinutes / 60;

 Serial.print("Minutes: "); 
 Serial.println (totalMinutes);

 totalSeconds = totalSwimSeconds + totalBikeSeconds + totalRunSeconds / 60;
 
 Serial.print ("Seconds: ");
 Serial.println (totalSeconds);

Your calculations are wrong when you work out the total hours/minutes/second for the three events.

You appear to know how to use integer division and modulo when you calculate the individual events and the individual event results appear correct.

You know the time in seconds for each of the three events. Add the three values up to get total time in seconds. Then reduce to hours/minutes/seconds with the same division and modulo calculations used in the individual events.