Multiple loop or sketch is it possible?

Hi all,

I want to run two different functions on the Arduino Mega 2560.

For example;

Arduino starting with the password screen will be opened, if password is correct, the other main function screen opened (Door Control System w/ relay). When you want to “exit” this loop to return to the beginning startup screen (Password Input)

I wrote both codes. How can I integrate these two codes.

Thanks for the help.

In your init() create something like :

int loopState = 0;    // login state

In your loop() create something lik this

if (loopState == 0) {
    login();
} else {
    normalProcess();
}

If the login succeeds, set loopState to 1. If you logout set loopState to 0.

another possibility would be to use an asynchronous check of the ‘Serial’ object, something like:

void loop(void)
{
static char buffer[32]; /* make it bigger if you need it bigger */
static int bufpos = 0;
  ...
  if(Serial.available())
  {
    char c1 = Serial.read();

    if(c1 == '\n') // newline?
    {
      process_command_string(buffer); // process my command
    }
    else if(c1 != '\r' && bufpos < sizeof(buffer) - 1) // buffer length check, ignore return
    {
      buffer[bufpos++] = c1;
      buffer[bufpos] = 0;
    }    
  }
  ...    /* other processing here */
}

The ‘process_command_string()’ function would then interpret whatever command string was typed in, including a response to a ‘login’ prompt. It would be done asynchronously, so you don’t have to block to read anything. Then you would use state variables (similar to what was described in the last post) to control what ‘loop’ does, whether it idles or performs a specific function based on the ‘login’ or ‘not logged in’ state.

unrelated - an obligatory ‘Use ALLMAN style it’s more readable than K&R’ jibe, heh heh heh
/ me regularly modifies preferences.txt to have these two lines:

editor.window.height.default=800
editor.window.width.default=1100

then you don’t need to compress things vertically any more using ‘K&R’ style. You can add more vertical white space (like ALLMAN style) to make the code more readable.