menu driven program using 4 buttons in 16X2 lcd

#include<LiquidCrystal.h>
LiquidCrystal lcd(5,4,3,2,1,0);

const int enter = 10;
const int up = 11;
const int down = 12;
const int back = 13;
int start =1;
int run =0;
int sel =0;
void screen(unsigned char x);

void setup()
{
pinMode(enter, INPUT);
pinMode(up, INPUT);
pinMode(down, INPUT);
pinMode(back, INPUT);
lcd.begin(16,2);
lcd.clear();

lcd.setCursor(0,0);
lcd.print(“LOADING…”);
delay(1000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(“CAMERA PROJECT”);
delay(1000);

}

void loop()
{
int e = digitalRead(enter);
int read_up = digitalRead(up);
int read_down = digitalRead(down);
int read_back = digitalRead(back);

screen(‘a’);

while(start==1)
{
if(e==1)
{
screen(‘b’);
while(e==1)
run=1;
start=0;
sel=0;
}

else
{
run=0;
start=1;
sel=0;

}
}

while(run==1)
{

if(enter==HIGH)
{
screen(‘d’);
while(enter==HIGH);
sel=1;
}

if((up==HIGH)||(down==HIGH))
{
screen(‘c’);
while((up==HIGH)||(down==HIGH));
run=2;
}

if(back==HIGH)
{
screen(‘b’);
while(back==HIGH);
start=1;
run=0;
}
}

}

void screen(unsigned char x)
{
switch(x)
{

case ‘a’:
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“CAMERA PROJECT”);
delay(1000);
lcd.clear();
lcd.setCursor(0,1);
lcd.print(“Press < to SUBSEL”);
break;

case ‘b’:

lcd.clear();
lcd.setCursor(0,0);
lcd.print(“1> MOTOR”);
delay(50);
lcd.setCursor(1,0);
lcd.print(“Press > for motor sub”);
break;

case ‘c’:

lcd.clear();
lcd.setCursor(0,0);
lcd.print(“2> CAMERA”);
lcd.setCursor(1,0);
delay(50);
lcd.print(“Press > for camera sub”);
break;

case ‘d’:

lcd.clear();
lcd.setCursor(0,0);
lcd.print("1> DISTANCE ");
delay(50);
lcd.setCursor(1,0);
lcd.print(“Press > for dist i/p”);
break;

case ‘e’:

lcd.clear();
lcd.setCursor(0,0);
lcd.print("1> TIME ");
delay(50);
lcd.setCursor(1,0);
lcd.print(“Press > for time i/p”);
break;

case ‘f’:

lcd.clear();
lcd.setCursor(0,0);
lcd.print("1> TRIGGER ");
delay(50);
lcd.setCursor(1,0);
lcd.print(“Press > for trig i/p”);
break;
}
}

please notify me if there is any error or please suggest me an alternate program .
my ultimate aim is to create the following menu with following four buttons :
-menu/enter
-up
-down
-back /back button is used to go back to the previous menu and not to the main menu/

1.menu /* when enter/menu key is pressed the following has to be displayed */
a)motor
b)camera
/up and down buttons are to be used for selecting motor or camera option/
2.motor /*when motor option is selected and enter button is pressed again the following has to b displayed */
a)distance
b)time

its menu ->submenu program

const int enter = 10;
const int up = 11;
const int down = 12;
const int back = 13; 

    int e = digitalRead(enter);
    int read_up = digitalRead(up);
    int read_down = digitalRead(down);
    int read_back = digitalRead(back);

    while(e==1)
            run=1;

This is going to be a problem if the "enter" input is ever HIGH (1). It will sit in that loop forever, waiting for 'e' to change.

What you meant to write is:

   while(digitalRead(enter))
            run=1;

You have made similar mistakes elsewhere in your sketch. To see what the state of a pin is NOW you have to do another digitalRead().

Fix all of those errors and try again.

hello sir,

thank u very much :) :) :) :) :)..

we followed your suggestion and got the output. :D :D :D :D

Hello sir

I have a doubt if a semicolon is mandatory after while statement in the places like the highlighted one..

while(start==1) { if(digitalRead(enter)==HIGH) { screen('b'); [u]while(digitalRead(enter)==HIGH)[/u] { run=1; start=0; sel=0; } }

else { run=0; start=1; sel=0;

} }

I have a doubt if a semicolon is mandatory after while statement in the places like the highlighted one..

Since that statement is followed by a block, the semicolon is neither mandatory or a good idea.

Of course, the values assigned to run, start, and sel are not forgotten just because the while statement loops, so assigning new values over and over seems pointless.

hey,
i have modified the code a little bit…the code works but not exactly…
i m not able to get the sequence right in the hardware…can anybody find possible errors or suggest an alternative…

#include<LiquidCrystal.h>
LiquidCrystal lcd(0,1,2,3,4,5);

const int enter = 10;
const int up = 11;
const int down = 12;
const int back = 13; 
int start =1;
int run =0;
int sel =0;
int sub=0;
void screen(unsigned char x);

