So, it's only a really basic program to allow the user to enter Athlete's times in a triathlon, with the fastest time being saved and, when the user wants too, they can exit the loop and have the fastest time displayed. However, I can't get my count to increment correctly, and furthermore when I set count to 1 (As their can't really be an "athlete 0") it updates the vale for FastestTime every time through the loop, instead of only when the current total is larger than FastestTime.
I have been beaten by a very basic program and it's a bit depressing really, so if anyone has any clues as to why this is happening and any possible fixes I'd be much obliged, as I can't fathom whats wrong with it.
int swimS = 0;
int swimM = 0;
int swimH = 0;
int bikeS = 0;
int bikeM = 0;
int bikeH = 0;
int runS = 0;
int runM = 0;
int runH = 0;
long total = 0;
long Swim = 0;
long Bike = 0;
long Run = 0;
int call = 0;
const int ConInt1 = 60;
const int ConInt2 = 3600;
int ExitLoop = 0;
long FastestTime = 0;
long FastestAthlete = 0;
int count = 0;
int FnInput(){
int x;
x = 0;
while (!Serial.available()) {
;
}
x = Serial.parseInt();
return x;
}
int FnOutput(){
Swim = swimS + (swimM * ConInt1) + (swimH * ConInt2);
Bike = bikeS + (bikeM * ConInt1) + (swimH * ConInt2);
Run = runS + (runM * ConInt1) + (runH * ConInt2);
total = Swim + Bike + Run;
Serial.begin(9600); ExitLoop = 0; count = 1; do { Serial.println("Please enter the number of seconds for the Swimming:"); swimS = FnInput(); Serial.println("Please enter the number of minutes for Swimming:"); swimM = FnInput(); Serial.println("Please enter the number of hours for swimming"); swimH = FnInput(); Serial.println("Please enter the number of seconds for the Biking:"); bikeS = FnInput(); Serial.println("Please enter the number of minutes for Biking:"); bikeM = FnInput(); Serial.println("Please enter the number of hours for Biking:"); bikeH = FnInput(); Serial.println("Please enter the number of seconds for the Running:"); runS = FnInput(); Serial.println("Please enter the number of minutes for Running:"); runM = FnInput(); Serial.println("Please enter the number of hours for Running:"); runH = FnInput(); FnOutput(); if (count = 1){ * FastestTime = total;* * FastestAthlete = count;* } if (FastestTime < total){ * FastestTime = total;* * FastestAthlete = count;* } count++; Serial.println(FastestTime); Serial.println(FastestAthlete); Serial.println(count); Serial.println("Enter another athlete?(Enter 0 to continue or 1 to exit)"); ExitLoop = FnInput(); } while (ExitLoop == 0); * Serial.print("The fastest athlete was no.");* * Serial.print(FastestAthlete);* * Serial.print(" with a total time of ");* * Serial.print(FastestTime / 3600);* * Serial.print("hr ");* * Serial.print((FastestTime / 60) % 60);* * Serial.print("min ");* * Serial.print(FastestTime % 60);* * Serial.print("sec.");* } void loop() { }
Re reading the instructions won't achieve anything as this is what the instructions tell me to do, it's just implementing it and getting around this error that's my problem not understand the instruction set.
If I put count++ inside of a loop, void setup or no void setup, surely that loop will run and count will be incremented by 1 each time?
Not really sure what you meant by that to be honest
Yep, well, that just happened.
I used Visual Basic before I started programming with the Arduino, so the whole double equals is a little bit new to me but yeah, I am not going to tell you how long I spent staring at that code but thank you very much for your help and I can say I will 100% never make that mistake again...
Yeah, you will. And, you'll again spend a bunch of time figuring out why the code doesn't work. It won't even be intentional when it happens.
I've been doing C and C++ coding for 30 years, on a daily basis. Just last week, I made that mistake, and spent 2 hours debugging the code, because I KNOW better than to use = in place of ==.
Oh yes you will! The only way to avoid ever making this mistake again is to stop coding. Especially if you start using other languages where "==" is considered a syntax error.