Go Down

Topic: A menu with some buttons (Read 625 times) previous topic - next topic

Vincent19

Dear all, I am doing a project which require a menu. And when I press on a tact switch it will go to another menu.

How can I implement this ?

I tried using if else, but I failed to do so.

Thank you.

LarryD

Show us what you have done.
The way you have it in your schematic isn't the same as how you have it wired up!

Vincent19


Show us what you have done.

Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin_1 = 10;     // the number of the pushbutton pin
const int buttonPin_2 = 9;
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState_1 = 0;         // variable for reading the pushbutton status
int buttonState_2 = 0;
void setup() {
  // initialize the LED pin as an output:
   lcd.begin(20, 4);
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin_1, INPUT);
digitalWrite(buttonPin_1,HIGH); 
pinMode(buttonPin_2, INPUT);
digitalWrite(buttonPin_2,HIGH); 
}

void loop(){
  // read the state of the pushbutton value:
  buttonState_1 = digitalRead(buttonPin_1);
   buttonState_2 = digitalRead(buttonPin_2);

Mainmenu();


}



void Mainmenu()
{
   lcd.setCursor(0,0);
   lcd.print("EEE 351");
   lcd.setCursor(0,1);
   lcd.print("Advanced Laboratory");
   lcd.setCursor(0,2);
   lcd.print("March 2014");
   
   return;
}


retronet_RIMBA1ZO

where's the "other" menu ?

and what are you doing with this
Code: [Select]
  buttonState_1 = digitalRead(buttonPin_1);
   buttonState_2 = digitalRead(buttonPin_2);
after you've read them ?

Vincent19


where's the "other" menu ?

and what are you doing with this
Code: [Select]
  buttonState_1 = digitalRead(buttonPin_1);
   buttonState_2 = digitalRead(buttonPin_2);
after you've read them ?

I am still trying >.<

retronet_RIMBA1ZO

have you tried the Example 'Button' in the Arduino IDE ?

Vincent19


have you tried the Example 'Button' in the Arduino IDE ?

Yes, I tried tat. But I am curious on how to go to other screen under an infinite loop. >.<

LarryD

One way:
lets say you have two switches.
If you press Switch #1  you increment a variable called menuItem
If you press Switch #2 you decrement the variable menuItem
In loop if the menuItem has changed since the last loop you display the words associated with the count in menuItem.
If menuItem has changed
     If menuItem equals 0 you clear the LCD and print  message #0
     If menuItem equals 1 you clear the LCD and print  message #1
etc.

The way you have it in your schematic isn't the same as how you have it wired up!

jasit

So you can do this a couple of ways

Say you have 5 buttons
I would put in the main loop
Check buttons();
Inside check buttons()
I would list all 5 buttons like you did, then do a if (button1 == high){button1menu();}
Then create your button1menu routine.  Inside button1menu(). You can write something to the display and then check button status again.   When I am waiting for  response I use a timer and while statement to see if a button is pressed. Otherwise it will loop right by and you won't get your screen. As you test write to Serial.print and use the serial monitor to see what is happening



Vincent19


So you can do this a couple of ways

Say you have 5 buttons
I would put in the main loop
Check buttons();
Inside check buttons()
I would list all 5 buttons like you did, then do a if (button1 == high){button1menu();}
Then create your button1menu routine.  Inside button1menu(). You can write something to the display and then check button status again.   When I am waiting for  response I use a timer and while statement to see if a button is pressed. Otherwise it will loop right by and you won't get your screen. As you test write to Serial.print and use the serial monitor to see what is happening




Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin_1 = 10;     // the number of the pushbutton pin
const int buttonPin_2 = 9;
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState_1 = 0;         // variable for reading the pushbutton status
int buttonState_2 = 0;
void setup() {
  // initialize the LED pin as an output:
   lcd.begin(20, 4);
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin_1, INPUT);
digitalWrite(buttonPin_1,HIGH); 
pinMode(buttonPin_2, INPUT);
digitalWrite(buttonPin_2,HIGH); 
lcd.setCursor(0,0);
   lcd.print("EEE 351");
   lcd.setCursor(0,1);
   lcd.print("Advanced Laboratory");
   lcd.setCursor(0,2);
   lcd.print("March 2014");
   delay(2500);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Group Members:");
   lcd.setCursor(0,1);
   lcd.print("Lim Chee Keat");
   lcd.setCursor(0,2);
   lcd.print("Teo Kae Hoong");
   lcd.setCursor(0,3);
   lcd.print("Vincent Kok");
   delay(2500);
   lcd.clear();
   lcd.setCursor(6,0);
   lcd.print("Embedded");
   lcd.setCursor(7,1);
   lcd.print("Audio");
   lcd.setCursor(6,2);
   lcd.print("System");