void setup()
{
  pinMode(enter, INPUT);
  pinMode(up, INPUT);
  pinMode(down, INPUT);
  pinMode(back, INPUT);
  lcd.begin(16,2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("LOADING");
  delay(500);
  lcd.setCursor(8,0);
  lcd.print(".");
  delay(500);
  lcd.setCursor(9,0);
  lcd.print(".");
  delay(500);
  lcd.setCursor(10,0);
  lcd.print(".");
  delay(1000);
  		
}

 
void loop()
{
    
    screen('a');
         
    while(start==1)
    {
        if(digitalRead(enter)==HIGH)
        {
            screen('b');
            run=1;
            start=0;
            sel=0;
           
        }
        
        else
        {
             run=0;
             start=1;
             sel=0;
         
         }
    }
    
    while(run==1)
    {
         
        if(digitalRead(enter)==HIGH)
        {
             screen('d');
             sel=1;
         }while(digitalRead(enter)==HIGH);
        
        if((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH))
        {
              screen('c');
              run=2;
        } while((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH));
        
        if(digitalRead(back)==HIGH)
        {
              screen('a');
              sel=0;
              run=0;
              start=1;
            
        } while(digitalRead(back)==HIGH);
    }
    
        while(run==2)
    {
      
        if(digitalRead(enter)==HIGH)
        {
             screen('f');
             sel=2;
            
        }while(digitalRead(enter)==HIGH);
        
        if((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH))
        {
              screen('b');
              run=1;
        }while((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH));
        
        if(digitalRead(back)==HIGH)
        {
              screen('a');
              sel=0;
              run=0;
              start=1;
        }while(digitalRead(back)==HIGH);
    }
    
        while(sel==1)
    {
      
        if(digitalRead(enter)==HIGH)
        {
             screen('g');
             sub=1;
        } while(digitalRead(enter)==HIGH);
        
        if((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH))
        {
              screen('e');
              sel=3;
             
        } while((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH));
        
        if(digitalRead(back)==HIGH)
        {
              screen('b');
              sel=0;
              run=1;
              sub=0;
        }  while(digitalRead(back)==HIGH);
    }
   
    
        while(sel==2)
    {
      
        if(digitalRead(enter)==HIGH)
        {
             screen('i');
        }while(digitalRead(enter)==HIGH);
        
        if((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH))
        {
              while((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH));
        }
        
        if(digitalRead(back)==HIGH)
        {
              screen('c');
              sel=0;
              run=2;
              sub=0;
             
        }while(digitalRead(back)==HIGH);
    }
   
       while(sel==3)
    {
      
        if(digitalRead(enter)==HIGH)
        {
             screen('h');
             sub=2;
         }while(digitalRead(enter)==HIGH);
        
        if((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH))
        {
              screen('d');
              sel=1;
              
        }while((digitalRead(up)==HIGH)||(digitalRead(down)==HIGH));
        
        if(digitalRead(back)==HIGH)
        {
              screen('b');
              run=1;
              
         }while(digitalRead(back)==HIGH);
    }
   
       
  } 
  
void screen(unsigned char x)
{
    switch(x)
    {

	case 'a':
		lcd.clear();					
		lcd.setCursor(0,0);
                lcd.print("welcome");
		delay(800);
       	        lcd.setCursor(0,0);
                lcd.print("Press ENTER to");
                lcd.setCursor(8,1);
                lcd.print("continue");
                
		break;

	case 'b':

		lcd.clear();
        	lcd.setCursor(0,0); 
                lcd.print("1> MOTOR   <");
                lcd.setCursor(0,1);
                lcd.print("2> CAMERA");
                delay(1000);
          	break;

        case 'c':
        
                lcd.clear();    
		lcd.setCursor(0,0);
                lcd.print("1> MOTOR");
		lcd.setCursor(0,1);
                lcd.print("2> CAMERA  <");
		break;
					

	case 'd':

		lcd.clear();
        	lcd.setCursor(0,0); 
                lcd.print("1> DISTANCE  < ");
		lcd.setCursor(0,1);
                lcd.print("2> TIME");
		break;

	case 'e':

		lcd.clear();
        	lcd.setCursor(0,0); 
                lcd.print("1> DISTANCE ");
		lcd.setCursor(0,1);
                lcd.print("2> TIME      <");
		break;


	case 'f':

	        lcd.clear();
        	lcd.setCursor(0,0); 
                lcd.print("1> TRIGGER ");
		lcd.setCursor(0,1);
                lcd.print("enter value");
		break;
        case 'g':
                
                lcd.clear();
                lcd.setCursor(0,0);
                lcd.print("dist value");
                
                
                
         case 'h':
                 
                 
                lcd.clear();
                lcd.setCursor(0,0);
                lcd.print("time value");
                
    }
 }
LiquidCrystal lcd(0,1,2,3,4,5);

This precludes using Serial for debugging. Why would you want to do that?

the code works but not exactly…

So, fix the “not exactly” part. You haven’t defined what the code actually does, or how that differs from what you want.

        if(digitalRead(enter)==HIGH)
        {
             screen('d');
             sel=1;
         }while(digitalRead(enter)==HIGH);

What is this mess? ONE statement per line; NOTHING after the }.

Looks like the intent is:

        if(digitalRead(enter)==HIGH)  // If the Enter button is down
        {
             screen('d');
             sel=1;
         }

      while(digitalRead(enter)==HIGH);  // Wait for the Enter button to go back up

Just bad formatting.

I get a warning that this post have not been replied to in 120 day so I think it is fair to bump it as I see no clear solution.

I have the same requirement and after searching for 7 days, watching Youtube, and a gazilion posts I am still no closer to a sketch.

I have downloaded "nue-rah" and "jonblack" library and they appear not to use 4 buttons. This must be a stand alone aplication.

I have written a very basic interrupt sketch to measure my lathe and milling machine spindle speed.

Now I would like to use a menu to enter material type, cutter type and current diameter and calculate cutting speed and see if that is above or below the recommended cutting rates for that type of material.

Once this work I will see if I can integrate my Digital Readout to provide the diameter automatically, but one thing at a time.

For now 3 buttons to navigate, Up, Down and Back and 1 to Enter.

Is there an applicable library or do I carry on with the code as provided in this post?

or do I carry on with the code as provided in this post?

Yes.