SOLVED! Switch Case Menu Help!

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
JamieA
 
Posts: 4
Joined: Sun Oct 30, 2011 6:15 pm

SOLVED! Switch Case Menu Help!

Post by JamieA »

Hi, first up- I'm a complete newbie to all this Arduino and programming stuff; I've been playing around for about a week now in an effort to build a semi-automated brewery, which some way off as yet :D

I have a 20X4 LCD wired up and a button with a pull down resistor and I'm trying to programme a simple one-button test menu. The code below works until case 2 where it just repeats and doesn't go forward. Any pointers as to where I'm going wrong would be greatly appreciated!

Oh, I'm sure the code could be somewhat more elegant :oops:

Code: Select all

#include <Bounce.h>

#include <LiquidCrystal.h>

#define button 8

Bounce bounced = Bounce(button, 5);

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte menu = 1;

void buttonPress(){
  while (menu == 1){
  bounced.update(); 
  byte value = bounced.read(); 
  if (value == HIGH) {
      condition1();
    }
   }
  }
  
void buttonPress1(){
  while (menu == 2){
    bounced.update(); 
  byte value = bounced.read(); 
  if (value == HIGH) {
      condition2();
   }
  }
 }

void buttonPress2(){
  while (menu == 3){
   bounced.update(); 
  byte value = bounced.read(); 
  if (value == HIGH) {
      conditionFinish();
   }
  }
 }

void mainMenu(){
  
  lcd.clear();
  lcd.setCursor(6,0);
  lcd.print("WELCOME!");
  lcd.setCursor(3,2);
  lcd.print("Press button to");
  lcd.setCursor(4,3);
  lcd.print("go forward");
}

void condition1(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Hello World");
}
  
  void condition2(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Goodbye!");
  }
  
  void conditionFinish(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Finished!!!");
  }

void setup() {
  lcd.begin(20,4);
  pinMode (button, INPUT);
  }

void loop(){
   switch (menu) {
  case 1:
    mainMenu();
    buttonPress();
      break;
  case 2:
      condition1();
      buttonPress1();
      break;
  case 3:
      condition2();
      buttonPress2();
      break;
  case 4:
      conditionFinish();
      break;
  }
 }
Cheers,
Jamie
Last edited by JamieA on Fri Nov 04, 2011 5:55 pm, edited 1 time in total.

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Switch Case Menu Help!

Post by adafruit_support_bill »

Where does "menu" get updated?

JamieA
 
Posts: 4
Joined: Sun Oct 30, 2011 6:15 pm

Re: Switch Case Menu Help!

Post by JamieA »

Whoops! I did have the following in place before commenting it out while trying to figure it out, then I got rid of the comments for posting :oops: .

Code: Select all

...
if (value == HIGH) {
      condition2();
      menu++;
Needless to say it doesn't work with it there!

Cheers,
Jamie

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Switch Case Menu Help!

Post by adafruit_support_bill »

Can you post the complete code? I'm not sure exactly where that fragment fits in.

JamieA
 
Posts: 4
Joined: Sun Oct 30, 2011 6:15 pm

Re: Switch Case Menu Help!

Post by JamieA »

Hi, here's the complete code:

Code: Select all

#include <Bounce.h>

#include <LiquidCrystal.h>

#define button 8

Bounce bounced = Bounce(button, 5);

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte menu = 1;

void buttonState(){
  while (menu == 1){
  bounced.update(); 
  byte value = bounced.read(); 
  if (value == HIGH) {
      condition1();
    }
   }
  }
  
void buttonPress1(){
  while (menu == 2){
    bounced.update(); 
  byte value = bounced.read(); 
  if (value == HIGH) {
      condition2();
      menu++;
   }
  }
 }

void buttonPress2(){
  while (menu == 3){
   bounced.update(); 
  byte value = bounced.read(); 
  if (value == HIGH) {
      conditionFinish();
      menu++;
   }
  }
 }

void mainMenu(){
  
  lcd.clear();
  lcd.setCursor(6,0);
  lcd.print("WELCOME!");
  lcd.setCursor(3,2);
  lcd.print("Press button to");
  lcd.setCursor(4,3);
  lcd.print("go forward");
}

void condition1(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Hello World");
}
  
  void condition2(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Goodbye!");
  }
  
  void conditionFinish(){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Finished!!!");
  }

void setup() {
  lcd.begin(20,4);
  pinMode (button, INPUT);
  }

void loop(){
   switch (menu) {
  case 1:
    mainMenu();
    buttonState();
      break;
  case 2:
      condition1();
      buttonState();
      break;
  case 3:
      condition2();
      buttonState();
      break;
  case 4:
      conditionFinish();
      break;
  }
 }

 
Cheers,
Jamie

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Switch Case Menu Help!

Post by adafruit_support_bill »

The only places where 'menu' is updated are the functions 'buttonPress1' and 'buttonPress2'. Neither of these functions is ever called form anywhere that I can see.

JamieA
 
Posts: 4
Joined: Sun Oct 30, 2011 6:15 pm

SOLVED! Switch Case Menu Help!

Post by JamieA »

Hi there, after a lot of head scratching I finally got the code working!

Code: Select all

#include <Bounce.h>

#include <LiquidCrystal.h>

#define button 8

Bounce bounced = Bounce(button, 5);

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte menu = 1;

void buttonState(){
  while ( bounced.update()) { 
    if ( bounced.read() == HIGH)
      menu++;
       if (menu>4) menu = 1;
        lcd.clear();
    }
  }
void mainMenu(){
  lcd.setCursor(6,0);
  lcd.print("WELCOME!");
  lcd.setCursor(3,2);
  lcd.print("Press button to");
  lcd.setCursor(5,3);
  lcd.print("go forward");
}

void condition1(){
  lcd.setCursor(0,0);
  lcd.print("Hello World");
}

void condition2(){
  lcd.setCursor(0,0);
  lcd.print("Goodbye!");

}
void conditionFinish(){
  lcd.setCursor(0,0);
  lcd.print("Finished!!!");
  lcd.setCursor(2,2);
  lcd.print("Press button to"); 
  lcd.setCursor(6,3);
  lcd.print ("restart");
}

void setup() {
  lcd.begin(20,4);
  pinMode (button, INPUT);
  }

void loop(){
   switch (menu) {
  case 1:
      mainMenu();
      buttonState();
    break;
  case 2:
      condition1();
      buttonState();
    break;
  case 3:
      condition2();
      buttonState();
    break;
  case 4:
      conditionFinish();
      buttonState();
    break;
  }
 }
 
Any pointers as to how it can be improved?

Cheers,
Jamie

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: SOLVED! Switch Case Menu Help!

Post by adafruit_support_bill »

Looks pretty clean!

Since "buttonState()' is called in every case of the switch statement, you could pull the 4 calls out of the switch and just have one call at the end of the loop.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”