delay(2000);
lcd.clear();
   
}

void loop(){
  // read the state of the pushbutton value:
   buttonState_1 = digitalRead(buttonPin_1);
   buttonState_2 = digitalRead(buttonPin_2);

mainmenu();
if (buttonState_2 == LOW)
{
  screen_2();
  if (buttonState_2 ==LOW);
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Screen3");
  }
}
}

void mainmenu()
{
  lcd.setCursor(0,0);
lcd.print("Main Menu");
lcd.setCursor(0,1);
lcd.print("1.Manual");
lcd.setCursor(10,1);
lcd.print("2.Preset");
lcd.setCursor(0,2);
lcd.print("3.Volume");
lcd.setCursor(10,2);
lcd.print("4.Info");
}

void screen_2()
{
  lcd.clear();
  while(true)
  {
lcd.setCursor(0,0);
  lcd.print("Preset Setting");
  lcd.setCursor(0,1);
  lcd.print("1.Rock");
  lcd.setCursor(0,2);
  lcd.print("2.Jazz");
  lcd.setCursor(0,3);
  lcd.print("3.Classic");
  }
}

This is my current coding. I can get into a main menu. And when button 2 is pressed it will go to Preset Setting Menu. And from this menu how can I make that when I press the same button 2, it will go to the third menu ?

Thanks !

LarryD

As I suggested, if you use a  variable menuItem, each time you push a switch the new value of the counter is used as a trigger to display a new screen message.
The way you have it in your schematic isn't the same as how you have it wired up!

Vincent19


As I suggested, if you use a  variable menuItem, each time you push a switch the new value of the counter is used as a trigger to display a new screen message.

I get your idea on using the variable menuitem. But if the variable increasing by 1, how am I gonna to use it for another screen ?

I mean how will be the algorithm for the code ?

Thanks

Vincent19


As I suggested, if you use a  variable menuItem, each time you push a switch the new value of the counter is used as a trigger to display a new screen message.

My main question is that in the void screen1 subroutine, how do I jump out from that subroutine since I am using the while(true) loop ?

Thanks !

jasit

so for example,

i declare the following
Code: [Select]

int buttonPins[] ={31,32,33,34,35,36};
int buttonState[] ={0,0,0,0,0,0};
[/code/

31,32,33 are the pins I use on the mega.  I use an array so that I don't have to do so many if statements
the example below is when i go into a menu, I display the options on the screen and then wait for a response, if they don't respond in 10 seconds I break out of the code and go back to the previous or main loop.

[code]
long mil = millis();
long timer = 0;
lcd.clear();
  lcd.print("Admin Mode:  1-Update");
  lcd.setCursor(0,1);
  lcd.print("2-FreeVend 3-Off");
int thisPin;
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
   int buttonpressed = 0;
     while(buttonpressed < 1){
       timer = millis();
       for (int thisPin = 0; thisPin < drinkPinCount; thisPin++)  {
            buttonState[thisPin] = digitalRead(buttonPins[thisPin]);
            buttonpressed += buttonState[thisPin];           
       }
       if(timer - mil > 10000) {buttonpressed = 1;}     
     }
     if(timer - mil < 10000){
      if (buttonState[0] == HIGH) {     
           lcd.print("Admin Mode: Update");
           lcd.setCursor(0,1);
           lcd.print(RFIDnum);
           delay(500);
        Serial.println("Admin mode 0");
        admintimeout = 0;
        while(admintimeout <1){AddtoCard();}
      }
      if (buttonState[1] == HIGH) {     
           lcd.print("Admin Mode:");
           lcd.setCursor(0,1);
           lcd.print("Free Drink On");
           for (int setcost = 0; setcost < drinkPinCount; setcost++)  {
            drinkCost[setcost] = 0;
           } 
           FreeDrink();
           delay(500);
           Serial.println("Admin mode 1");
       
      }
      if (buttonState[2] == HIGH) {     
           lcd.print("Admin Mode:");
           lcd.setCursor(0,1);
           lcd.print("Free Drink Off");
           soundSad();
           soundSad();
           soundSad();
           delay(1500);
           ReadPrices();
           
        Serial.println("Admin mode 2");
     }
   
   
    }



[/code]

Vincent19

#14
Mar 06, 2014, 02:36 am Last Edit: Mar 06, 2014, 02:56 am by Vincent19 Reason: 1

so for example,

i declare the following
Code: [Select]

int buttonPins[] ={31,32,33,34,35,36};
int buttonState[] ={0,0,0,0,0,0};
[/code/

31,32,33 are the pins I use on the mega.  I use an array so that I don't have to do so many if statements
the example below is when i go into a menu, I display the options on the screen and then wait for a response, if they don't respond in 10 seconds I break out of the code and go back to the previous or main loop.

[code]
long mil = millis();
long timer = 0;
lcd.clear();
 lcd.print("Admin Mode:  1-Update");
 lcd.setCursor(0,1);
 lcd.print("2-FreeVend 3-Off");
int thisPin;
 // the array elements are numbered from 0 to (pinCount - 1).
 // use a for loop to initialize each pin as an output:
  int buttonpressed = 0;
    while(buttonpressed < 1){
      timer = millis();
      for (int thisPin = 0; thisPin < drinkPinCount; thisPin++)  {
           buttonState[thisPin] = digitalRead(buttonPins[thisPin]);
           buttonpressed += buttonState[thisPin];          
      }
      if(timer - mil > 10000) {buttonpressed = 1;}    
    }
    if(timer - mil < 10000){
     if (buttonState[0] == HIGH) {    
          lcd.print("Admin Mode: Update");
          lcd.setCursor(0,1);
          lcd.print(RFIDnum);
          delay(500);
       Serial.println("Admin mode 0");
       admintimeout = 0;
       while(admintimeout <1){AddtoCard();}
     }
     if (buttonState[1] == HIGH) {    
          lcd.print("Admin Mode:");
          lcd.setCursor(0,1);
          lcd.print("Free Drink On");
          for (int setcost = 0; setcost < drinkPinCount; setcost++)  {
           drinkCost[setcost] = 0;
          }  
          FreeDrink();
          delay(500);
          Serial.println("Admin mode 1");
       
     }
     if (buttonState[2] == HIGH) {    
          lcd.print("Admin Mode:");
          lcd.setCursor(0,1);
          lcd.print("Free Drink Off");
          soundSad();
          soundSad();
          soundSad();
          delay(1500);
          ReadPrices();
         
       Serial.println("Admin mode 2");
    }
   
 
   }



[/code]

Hi, thanks for the reply. However, I wanted to know is that any possibility if I were to modified my current sketch ?

Thank you.

This is my current code :
Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin_1 = 6;     // the number of the pushbutton pin
const int buttonPin_2 = 7;
const int buttonPin_3 = 9;
const int buttonPin_4 = 10;
const int buttonPin_5 = 8;

// variables will change:
int buttonState_1 = 0;         // variable for reading the pushbutton status
int buttonState_2 = 0;
int buttonState_3 = 0;         // variable for reading the pushbutton status
int buttonState_4 = 0;
int buttonState_5 = 0;


void setup() {
  // initialize the LED pin as an output:
   lcd.begin(20, 4);   
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin_1, INPUT);
  digitalWrite(buttonPin_1,HIGH); 
  pinMode(buttonPin_2, INPUT);
  digitalWrite(buttonPin_2,HIGH); 
  pinMode(buttonPin_3, INPUT);
  digitalWrite(buttonPin_3,HIGH); 
  pinMode(buttonPin_4, INPUT);
  digitalWrite(buttonPin_4,HIGH);
pinMode(buttonPin_5, INPUT);
  digitalWrite(buttonPin_5,HIGH);   
  /*lcd.setCursor(0,0);
   lcd.print("EEE 351");
   lcd.setCursor(0,1);
   lcd.print("Advanced Laboratory");
   lcd.setCursor(0,2);
   lcd.print("March 2014");
   delay(2500);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Group Members:");
   lcd.setCursor(0,1);
   lcd.print("Lim Chee Keat");
   lcd.setCursor(0,2);
   lcd.print("Teo Kae Hoong");
   lcd.setCursor(0,3);
   lcd.print("Vincent Kok");
   delay(2500);
   lcd.clear();
   lcd.setCursor(5,0);
   lcd.print("Embedded");
   lcd.setCursor(7,1);
   lcd.print("Audio");
   lcd.setCursor(6,2);
   lcd.print("System");
delay(2500);
lcd.clear();
  */
}

void loop(){
 
  // read the state of the pushbutton value:
   buttonState_1 = digitalRead(buttonPin_1);
   buttonState_2 = digitalRead(buttonPin_2);
    buttonState_3 = digitalRead(buttonPin_3);
   buttonState_4 = digitalRead(buttonPin_4);
    buttonState_5= digitalRead(buttonPin_5);

mainmenu();
if (buttonState_1 == LOW)
{
  screen_1();
  mainmenu();
}
else if (buttonState_2 == LOW)
{
  screen_2();
}
else if (buttonState_3 == LOW)
{
  screen_3();
}
else if (buttonState_4 == LOW)
{
  screen_4();
}
}



void mainmenu()
{
  while(true)
  {
  lcd.setCursor(0,0);
lcd.print("Main Menu");
lcd.setCursor(0,1);
lcd.print("1.Manual");
lcd.setCursor(10,1);
lcd.print("2.Preset");
lcd.setCursor(0,2);
lcd.print("3.Volume");
lcd.setCursor(10,2);
lcd.print("4.Info");
return;
}

}

void screen_1()
{
  lcd.clear();
  while(true)
  {
lcd.setCursor(0,0);
  lcd.print("Manual Selection");
  if (buttonState_5 == LOW)
  {
    return;
  }
}
}

void screen_2()
{
  lcd.clear();
  while(true)
  {
lcd.setCursor(0,0);
  lcd.print("Preset Setting");
  lcd.setCursor(0,1);
  lcd.print("1.Rock");
  lcd.setCursor(0,2);
  lcd.print("2.Jazz");
  lcd.setCursor(0,3);
  lcd.print("3.Classic");
  }
}

void screen_3()
{
  lcd.clear();
  while(true)
  {
lcd.setCursor(0,0);
  lcd.print("Volume Control");
  lcd.setCursor(0,1);
  lcd.print("1.UP");
  lcd.setCursor(0,2);
  lcd.print("2.DOWN");
  }
}

void screen_4()
{
  lcd.clear();
  while(true)
  {
lcd.setCursor(0,0);
  lcd.print("Information");
  lcd.setCursor(2,1);
  lcd.print("Graphic Equalizer");
  lcd.setCursor(8,2);
  lcd.print("V1.0");
 
  }
}
void screen_5()
{
  lcd.clear();
  while(true)
  {
lcd.setCursor(0,0);
  lcd.print("Screen5");
 
  }
}




Go Up