How to jump to a certain part of a program when a condition is met?

Another beginners question here, hopefully you guys can help.

I am making a touch screen display for my boats control panel, it will display a main screen showing temperatures of various places (engine, inside temp, outside temp, water temp and a clock)

i am trying to set it up so that when I press a button on the screen it takes me to another screen with detailed info on it.

I am looking for a way to do this so that i can return to the "main menu" screen again.

basically something similar to the goto function (which i understand i shouldnt use as there is always a way round it using other functions)

I have included my sketch which you will probably think is quite messy but please bear in mind I am still learning :slight_smile:

#include <DS3231.h>
#include <UTFT.h>
#include <UTouch.h>
#include <UTFT_tinyFAT.h>
#include <tinyFAT.h>
#include <Wire.h>

extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

char* files480[]={"pic998.RAW, pic1000.RAW"};

char* files[10];

int picsize_x, picsize_y;

word res;

UTFT myGLCD(ITDB43,38,39,40,41);
UTFT_tinyFAT myFiles(&myGLCD);
DS3231  rtc(SDA, SCL);
UTouch  myTouch( 6, 5, 4, 3, 2);

int val;
int tempPin = 2;

int val2;
int tempPin2 = 3;

int val3;
int tempPin3 = 4;

int x, y;

void waitForIt(int x1, int y1, int x2, int y2)
{

 while (myTouch.dataAvailable())
    myTouch.read();
 
}



void setup()
{
  Serial.begin(115200);
  
  myGLCD.InitLCD();
  file.initFAT();
  
  rtc.begin();
  myTouch.setPrecision(PREC_MEDIUM);
  myTouch.InitTouch();

  myGLCD.setColor(255,255,255);
  myGLCD.setFont(SmallFont);
  picsize_x=myGLCD.getDisplayXSize();
  picsize_y=myGLCD.getDisplayYSize();
  switch (picsize_x)

  
  
  {
     for (int z=0; z<sizeof(files480)/sizeof(*files480);z++)
        files[z] = files480[z];
      }
     
}
  
int var = 100;

void loop()

{
 // int buf[478];
 // int x, x2;
//  int y, y2;
//  int r;

// DRAW THE PATTERN AND LOGO

myGLCD.clrScr();
  myGLCD.setColor (255,0,0);
  for (int i=15; i<256; i+=5)
  {
    myGLCD.drawLine(1, i, (i*1.88)-10, 256);
  }
  myGLCD.setColor (255,0,0);
  for (int i=256; i>15; i-=5)
  {
    myGLCD.drawLine(478, i, (i*1.88)-11, 15);
  }
  myGLCD.setColor (0,255,255);
  for (int i=256; i>15; i-=5)
  {
    myGLCD.drawLine(1, i, 491-(i*1.88), 15);
  }
  myGLCD.setColor (0,255,255);
  for (int i=15; i<256; i+=5)
  {
    myGLCD.drawLine(478, i, 490-(i*1.88), 256);
  }
  {
    res=myFiles.loadBitmap(75, 105, 330, 60, "pic1000.RAW");
  }
    delay(5000);

     myGLCD.clrScr();

    
// DRAW THE MAIN SCREEN
   
  {
  myGLCD.setBackColor (0,0,0);
  res=myFiles.loadBitmap(0, 0, picsize_x, picsize_y, "pic998.RAW");
  while(var < 200)  
 {

// DRAW TIME, DAY & DATE
  
  myGLCD.setFont(SmallFont);
  myGLCD.setColor (255,255,255);
    myGLCD.print(rtc.getDateStr(), 355, 73);
    myGLCD.print(rtc.getDOWStr(), 60, 73);
    myGLCD.setColor (255,255,255);
    myGLCD.setFont(BigFont);
    myGLCD.print(rtc.getTimeStr(), 180, 71);

 { 

// MATHS FOR LM35 TEMP SENSORS

  val = analogRead(tempPin);
  float mv = (val / 1024.0) * 5000;
  float cel = mv / 10;

  val2 = analogRead(tempPin2);
  float mv2 = (val2 / 1024.0) * 5000;
  float cel2 = mv2 / 10;

  val3 = analogRead(tempPin3);
  float mv3 = (val3 / 1024.0) * 5000;
  float cel3 = mv3 / 10;

  // PRINT TEMPS TO TFT

  myGLCD.printNumF(rtc.getTemp(), 1, 279, 158);
  myGLCD.printNumF(cel, 1, 279, 120);
  myGLCD.printNumF(cel2, 1, 279, 197);
  myGLCD.printNumF(cel3, 1, 279, 234);
 }

  
// TOUCH SCREEN COMMANDS

 if (myTouch.dataAvailable())
    {
      
      myTouch.read();
      x=myTouch.getX();
      y=myTouch.getY();
      
      if ((y>=10) && (y<=60))  // Upper row
      {
        if ((x>=10) && (x<=60))  // Button: 1
        {
          waitForIt(10, 10, 60, 60);
          int var2 = 100;
          while (var2 < 200)
          {
          
          
          myGLCD.setFont(BigFont);
          myGLCD.clrScr();
          res=myFiles.loadBitmap(0, 0, picsize_x, picsize_y, "pic998.RAW");
          delay (2000);
          myGLCD.clrScr();
          res=myFiles.loadBitmap(75, 105, 330, 60, "pic1000.RAW");
          delay (2000);

          

          // THIS IS WHERE I NEED HELP - I AM LOOKING FOR A WAY TO EXIT THE while COMMAND
          // AND RETURN TO THE MAIN SCREEN
          
          
    
          
          
      }}}}
        else 
        {myGLCD.print("FAIL", CENTER, CENTER);
        }
}}}

you need to hold state and split the reading of the buttons of the actions involved

int state = START;

void loop()
{
  x = buttonPressed();
  switch(state)
  {
    case START: 
    switch(x)
    {
      csae 'a' : doActionA(); break;  // this action also changes state
      case 'b': doActionB(); break
    }
    break;
    case MENU1: 
    switch(x)
    {
      csae 'a' : doActionC(); break;  // this action also changes state
      case 'b': doActionB(); break
    }
    break;
  }
   // etc
}

get the idea.?

What exactly is this controlling? Something with temperature sensors? You need to make that function the core of your program and then add the user interface around that. Imagine that loop() executes thousands of times per second, checking temperatures and driving motors or whatever. That's its real job. As part of that main loop, it should check the inputs like Serial and the touchscreen. Don't use delay().

When there is a new input, you can perform that actions but always try to keep each action small. Don't wait for 5 seconds for the user to input a number with buttons; handle each button individually and add that to a buffer or internal variable. When the user finishes the entry, then perform the action.

One of the actions might be "draw main menu" which draws the basic menu on the screen. That only needs to be done once when the menu is entered, not every time around the loop. Gather together all of those lines of code which draw the menu into one function and give it a name. Then you can call that action from inside the main loop, when it is required.

Another action might be "update variables on main menu". Which will draw the temperatures on the screen in the right places. You might call this every time loop() runs or you may only call it once every 500 milliseconds. Obviously when you're not displaying the main menu, this function should not be called. Therefore you need some variable to keep track of which menu you are on.

Also the touchscreen inputs need to know which menu is currently displayed, to know what the different actions are. That will use the same variable, but it may modify the variable when it's time to switch to a different menu. The touchscreen code will be large and messy, compared to your main loop, so it's usually best to put this into a function like processTouchscreenInputs() and keep all of that mess out of the core loop.

There is a difference between a jump (goto)
and a function call.

99.99% of the time, it is a bad idea to use a goto. It has no return ability.

Create several functions, and call them as needed. they will be able to return to the line just after you called the function.

You can selectively call a function based on if() or select/case.

Thanks so much for the information you guys have provided, I am getting a much better understanding of it all now :slight_smile:
thanks for taking the time to explain it to me.
I am going to start over from the beginning, i am a long way off but at least now I have a general understanding of how my final sketch should look.

I will spend some time researching the commands you have mentioned and I will try to work on a new sketch implementing them, no doubt I will have more questions but in the meantime - i have got some homework to do :confused:
thanks again

please see attached for a better understanding of what I am trying to achieve,
when I press the clock - takes me to a screen which displays a large clock with buttons to set time
when i press blueberry hill - takes me to a mini slideshow of raw images
when i press any of the view buttons - takes me to individual screens with more info

on all of the screens there will be a button "back button" to take me back to the "main screen" which shows all of the live data.

TFT SCREEN IMAGE 2.jpg

i was trying to upload a short video but there is a 1MB max.
so I have just attached a sill image, this is what the main screen consists of.

Sounds like a challenging project.
You should understand the concepts of "blink without delay" for such a challenging project.

Yip certainly is a challenging one for a beginner lol
I should really be happy with what I have so far - a working clock and live temp readings as well as a demo splash screen with my boats logo,

but i have always loved a challenge and like to take things to the next level (i'm my own worst enemy at times lol)

Thank you I have actually heard of that project but just didnt think it would be relevant to my project, thanks for even more literature :slight_smile: I will spend some time studying tonight ( if my girlfriend allows it lol )

Blink without delay, is the basis for
"simulating multitasking".

The loop() function can run around 120,000 times a second. So you can have many functions being called really fast. but if any one of them have a delay() function in it, then that slows down everything.

There was another great example that I can't locate at the moment, like "do several things at the same time". Maybe someone knows where it is and can post that URL.

You may find some useful stuff in planning and implementing a program. As well as using millis() to manage timing (as in BWoD) it illustrates the use of different functions for different parts of the code.

It will be much easier to manage your project with the "action" code in a series of functions so the logic part of the code can be short and clear.

...R

Robin2:
You may find some useful stuff in planning and implementing a program. As well as using millis() to manage timing (as in BWoD) it illustrates the use of different functions for different parts of the code.

It will be much easier to manage your project with the "action" code in a series of functions so the logic part of the code can be short and clear.

...R

Some more really helpful info, and at a quick glance a really helpful link, as this seems to be my ultimate problem (adding/deleting from current examples)
I am going to make a start from the beginning using the methods detailed above. I will do some homework and hopefully make a start on my programme in a couple of days. Wish me luck, and no doubt i will have more questions through time.
Thanks