Can I exit main to do some function and then come back to main without using interrupts?

I am building a device and I want the user to acces four different settings menus with the help of 4 momentary switches. Then, when done, return to the main function with the help of a fifth momentary switch. I do not want to use interrupts as I need to use the switches inside the four functions. I have tried a why loop:


const int SetSwitch = 2;
const int UpSwitch = 3;
const int DownSwitch = 4;
const int LeftSwitch = 5;
const int RightSwitch = 6;

volatile bool SetSwitchState;
volatile bool UpSwitchState;
volatile bool DownSwitchState;
volatile bool LeftSwitchState;
volatile bool RightSwitchState;


void SettingMenu0 () {
  Serial.println("0");
}
void SettingMenu1 () {
  Serial.println("1");
}
void SettingMenu2 () {
  Serial.println("2");
}
void SettingMenu3 () {
  Serial.println("3");
}
void MainLoop() {
  Serial.println("Working...");
}

int main() {
  // put your main code here, to run repeatedly:

  Serial.begin(9600);

  pinMode(SetSwitch, INPUT_PULLUP);
  pinMode(UpSwitch, INPUT_PULLUP);
  pinMode(DownSwitch, INPUT_PULLUP);
  pinMode(LeftSwitch, INPUT_PULLUP);
  pinMode(RightSwitch, INPUT_PULLUP);

  SetSwitchState = (digitalRead(SetSwitch));
  UpSwitchState = (!digitalRead(UpSwitch));
  DownSwitchState = (!digitalRead(DownSwitch));
  LeftSwitchState = (!digitalRead(LeftSwitch));
  RightSwitchState = (!digitalRead(RightSwitch));

  while (!SetSwitchState) {

    if (UpSwitchState) {
      SettingMenu0();
      delay(50);
    }
    if (DownSwitchState); {
      SettingMenu1();
      delay(50);
    }
    if (LeftSwitchState) {
      SettingMenu2();
      delay(50);
    }

    if (RightSwitchState) {
      SettingMenu3();
      delay(50);
    }
    MainLoop();
  }
}

I have also tried a switch loop:


const int SetSwitch = 2;
const int UpSwitch = 3;
const int DownSwitch = 4;
const int LeftSwitch = 5;
const int RightSwitch = 6;


volatile bool SetSwitchState;
volatile bool UpSwitchState;
volatile bool DownSwitchState;
volatile bool LeftSwitchState;
volatile bool RightSwitchState;

char Switches;


void SettingMenu0 () {
  Serial.println("0");
 
}

void SettingMenu1 () {
  Serial.println("1");

}

void SettingMenu2 () {
  Serial.println("2");

}

void SettingMenu3 () {
  Serial.println("3");

}

void MainLoop() {
  Serial.println("Working...");

}


int main() {
  // put your main code here, to run repeatedly:
  Serial.begin(9600);

  pinMode(SetSwitch, INPUT_PULLUP);
  pinMode(UpSwitch, INPUT_PULLUP);
  pinMode(DownSwitch, INPUT_PULLUP);
  pinMode(LeftSwitch, INPUT_PULLUP);
  pinMode(RightSwitch, INPUT_PULLUP);

  SetSwitchState = (digitalRead(SetSwitch));
  UpSwitchState = (!digitalRead(UpSwitch));
  DownSwitchState = (!digitalRead(DownSwitch));
  LeftSwitchState = (!digitalRead(LeftSwitch));
  RightSwitchState = (!digitalRead(RightSwitch));


  switch (Switches) {
    case 'UpSwitchState':
      SettingMenu0();

    case 'DownSwitchState':
      SettingMenu1();

    case 'LeftSwitchState':
      SettingMenu2();

    case 'RightSwitchState':
      SettingMenu3();

    default:
      MainLoop();
    }

  }

Both Sketches are failing. Am I missing something? Is there another way to achieve that without using interrupts ?

For starters.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Then you could pick one of those .ino's that you want to get working, with its code posted, you could describe what the code is supposed to do and what its doing instead.

You might want to know about

Which MCU are you using. An ESP32?

Thank you, I done the modification.

What does that mean?

And don't go back and edit your earlier posts.

I am using an arduino nano.

Which code do you want to get working? Post it below.

Oh and post the whole code.

What does it mean exactly? Compilation fails, the program was hanging?

Arduino code has a setup() and loop() function.

The sketches are failing to exit from main to do the settings functions and then return to main.

Did you post the entirety of the code you wrote in the Arduino IDE?

Why do you think so?

I did post the complete code for the two exemples.

In both sketches your reading buttons code located outside the while loop... so while the program is running you don't read the buttons at all.

The Serial Monitor does not print the string inside the functions when switches are pressed.

Spend some time studying functions, then use them as a way to exit loop and return at the next instruction. They are very powerful and most programs will not be viable without using them. They also save a lot of redundant code. I taught myself "C" by working with one instruction until I understood it, the next day started over with a new instruction. Sometimes it took a few days to master one. No I do not fully understand them but I can now do what I want to do or have the basic understanding to look up the needed information. Start with the Serial.print("hello gstemarie"); then Serial.println(F("hello gstemarie")); then Serial.print(F("\n\thello gstemarie\n"));. After that add an additional line. Learn the "" options in the print function. Remember you are learning a non spoken language and probably electronics at the same time. These are some of the very basic functions built in. Most important have fun!

OK then. Good luck.

I moved the switches state declaration inside the while loop.
I get:
11:20:04.618 -> 0
as the serial monitor output.

please show full revised code


const int SetSwitch = 2;
const int UpSwitch = 3;
const int DownSwitch = 4;
const int LeftSwitch = 5;
const int RightSwitch = 6;

volatile bool SetSwitchState;
volatile bool UpSwitchState;
volatile bool DownSwitchState;
volatile bool LeftSwitchState;
volatile bool RightSwitchState;


void SettingMenu0 () {
  Serial.println("0");
}
void SettingMenu1 () {
  Serial.println("1");
}
void SettingMenu2 () {
  Serial.println("2");
}
void SettingMenu3 () {
  Serial.println("3");
}
void MainLoop() {
  Serial.println("Working...");
}

int main() {
  // put your main code here, to run repeatedly:

  Serial.begin(9600);

  pinMode(SetSwitch, INPUT_PULLUP);
  pinMode(UpSwitch, INPUT_PULLUP);
  pinMode(DownSwitch, INPUT_PULLUP);
  pinMode(LeftSwitch, INPUT_PULLUP);
  pinMode(RightSwitch, INPUT_PULLUP);


  while (!SetSwitchState) {

    SetSwitchState = (digitalRead(SetSwitch));
    UpSwitchState = (!digitalRead(UpSwitch));
    DownSwitchState = (!digitalRead(DownSwitch));
    LeftSwitchState = (!digitalRead(LeftSwitch));
    RightSwitchState = (!digitalRead(RightSwitch));

    if (UpSwitchState) {
      SettingMenu0();
      delay(50);
    }
    if (DownSwitchState); {
      SettingMenu1();
      delay(50);
    }
    if (LeftSwitchState) {
      SettingMenu2();
      delay(50);
    }

    if (RightSwitchState) {
      SettingMenu3();
      delay(50);
    }
    MainLoop();
  }
}

If you use main() instead of loop(), you have to use while with always true condition:

In general, if you write in arduino ide - use setup and loop, don't try to look smarter than you are :slight_smile: