How to change boolean value with LCD keypad shield button ?

Hello all.
I want to make just a testing project for my education.
I want to ‘stop’ loop() function with boolean runloop = true.
Inside loop is this condition, BUT i dont know ho to write it correctly
(ask freely if you want to more explaining info)

#include <LiquidCrystal.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);


const int rs = 8, en = 9, d4 = 4, d5 =5, d6 = 6, d7 = 7;
boolean runloop = true;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7); 

int lcd_key     = 0;
int adc_key_in  = 0;
int MmenuPage = 0, NetWMenuPage = 0;


#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

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); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  lcd.createChar(6, menuCursor);
  dht.begin();
  
}

void loop() {
  lcd.setCursor (0,0);
  lcd_key = read_LCD_buttons();   // read the buttons

  if (startMmenu()) {
  int readData = (DHTPIN);
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  float hic = dht.computeHeatIndex(t,h,false);
  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 (!runloop) {
    lcd.setCursor(4,0);
    lcd.print("IT WORKS !!!!!");
    }
  }
}

  
void startMmenu (){
  if (btnSELECT) {
    !runloop;
    }
  else if (btnLEFT) {
    runloop;
    }
  }

I want to 'stop' loop() function with boolean runloop = true.

I am not sure why anyone would want to stop the loop() function

If you want to control which runs code using a boolean then have the code test the value of the boolean

boolean OKToRun = true;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  if (OKToRun == true)
  {
    //execute the code here
  }
  else
  {
    //execute the code here or maybe don't do anything
  }
  //have code here to change the value of OKToRun from true to false and vice versa
}

UKHeliBob: I am not sure why anyone would want to stop the loop() function

I don't want to stop it i just want to loop that temp. and humid. updating. I want to add menu and i already did, but when its in loop function, then if you will press SELECT, but if you will press down button (scroll down to other options of the menu) it will go automatically back to temp. and humid. measuring, but it also bugged. That's why I want to put menu starting code to boolean condition.

I want to make it like if is btnSELECT pressed --> !runloop . But it won't change the value from true to false.

 if (btnSELECT)
  {
    !runloop;
  }
  else if (btnLEFT)
  {
    runloop;
  }

The first thing to note is that statements like

!runloop;

Do nothing at all. Perhaps you meant

  if (btnSELECT)
  {
    runloop = false;
  }
  else if (btnLEFT)
  {
    runloop = true;
  }

Then in other places in the code you can say

if (runloop == true)
{
  //execute the code here
}

I taught that if you will add ! before that bool or boolean value, it will negate it.
btw. I also tried your edited piece of code but still nothing when i press select.
Please can you write it for me or help me solve it (i don’t mean you are not helping me).

And dont mind that defined menu strings i will write more about that code later

#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht (DHTPIN,DHTTYPE);

String NetWMenuItems [] = {"IP Address","Netmask","Gateway","DNS Server","NTP Server","Back"};
String MMenuItems[] = {"Device ID", "Network Setup", "SQL Setup", "Back","Help"};
const int rs = 8, en = 9, d4 = 4, d5 =5, d6 = 6, d7 = 7;

LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

int lcd_key     = 0;
int adc_key_in  = 0;
int menuPage = 0;
int maxMMenuPages = round(((sizeof(MMenuItems) / sizeof(String)) / 2) + .5);
int NetWMenuPages = round(((sizeof(NetWMenuItems) / sizeof(String)) / 2) + .5);
int cursorPosition = 0;
bool runloop;

byte menuCursor[8] = {
  B01000, 
  B00100, 
  B00010, 
  B00001, 
  B00010, 
  B00100, 
  B01000, 
  B00000  
  };

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

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);
  dht.begin();
  lcd.createChar(0, menuCursor); 
  }

void loop() {
  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");
}

I taught that if you will add ! before that bool or boolean value, it will negate it.

runLoop = !runLoop;  //flip the boolean value of runLoop
if (runLoop)  //if runLoop is true
{
  //do this
}

if (!runLoop)  //if runLoop is false (does not change the value of runLoop)
{
  //do this
}

I also tried your edited piece of code but still nothing when i press select.

Please post the code that you tried

UKHeliBob:
Please post the code that you tried

#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht (DHTPIN,DHTTYPE);

String NetWMenuItems [] = {"IP Address","Netmask","Gateway","DNS Server","NTP Server","Back"};
String MMenuItems[] = {"Device ID", "Network Setup", "SQL Setup", "Back","Help"};
const int rs = 8, en = 9, d4 = 4, d5 =5, d6 = 6, d7 = 7;

LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

int lcd_key     = 0;
int adc_key_in  = 0;
int menuPage = 0;
int maxMMenuPages = round(((sizeof(MMenuItems) / sizeof(String)) / 2) + .5);
int NetWMenuPages = round(((sizeof(NetWMenuItems) / sizeof(String)) / 2) + .5);
int cursorPosition = 0;
bool runloop;

byte menuCursor[8] = {
  B01000, 
  B00100, 
  B00010, 
  B00001, 
  B00010, 
  B00100, 
  B01000, 
  B00000  
  };

#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

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);
  dht.begin();
  lcd.createChar(0, menuCursor); 
  }

void loop() {

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

  if (btnNONE or btnLEFT) {
    runloop = true;
    }
  else if (btnSELECT) {
    runloop = false;
    }
  if (runloop == true) {
  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");
  }
  else if (runloop == false) {
    lcd.print("It Works");
    }
}

maybe I write something bad :confused:

  lcd_key = read_LCD_buttons();   //get the value of any button that is pressed
  if (btnNONE or btnLEFT) //this line should test the value of lcd_key

Note my added comments. The #defined values of btnNONE and btnLEFT will never change so will always be true

UKHeliBob: Note my added comments. The #defined values of btnNONE and btnLEFT will never change so will always be true

ok so how should I write it ?

and you know what i mean bro right ? default setup (when you power up Arduino uno) is temperature and humidity displayed, all buttons will do nothing except SELECT --> will open menu and until only LEFT is pressed (you can press any other and will still stay in menu = down and up for scrolling to submenus) :)

just making sure :)

ok so how should I write it ?

  lcd_key = read_LCD_buttons();   //get the value of any button that is pressed
  if (lcd_key == btnNONE or lcd_key == btnLEFT) //test the value of lcd_key returned by the read_LCD_buttons() function

yes it works but still when i’m in the menu and press down button to scroll down it will return to the default setup and bugged :frowning:

i wrote the code and its complete (see attachment) and i have found 2 more problems. will you help me solve them too ? please

FIRST PROBLEM : when in menu you cant go in submenu NETWORK SETUP → i want it submenu but it will operate like mainmenu “menu in menu”

SECOND PROBLEM : when you scroll at the bottom of the main menu, then first item of the NETWORK SETUP menu is showed and i dont know why cuz its not a part of mainmenu submenus (its submenu of network setup)

please if you can edit my code and sed it like attachment pleeease. :slight_smile:

code.ino (12.6 KB)

i think that must be isolated y'no cuz i think that loop is takin it back to default setup

when i'm in the menu and press down button to scroll down

Your code would be easier to follow if you used the #defined names for the buttons throughout instead of sometimes using magic numbers as in the operateMMenu() function

Add some Serial prints to the program so that it is easier to follow its flow and the value of pertinent variables. These are particularly helpful at decision points in the program