Multiple loops in a programme

Hi,

I have been searching for an answer but cant find a similar question;

Is it possible to have multiple loops in a programme and a command prompting which loop to go to at a certain point.

For example, when a switch is low the void loop runs, when a certain switch goes high - goto - "void loop 2" etc....

For the programme I am working on, this would make the programming much easier to use multiple loops. They do not need to run at the same time but the switches will do different things based on the loop that's running.

Thanks in advance
Paul

Is it possible to have multiple loops in a programme

Of course. It is not, though, possible to have multiple functions called loop().

and a command prompting which loop to go to at a certain point.

Where are you going to display that prompt? Where are you going to get the input from?

You will NOT be using goto to go anywhere.

You CAN get input from the serial port, and CALL the appropriate function, based on the input.

1 Like

There is always only one loop, but that loop can take decisions at run time based on the status of your switch

your code structure could basically look like this

void loop1()
{
   // do something

}

void loop2()
{
  // do something  else

}


void loop()
{
    // assuming you have a function readPositionOfSwitch() to check the button status, whatever that is
    if (readPositionOfSwitch() == LOW) {
      loop1();
    } else {
      loop2();
    }
}

but this is not like having two different programs in your arduino. There is only one setup() and global variables are shared etc.. so you probably need to handle what happens to variables when you go from one mode to the other if necessary

Have a look at the demo Several Things at a Time and at
Planning and Implementing a Program

...R

Thanks for the advice. I’ll have a good read through tonight.

your code structure could basically look like this

We'll kick your ass all over the place if you name your functions loop1(), loop2(), etc. But, otherwise, yeah, that's the general idea.

PaulS:
We’ll kick your ass all over the place if you name your functions loop1(), loop2(), etc. But, otherwise, yeah, that’s the general idea.

:grin:

yes - make the name of the function something meaningful!

Hi,

Sorry to dig out my old thread but I've been working on this again tonight and I'm getting an error that I cannot work out despite lots of searching. I know its a common one but the answer is just not clicking with me.

I have a switch and a pot controlling a variable load. The load is connected via a transistor.

When the switch is high; I want the load to be "live" and variable with the pot.
When the switch is low I want to kill the power by dropping the output to the transistor base.

int potPin = A0;
int motorPin = 9;
int trainstatus = 7;

int potValue = 0;
int motorValue = 0;
int buttonState = 0;

void setup() {
  Serial.begin(9600);  
  pinMode(trainstatus), INPUT;
}


void trainrunning()
{
  potValue = analogRead(potPin);
  motorValue = map(potValue, 0, 1023, 0, 255);

  analogWrite(motorPin, motorValue);

  Serial.print("potentiometer = " );

 Serial.print(potValue);
 Serial.print("\t motor = ");
 Serial.println(motorValue);

}

void trainstopping()
{
 motorPin = LOW; 
}


void loop() {
  buttonState = digitalRead (trainstatus);
if (trainstatus() == HIGH) {

  trainrunning();
  
    } else {
      trainstopping();
    }
  
  {
  
}
}

my error message;

traincontrollersimple111017:39: error: 'trainstatus' cannot be used as a function

if (trainstatus() == HIGH) {

^

exit status 1
too few arguments to function 'void pinMode(uint8_t, uint8_t)'

Thank you in advance for any help

Paul

pinMode(trainstatus), INPUT;

It gave you the error and it pointed out this line. Does that line look right? Maybe go look at some pinMode calls in some examples and see if you can spot the difference.

if (trainstatus() == HIGH) {

Yup, trainstatus is an int variable and not a function. You can’t just jam some parenthesis on the end of something and pretend it is a function. Is there another variable somewhere that you stored the STATE of the pin in? Look at any of the examples where they read a pin, do they just jam a pair of parenthesis on the end of the name of the variable holding the pin number? Look around that line and see if you can’t spot the name of the variable where you saved what you read from that pin.

Many thanks Delta_G. I must have just been looking at it for too long for the obvious things to jump out at me!

bedtime now I think!

much appreciated