[SOLVED] menu problem ... please help (change bool with key on LCD Keypad Shied)

Hello everyone :slight_smile:
I want to make loop which will measure temperature in my room ... already done.
and i want to make a menu. menu will show up after pressing select button on lcd keypad shield (menu is done too)
however, i need to make a condition or something that will keep me in menu operating until i press "left" key in main menu and then it will return me to temp.measuring main loop ...
when i press select, menu will appear,but when i want to navigate in it (up and down) it will return me into measuring loop and bugger from menu before ... i tried what i could but i do not know how to fix it ...
I want it like when im in the menu, until i press "left" i will stay in menu and when i press left, it will return to temp.measuring loop.
im new in programming so all tips for my code are appreciated (using links, making it maybe faster or lower on memory size and in menu is switch condition and defined buttons ... i think it can be fixed too ... )
as i said, i'm new so please tell me step by step how to do what do you mean :slight_smile:
thank you

(Attachment was deleted)

Welcome to the party! Your code is very long. How much of it did you write and how much do you understand? I might give someone who has written a menu a different answer than someone who copied a menu from somewhere and hacked it to make it work.

I wrote just that temp. Measuring loop which is in loop and menu was copyied from one website, i didnt programmed it myself ... and defining buttons and using them in menu are also copyied ... i uderstand it a little bit, like string is used for names for menu items and sceolling up and down conditions.
As i said, this is hard for me to understand now ... i need and want to learn more to understand even more than just this
And i did not hack it ... it was a tutorial someone made for begginers like i am

Suppose your Mom said "Go to your room, and don't come out until 5:00PM". Could you figure out how to do that?

Would you be back earning her wrath 100 nanoseconds later? I'll bet not. You'd go to your room (menu function) and stay there while(itIsNotTimeToLetMomSeeMe).

It is easy to change the condition that would let you exit the function...

I know PaulS ... but i do not know how to write it and where to write it in my code

Ivo_Creamo_Durec:
I know PaulS ... but i do not know how to write it and where to write it in my code

If you don't know how to write a function, you should certainly learn. The first thing in the function is the blocking loop, to keep you there until the exit condition is true.

what kind of function : if or do while ? i tried playing with buttons like is in my posted code, i also read about something like "exit 0" or something like that to jump out from the main loop function or stop it ... i tried to write it in but it was always reporting something wrong. and what do you mean by

PaulS:
The first thing in the function is the blocking loop, to keep you there until the exit condition is true.

how should i write it ?

Ivo_Creamo_Durec:
I wrote just that temp. Measuring loop which is in loop and menu was copyied from one website, i didnt programmed it myself ... and defining buttons and using them in menu are also copyied ... i uderstand it a little bit, like string is used for names for menu items and sceolling up and down conditions.
As i said, this is hard for me to understand now ... i need and want to learn more to understand even more than just this
And i did not hack it ... it was a tutorial someone made for begginers like i am

And in a twist of irony, you name the program "my own code". I am not flaming you, just noting the humor. I don't like your code. Particularly, I don't like the use of String class. If there is a menu tutorial, the goal is not to copy the tutorial example, but to learn and produce your own menu. I would ask you to remove all code from your program that you didn't write or don't understand. Then, only add code as you understand it. This will be your best method going forward because you have a body of code that only you know what it "should" do for a project system only you have. None of the rest of us will be able to test the advise we give you. Regarding your OP, there is a function for the "Main menu" which should be your main menu, that is in your concept, no menu, or the temp measuring loop with the call to what is now main menu being similar to how the main menu calls one of the sub menus. If you post your code with only what you understand, we can better help you add the menu.

Ivo_Creamo_Durec:
what kind of function : if or do while ?

I gave you a pretty big clue as to what statement to use in the function. All I've heard since then is "I don't know how to do that" which sounds an awful like "I don't want to learn how to do that".

yaaaay :smiley:
i did it ... i have added bool cycle =1; (is global bool)
and edited the following code to this form and it solved my problem ...

#include <LiquidCrystal.h>

bool cycle = 1;

int read_LCD_buttons(){               // read the buttons
    adc_key_in = analogRead(0);       // read the value from the sensor 

     if (adc_key_in < 50)   return btnRIGHT;  
     if (adc_key_in < 195)  return btnUP; 
     if (adc_key_in < 380)  return btnDOWN; 
     if (adc_key_in < 555)  return btnLEFT; 
     if (adc_key_in < 790)  return btnSELECT;   
     if (adc_key_in > 1000) return btnNONE;  
   
    return btnNONE;                // when all others fail, return this.
}

void setup(){
  lcd.display();
  lcd.begin(16,2);
}

void loop() {

  lcd_key = read_LCD_buttons();

  while (cycle == 1) {

  lcd.setCursor (0,0);
  lcd_key = read_LCD_buttons();

  int readData = (DHTPIN);
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float hic = dht.computeHeatIndex(t,h,false);
  lcd.setCursor(0,0);
  lcd.print("TIME AND DATE");  
  lcd.setCursor(0,1); // Sets cursor on the next line
  lcd.print(h,1);
  lcd.print(" %");
  lcd.setCursor(9,1);
  lcd.print(t,1); // Prints the temperature value from the sensor
  lcd.print(" ");
  lcd.print(char(223));
  lcd.print("C");
  
      if (lcd_key == btnSELECT) {
        cycle = 0;
        while (cycle == 0) {
          MMenuDraw();
          drawCursor();
          operateMMenu();
          delay (100);
          lcd_key = read_LCD_buttons();
          if (lcd_key == btnLEFT) {
            cycle = 1;
            lcd.clear();
            exit;
        }
      }
    }
  } 
}

thanks to all who helped me or gave me some advises ... i appreciate that

bool cycle = 1;

Boolean variables should be assigned values of true or false, not 0 or 1. If you want to use 0 or 1, change the type to byte.