Firstly, if my code is awful, please be gentle, I'm very new lol. I'm trying to write a code where the user inputs a number between 8.0 and 12.0. Each value (8.0, 8.1, 8.2, 8.3, etc..) has two times associated with it. So, when the user inputs their number, I want the right associated time to print out.
The code I wrote works with most of the numbers. But for some numbers (e.g. 8.4), it doesn't print out anything and it loops back to the beginning. Any idea why?
Also, is there a better way to do this?
int user_choice;
float linespeed[] = {8.0, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9.0, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9, 10.0, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11.0, 11.1, 11.2, 11.3, 11.4, 11.5, 11.6, 11.7, 11.8, 11.9, 12.0};
String starttime[] = {"1:35", "1:36", "1:37", "1:38", "1:39", "1:39", "1:40", "1:41", "1:42", "1:43", "1:43", "1:44", "1:45", "1:45", "1:46", "1:47", "1:47", "1:48", "1:49", "1:49", "1:50", "1:51", "1:51", "1:52", "1:52", "1:53", "1:53", "1:54", "1:54", "1:55", "1:55", "1:56", "1:56", "1:57", "1:57", "1:58", "1:58", "1:59", "1:59", "2:00", "2:00"};
String stoptime[] = {"12:27", "12:28", "12:30", "12:31", "12:32", "12:33", "12:35", "12:36", "12:37", "12:38", "12:39", "12:40", "12:42", "12:43", "12:44", "12:45", "12:46", "12:47", "12:48", "12:48", "12:49", "12:50", "12:51", "12:52", "12:53", "12:54", "12:55", "12:55", "12:56", "12:57", "12:58", "12:58", "12:59", "1:00", "1:01", "1:01", "1:02", "1:03", "1:03", "1:04", "1:04"};
float user_num;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Enter 1 for color change times, enter 2 for times to stop hanging: ");
while (!Serial.available()){
}
user_choice = Serial.parseInt();
if (user_choice == 1){
Serial.println("Enter the fastest RED line speed (8.0 - 12.0): ");
while (!Serial.available()){
}
user_num = Serial.parseFloat();
for(int i = 0; i < 41; i++){
if (user_num == linespeed[i]){
Serial.println(starttime[i]);
}
}
}
if (user_choice == 2){
Serial.println("Enter the slowest RED line speed (8.0 - 12.0): ");
while (!Serial.available()){
}
user_num = Serial.parseFloat();
for(int j = 0; j < 41; j++){
if (user_num == linespeed[j]){
Serial.println(stoptime[j]);
}
}
}
}