Change the program (void) with just one button

Hello the Arduino team,

i'm looking for a code to change the program (void) with just one button.

Press the button once to change from "PGM2" to "PGM2" to "PGM3" ...and go back to the "PGM1".

Here is my very simple code, but it works :

int button = 2


void setup (void){

   pinMode(button, INPUT_PULLUP);
}


void loop(void){
    
while  (digitalRead(button)==HIGH);
 PGM1();
 }



 void PGM1()
{
 dmd.writePixel( 3, 3, GRAPHICS_NORMAL, 1 );
 delay(30);
 dmd.clearScreen( true );
 delay(30);
}
  • I certainly need an expression that increments a number to trigger a particular "while".

Like : int count = 0 -> while (0) PGM1

  • press : count+1 = 1 -> while(1) PGM2"*
  • I want to use "while" to read the selected program repeatedly, as long as the number is unchanged.

  • And I'm looking to go back to the first program after the 5th, always by pressing the single and only button.

A loop of loops in a loop :smiley:

Thank you for your precious help

The state change detection tutorial shows how to count button presses.

1.) You're not changing programs, you're changing functions
2.) Don't use while loops for running functions - they will block your code
3.) See BlinkWithoutDelay
4.) "A loop of loops in a loop" is absolutely terrible for performance on a low-power MCU

Like this:

const int button = 2;
const int pgmMax = 3;
int pgmCounter = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(button, INPUT_PULLUP);
}

void loop()
{
  static int prevButtonState = HIGH;
  int buttonState = digitalRead(button);
  
  if (prevButtonState == HIGH && buttonState == LOW)
  {
    pgmCounter++;
    pgmCounter %= pgmMax;
    delay(50); // debounce
  }  
  prevButtonState = buttonState;
    
  switch (pgmCounter)
  {
    case 0:
      Serial.println("PGM1");
      break;

    case 1: 
      Serial.println("PGM2");
      break;

    case 2: 
      Serial.println("PGM3");
      break;
    
    default:
      Serial.println("Invalid PGM number");
      break;
  }
}

Thanks guys! :wink:

ToddL1962, i understood and adapted your code to mine, and it works!

But now, i want to merge my different programs, already written in the selector.

Basically my programs are basic but work mainly with "void loop" and "void setup". It is important to find this ecosystem for my programs.

and I also want to use multiple files for readability.

Is there a system that works this way ?
Like:

main.ino

 case 0:
      Serial.println("PGM1");
      PGM1();
      break;

void PGM1()
{
  Go to file PGM1.ino
}

PGM1.ino

void setup(void)
{
      code code code code
}

void loop(void)
{
      code code code code
}

The purpose of to simply integrate my programs as well as to change them quickly

Thanks

You cannot do that. For one thing you can only have one setup() and one loop() function in a program

If you want your functions to behave as if they were in loop() so that the code is called repeatedly then you need to call the function repeatedly from loop().

You can do this by setting a variable to different values using the input button and calling the function associated with the current value. Basically you replace the Serial.print()s in the code in reply #3 with calls to your functions

The functions can be on separate tabs of the IDE if you want, but in order for the program to remain responsive to inputs they must not contain any blocking code that would prevent frequent reading of the input

Maybe this is what you're looking for?

case 0:
Serial.println("PGM1");
PGM1();
// or, just put all the PGM1 code right here
break;

case 1:
Serial.println("PGM2");
PGM2();
// or, just put all the PGM2 code right here
break;

case n: 

//-----------

void PGM1()
{
  PGM1 code
}

void PGM2()
{
  PGM2 code
}

void n

Thanks guys ! it's works :wink: