Should I use methods? or nested loops?

Hi,

I'm designing a small car that follows the white line on an athletic track for training and I need to create 2 workout programs. 1st program=steady state, same speed during a given amount of time. 2nd program= interval training, alternate between sprint and rest. The user should be able to pick between either program, and then enter the followings: the speed and the time for the first program, the speed of the sprint, the speed of the "rest", the time of the sprint, and the time of rest for the interval training program. Should I use 2 methods? one void steady() and one void interval() and depending on what the user input is I can call the good method? or should I use nested loops? I'm a beginner with Arduino...

2 methods. At top of loop, check to see if something has happened that would cause a change - user press stopped, user pressed program change, whatever. Then continue the mode you were in if nothing changed:

void loop(){
// read buttons, decide if the mode you were in changed
// or process commands from RF remote
// or ...
:
:
switch(mode){
case 0:
// steady mode
:
break;

case 1:
// interval mode
:
break;
case 2:
// pause mode while user has a smoke while coach is in other side of track
:
break;
case 3:
// stop mode while user pants and catches  breath before starting a new mode
:
break;
} // end switch
} // end loop

Thank you! the switch statement makes sense to me and it seems like the best idea in this case. I only have 3 push buttons wired on my arduino board, basically I use 1 as an increase button, 1 as a decrease button, and 1 as a start (next mode) button. Should I start a count and once I hit start it takes that number and goes to the appropriate case?

It is generally good practice to write different functions / methods for different functionalities. So yes, I would recommend it. You can call each function in your switch statement.

Regarding the usage of three buttons to switch between modes: that seems fine to me. Although it kind of depends how many modes you are going to implement and how much they are related. But I think this will work.

Whatever functionalilty you wish to implement from the buttons. Just be sure to read them periodically enough so they seem responsive to the user - so no big delay() statements in your code. Use blink-without-delay and watch the time passing to determine when your next event is to happen.

I will try this and get back to you, thank you!

I tried the switch case statement with just a small message in each case which would print out on my lcd screen to see if I could access each case, but my lcd screen displays 1 when I run the program and doesn’t change if I press the increase or decrease button. Do you know why I cannot change my count?

#include <LiquidCrystal.h>    //Includes library from using the LCD display
   
 const int decButton=8;    //map pins to buttons
 const int incButton=9;    //map pins to buttons
 const int startButton=7;  //map pins to buttons
 int sB,iB,dB;             //values of buttons (start/increase/decrease)
   
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);      //map pins to lcd screen

 boolean brk=true;

 int count=1;
 int def=0;

void setup()
{
  Serial.begin(9600);
  pinMode(decButton, INPUT);
  pinMode(incButton, INPUT);
  pinMode(startButton, INPUT);
}

void loop()
{
     sB=digitalRead(startButton);  //set value of sB based on wheather or not the start button has been pressed
     iB=digitalRead(incButton);    //set value of iB based on wheather or not the increase button has been pressed
     dB=digitalRead(decButton);    //set value of dB based on wheather or not the decrease button has been pressed
do{
  if (iB==HIGH)
  {
    count++;
  }
  if (dB==HIGH)
  {
    count--;   
  }

  lcd.begin(16,2);
  lcd.print(count);
  delay(250);
  
  if (sB==HIGH)
  {
    def=count;
    brk=false;  
  }
}while(brk=true);
  
  switch (def) {
  case 0:
    lcd.begin(16,2);
    lcd.print("no program");
    delay(2000);
    break;
  case 1:    //steady state
    lcd.begin(16,2);
    lcd.print("steady state");
    delay(2000);
    break;
  case 2:    //interval
    lcd.begin(16,2);
    lcd.print("interval train");
    delay(2000);
    break;
  } 
}

Some syntax errors:

}while(brk=true); needs == for comparison

How are the switches wired up? I recommend this change for all buttons: pinMode(decButton, INPUT_PULLUP); Wire the switch to connect to GND when pressed. Then check for a low: if (iB==LOW)

When you post your code here, mark it up and use the "[ # ]" icon to put it in "code" tags so that it comes up properly formatted in a "code" window. It makes it much easier to deal with, and you can go back and do that on your previous post by using the "modify" option.