how to stay in sub program

Hello.

when I push on a button I will like to go to a sub program.
and stay dare until I exit the sub

void loop() {
if (next) {
next = false;
mode = 1;
display.clearDisplay();
drawMenu();
}

My display is refresh and I see my sub program but it's direct return to the main program

I have tried to add a "mode" but this does not work.

does anyone have a example how to do this?

Thanks

I think the problem is that your next = false is applying to drawMenu() when it probably should not. Try this

void loop() {
   if (next) {
      next = false;
      mode = 1;
      display.clearDisplay();
    }
    drawMenu();
}

And for the future please use the code button </> for your program code.

In general it would be bad practice to stay in a function because that blocks the Arduino from being able to do other things.

…R

I'm guessing that by 'sub' you mean a function.

The function won't return until you use the 'return' statement or you reach the end of the function body. Or your sketch crashes.

Perhaps if you posted your entire sketch someone can tell you why your function is returning before you tell it to.

Robin2:
I think the problem is that your next = false is applying to drawMenu() when it probably should not. Try this

void loop() {

if (next) {
     next = false;
     mode = 1;
     display.clearDisplay();
   }
   drawMenu();
}




And for the future please use the code button </> for your program code.

In general it would be bad practice to stay in a function because that blocks the Arduino from being able to do other things.

...R

when I do your option I have direct my sub program.
and can not see my Main program…

What condition(s) must be true in order to exit your function ?

Use a while loop to test the condition(s) inside the function and it will return when the condition(s) become true.

pvdbos:
when I do your option I have direct my sub program.
and can not see my Main program…

I don’t understand. Please explain more clearly what you mean.

Your use of the word “Main” is especially confusing. In a normal Arduino program the function loop() is the principal or top-level function and the function called main() is not intended to be visible.

…R

johnwasser:
Perhaps if you posted your entire sketch someone can tell you why your function is returning before you tell it to.

Ok this is my code. it’s a GPS, RTC and now aim creating a menu in it.
The time is synced from a GPS and when it’s done I will like that it’s saved to an DS3132 chip
after that the GPS will turned off for battery savings.

with my “menu” I will like the two timers to set a alarm clock turn on/off the GPS dim the display
set time zone reboot etc.

buttonstate.ino (601 Bytes)

skylab_clock.ino (9.35 KB)

pvdbos:
when I do your option I have direct my sub program.
and can not see my Main program…

I’m repeating this reference after looking at your code attached to Reply #7

If you were trying to implement the suggestion I made in Reply #1 then you have not done what I suggested. and the program seems to be the same as in the snippet you posted in your Original Post.

You need to explain to us exactly what your program does and what you want it to do differently.

…R

Robin2

I have trayed your suggestion but now I have by boot up the Sub program and I can not back to the main program
with my buttons

the buttons doesn't work now!?

Please post your new code.

pvdbos:
Robin2

I have trayed your suggestion but now I have by boot up the Sub program and I can not back to the main program
with my buttons

the buttons doesn't work now!?

Did you read Reply #5? - if so, why are you still using the word "main" and assuming I will know what you mean?

There are some typos in "I have by boot up the Sub program" and I have no idea what you are trying to say. It was also explained in Reply #2 that "function" is the correct term for a part of a C++ program. And your program has several functions. How are we to know which one you mean?

Help us to help you!

...R

I am not so good in programming the "main" is actually the loop.
and my English is also not so good. sorry...

how do I create an "function" I have a sub called "drawmenu" is that no "function"?

P.

Maybe you should describe what you actually want to do instead of how you think you could do it because what you are describing now makes no sense.

pvdbos:
how do I create an "function" I have a sub called "drawmenu" is that no "function"?

What you are calling a "sub" is what we call a "function". It is much easier to communicate if you use the standard C++ words for things.

You already have a function called drawMenu().

Now tell us what you want to do with it.

...R

Hello, Delta_G

in #7 I explain what I want.

But I explain it again:

My code is a GPS RTC the time/date is synced from a GPS, when the time is right it must be put in a DS3231
after that the GPS is power off for battery savings.
the DS3231 have two alarm clocks that I will like to edit via my menu (in EEPROM)
“set time to wake up.”
in the menu I will like to change the timeout of the display to dim and power off
and when I push a button the display is going on.

P

when I push a button the display is going on.

That is the important phrase.

void drawMenu()
{
  while (the buttonIsPressed == false)
  {
     theButtonIsPressed = digitalRead(theButton);
    //code here to do what you want
  }
}

The code in the function must not block the execution of the program otherwise the response to pressing the button will be sluggish or will not be picked up at all. Depending on your circuit you may need to amend the logic of the while loop and the test for the button being pressed.

UKHeliBob.

I have add your code to my code and when I press button "next"

I have now my "menu" on the screen thats ok.

but what must I add to theButtonIsPressed = digitalRead(theButton);

void drawMenu()
{
while (next == false)
{
theButtonIsPressed = digitalRead(theButton);
//code here to do what you want
}
}

Assuming that you have declared the variables correctly and that your wiring is correct then after you read the state if the button you can test it and if it has been pressed then return from the function.

void drawMenu()
{
  while (next == false)  //The function will end if next, whatever that is, is true
  {
    theButtonIsPressed = digitalRead(theButton);
    if (theButtonIsPressed == true)
    {
      return;  //exit the function because the button has been pressed
    }
    else
    {
      //code here to do what you want with the menu
    }
  }
}

UKHeliBob.

you gif in your replay to add your code in the "drawMenu"
is this the right way to do this?

it's doesn't work or do I some wrong...

void drawMenu() {					//pagina 1
	while (mode == 1)
		next = digitalRead(next);
	if (next == true)
	{
		return;  //exit the function because the button has been pressed
	}
	if (page == 1)
	{
		display.setTextSize(2);
		display.clearDisplay();
		display.setTextColor(WHITE, BLACK);
		display.setCursor(10, 0);
		display.print("SETTINGS");
		display.drawFastHLine(0, 16, 128, WHITE);
		display.setTextSize(1);

		if (menuitem == 1) { display.setTextColor(BLACK, WHITE); }
		else { display.setTextColor(WHITE, BLACK); }
		display.setCursor(0, 18);
		display.print("> Set Alarm");
		display.setCursor(0, 28);

		if (menuitem == 2) { display.setTextColor(BLACK, WHITE); }
		else { display.setTextColor(WHITE, BLACK); }
		display.print("> Egg Timer");

		if (menuitem == 3) { display.setTextColor(BLACK, WHITE); }
		else { display.setTextColor(WHITE, BLACK); }
		display.setCursor(0, 38);
		display.print("> StopWatch");

		if (menuitem == 4) { display.setTextColor(BLACK, WHITE); }
		else { display.setTextColor(WHITE, BLACK); }
		display.setCursor(0, 48);
		display.print("> Options");
		display.display();
}

	else if (page == 2) {				//pagina 2
		display.setTextSize(2);
		display.clearDisplay();
		display.setTextColor(WHITE, BLACK);
		display.setCursor(10, 0);
		display.print("SET ALARM");
		display.drawFastHLine(0, 16, 128, WHITE);
		display.setTextSize(1);

		if (menuitem == 1) { display.setTextColor(BLACK, WHITE); }
		else { display.setTextColor(WHITE, BLACK); }
		display.setCursor(0, 18);
		display.print("Alarm1 ");
		if (Alarm1) { display.print("ON"); }
		else { display.print("OFF"); }

		if (menuitem == 2) { display.setTextColor(BLACK, WHITE); }
		else { display.setTextColor(WHITE, BLACK); }
		display.setCursor(0, 28);
		display.print("Alarm2 ");
		if (Alarm2) { display.print("ON"); }
		else { display.print("OFF"); }

		if (menuitem == 3) { display.setTextColor(BLACK, WHITE); }
		else { display.setTextColor(WHITE, BLACK); }
		display.setCursor(0, 38);
		display.print("Back");
		display.display();
	}
	if (up && page == 1) {					// Pagina 1 UP
		up = false;							
		menuitem--;							
		if (menuitem == 0)						
		{									
			menuitem = 4;					
		}									
	}										

	if (down && page == 1) {				// Pagina 1 DOWN
		down = false;						
		menuitem++;							
		if (menuitem == 5)					
		{									
			menuitem = 1;					
		}									
	}										
											 
	if (up && page == 2) {					// Pagina 2 UP
		up = false;							
		menuitem--;							
		if (menuitem == 0)					
		{									
			menuitem = 3;					
		}									
	}

	if (down && page == 2) {				// Pagina 2 DOWN
		down = false;						
		menuitem++;							
		if (menuitem == 4)					
		{									
			menuitem = 1;					
		}									
	}										

	if (next) {
		next = false;
		if (page == 2 && menuitem == 1)
		{
			if (Alarm1)
			{
				Alarm1 = false;
				Alarm1Off();
			}
			else
			{
				Alarm1 = true;
				Alarm1On();
			}
		}

		if (page == 2 && menuitem == 2) {
			if (Alarm2)
			{
				Alarm2 = false;
				Alarm2Off();
			}
			else
			{
				Alarm2 = true;
				Alarm2On();
			}
		}
		if (page == 1 && menuitem == 1) { page = 2; }
		else if (page == 2 && menuitem == 3) { page = 1; }
		//else if (page == 2) { page = 1; }
	}
}