Calling a group of Functions outside setup and loop.

Hi.

I want to call a group of functions in a library outside setup and loop how can this be done?

Below is my sample code,I want to move Menu1 and Menu2 outside setup and loop.

//==============================================
//==== colors for '' ILI9163 '' ================
//==============================================
#include "SPI.h"
#include "Adafruit_GFX_AS.h"
#include "Adafruit_ILI9341_AS.h"

#define sclk 13  // Don't change
#define mosi 11  // Don't change
#define cs   10
#define dc   9
#define rst  7  

Adafruit_ILI9341_AS tft = Adafruit_ILI9341_AS(cs, dc, rst);       // Invoke custom library

void setup()
{
    tft.init();
   
  // ==== Menu 1 =====
    tft.fillScreen(WHITE); 
    tft.setTextColor(BLACK);
    tft.drawString("Menu1",50,0,2);

    delay(1000);
    
   // ==== Menu 2 =====
    tft.fillScreen(GREEN); 
    tft.setTextColor(RED);//fore ground,back ground===removes flicker when update
    tft.drawString("Menu2",50,0,2);

    delay(1000);

    }

void loop()
  {
    //Button 1 select Menu1

    //Button 2 select Menu2

  }

Thanks.

Mike.

All executable code must be inside SOME function. That can be setup(), loop(), or a function that you write.

gfvalvo: All executable code must be inside SOME function. That can be setup(), loop(), or a function that you write.

But unless you are writing your own version of main, then you have to understand that you have to work within the constraints of what main is doing. It's only calling setup and loop. You can write as many other functions as you want, but if you don't call them from setup or loop then they don't run.

*Yes I know SerialEvent but it is stupid so I didn't mention it.

//==============================================
//==== colors for '' ILI9163 '' ================
//==============================================
#include "SPI.h"
#include "Adafruit_GFX_AS.h"
#include "Adafruit_ILI9341_AS.h"

#define sclk 13  // Don't change
#define mosi 11  // Don't change
#define cs   10
#define dc   9
#define rst  7

const byte Button1 = 2;
const byte Button2 = 3;

Adafruit_ILI9341_AS tft = Adafruit_ILI9341_AS(cs, dc, rst);       // Invoke custom library

void setup()
{
  tft.init();
}

void loop()
{
  if(Button1) Menu1();

  if(Button2) Menu2();
}

void Menu1()
{
  // ==== Menu 1 =====
  tft.fillScreen(WHITE);
  tft.setTextColor(BLACK);
  tft.drawString("Menu1", 50, 0, 2);

  delay(1000);
}

void Menu2()
{
  // ==== Menu 2 =====
  tft.fillScreen(GREEN);
  tft.setTextColor(RED);//fore ground,back ground===removes flicker when update
  tft.drawString("Menu2", 50, 0, 2);

  delay(1000);
}
  if(Button1) Menu1();

  if(Button2) Menu2();

Button1 is 2, which is true. Button2 is 3, which is true. Both are const, so they can not change. So, this code might as well be:

  Menu1();

  Menu2();

which is clearly NOT what OP wanted.

Perhaps you meant to read the state of the pin, and act if it is in some specific state.

Have a look at how functions are used in Planning and Implementing a Program

...R