Hi,
I have been working on an assignment where the task is to collect user input from 3 different events, convert them all to seconds and then display them all in an array (and multiply them back up to display in he correct format) , along with the overall total.
//long variables to display overall times in the array
long Swim = 0;
long Bike = 0;
long Run = 0;
long total = 0;
void setup() {
Serial.begin(9600);//open port at this speed
//swimming
Serial.println("Swimming Details- ");
Serial.println("how many hours?");
int swim_hours;
while (!Serial.available()) {
//wait for a user to enter a value
;
}
swim_hours = Serial.parseInt(); //getting user hours
Serial.println("how many minuites?");
int swim_mins;
while (!Serial.available()) {
;
}
swim_mins = Serial.parseInt(); //getting user mins
Serial.println("how many seconds?");
int swim_secs;
while (!Serial.available()) {
;
}
swim_secs = Serial.parseInt(); //getting user seconds
//biking
Serial.println();
Serial.println("Biking Details- ");
Serial.println("how many hours?");
int bike_hours;
while (!Serial.available()) {
//wait for a user to enter a value
;
}
bike_hours = Serial.parseInt();
Serial.println("how many minuites?");
int bike_mins;
while (!Serial.available()) {
;
}
bike_mins = Serial.parseInt();
Serial.println("how many seconds?");
int bike_secs;
while (!Serial.available()) {
;
}
bike_secs = Serial.parseInt();
//running
Serial.println();
Serial.println("Running Details- ");
Serial.println("how many hours?");
int running_hours;
while (!Serial.available()) {
//wait for a user to enter a value
;
}
running_hours = Serial.parseInt();
Serial.println("how many minuites?");
int running_mins;
while (!Serial.available()) {
;
}
running_mins = Serial.parseInt();
Serial.println("how many seconds?");
int running_secs;
while (!Serial.available()) {
;
}
running_secs = Serial.parseInt();
Serial.println();
//converting the user input (time) into seconds
Swim = swim_hours * 3600 + swim_mins * 60 + swim_secs;
Bike = bike_hours * 3600 + bike_mins * 60 + bike_secs;
Run = running_hours * 3600 + running_mins * 60 + running_secs;
total = Run + Swim + Bike;
String NamesArray[] = {"Swim Time: ", "Bike Time: ", "Run Time: ", "Total Time: "}; // creating string array for names
int SecondsArray[4]; //creating int array for values
SecondsArray[1] = Swim;
SecondsArray[2] = Bike;
SecondsArray[3] = Run;
SecondsArray[4] = total;
//printing array of the 3 events and dividing to conver back to correct format from seconds
int i = 0;//creating variable i
for(i=0;i<4;i++){//if i is less than 4 increment i by 1
Serial.println(NamesArray[i] + (SecondsArray[i] / 3600) + "hr " + ((SecondsArray[i] / 60) % 60) + "min " + (SecondsArray[i] % 60) + "sec.");//matching names array with data to display in array
}
}
I have included an attachment of the output in the serial monitor, it seems to be a problem with swim and for some reason the hours have not added up correctly in the final total either.
Thanks in advance for any help offered!