Buttons to start different loops

Hi, I have a Dot Matrix Display and i'm needing help on having two buttons one to run one loop and the other button to run the other. The only help i need is the code for the buttons to run the loops i have the dot matrix display code.

Thanks Ben

3 different models, partial code, search the differences, what behaviour are you looking for?

model 1

void loop()
{
  while (digitalRead(PIN_A) == HIGH) loopA();
  while (digitalRead(PIN_B) == HIGH) loopB();
}

void loopA()
{
  // do you A thingie
}

void loopB()
{
  // do your B thingie
}

model 2

void loop()
{
  if (digitalRead(PIN_A) == HIGH) loopA();
  if (digitalRead(PIN_B) == HIGH) loopB();
}

void loopA()
{
  while(digitalRead(PIN_B) == LOW)
  {
  // do you A thingie 
  }
}

void loopB()
{
  while(digitalRead(PIN_A) == LOW)
  {
  // do you B thingie
  }
}

model 3

void loop()
{
  if (digitalRead(PIN_A) == HIGH) loopA();
  if (digitalRead(PIN_B) == HIGH) loopB();
}

void loopA()
{
  // do you A thingie
}

void loopB()
{
  // do your B thingie
}

Q: Which model can be extended to lets say 6 button?

I would prefer 'model 3' since it avoids leaving you stuck in loop() (which would prevent main() from doing anything else) and keeps you closest to the behaviour of a plain old sketch. Naturally you need to design your loopA(), loopB() etc to be non-blocking too.

Thanks for this i can get it to start the loop but it wont switch to the other loop when the other button is pressed

Thanks for this i can get it to start the loop but it wont switch to the other loop when the other button is pressed

Without seeing your code, all I can suggest is that you rewrite the functions so that doesn't happen. Most likely, you have delay()s all over the place. Get rid of them, using the methodology taught in the blink without delay example.

Thanks for this i can get it to start the loop but it wont switch to the other loop when the other button is pressed

model 2 does. Did you implement the 3 models and compared their behaviour?

You can also implement a state machine which holds a variable telling which loop to run, that is affected by the buttons pressed.

int whichLoop = 0;

void loop()
{
  // CHECK THE BUTTONS
  whichLoop = 0;
  if (digitalRead(PIN_A) == HIGH) whichLoop = 1;
  if (digitalRead(PIN_B) == HIGH) whichLoop = 2; 
  if (digitalRead(PIN_C) == HIGH) whichLoop = 3;

  // SELECT THE LOOP
  switch(whichLoop)
  {
  case 1: loopA(); break;
  case 2: loopB(); break;
  case 3: loopC(); break;
  default: break;
  }
}

void loopA()
{
  // do you A thingie
}

void loopB()
{
  // do your B thingie
}

void loopC()
{
  // do your C thingie
}

Sounds like a state machine. What should your code do if both buttons are pressed?

Sort of psuedo-code here. Still on my 1st cup of coffee.

int state;

void loop () {
   state = 0;
   if (digitalRead(PIN_A) == HIGH state = state || 1;
   if (digitalRead(PIN_B) == HIGH state = state || 2;

   switch (state) {
     case 1:
           // dooby dooby dooohhh (button 1 stuff)
           break;
     case 2:
           // da doo doo doo (button 2 stuff)
           break;
     case 3: // both buttons are pushed
           // dooo that voodoo that you doooo
           break;
     default:
           // maybe there is no do to do
   }

justjed: Sounds like a state machine. What should your code do if both buttons are pressed?

Sort of psuedo-code here. Still on my 1st cup of coffee.

int state;

void loop () {    state = 0;    if (digitalRead(PIN_A) == HIGH state = state || 1;    if (digitalRead(PIN_B) == HIGH state = state || 2;

Given you reset state within the loop, you probably should make it a local variable. More importantly, you want the '|' operator and not '||'. The '||' only returns 0 or 1, while you want the state to vary between 0 and 3. I would write it as:

void loop () {
    int state = 0;
    if (digitalRead (PIN_A))
        state |= 1;
    if (digitalRead (PIN_B))
        state |= 2;
    // ...
}

or even, though this is probably harder to read:

void loop () {
    int state = ((digitalRead (PIN_A) ? 1 : 0) | ((digitalRead (PIN_B) ? 2 : 0);
    // ...
}

Well, I did include the coffee disclaimer. Besides, if I just wrote it well to begin with, what would the OP have left to do?

Coincidentally, the following was the ‘fortune’ in my logwatch today:

last|perl -pe ‘$_ x=/(…:…)…(.*)/&&"’$1’“ge$1&&”’$1’"lt$2’
That’s gonna be tough for Randal to beat… :slight_smile:
– Larry Wall in 1991Apr29.072206.5621@jpl-devvax.jpl.nasa.gov

So, your C isn’t so bad, eh?