lcd.menu(help-me)

hey guys i want to programm a sketch where i can use several sketches in one menu i pretty new to the arduino but still i am that sort of human who is tring the hardest things

so ive got the problem that i can “start” the next program this time a dice where i have to push one button to roll the first dice and then push the button again to roll the second dice 3rd time button push resets all variables so if i got the sketch allone it works fine but when i have insert the dice funktion in the menu it will not work i cant describe it i think you have to build up the lcd your selfe

here the code:

#include <TrueRandom.h>
#include <LiquidCrystal.h>
#include <TimeLib.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

byte ue[8] = {
  0b01010,
  0b00000,
  0b10001,
  0b10001,
  0b10001,
  0b10011,
  0b01101,
  0b00000
};


int Layer;
int uLayer;



int up = 12;
int right = 13;
int down = 11;
int left = 10;
int esc = 9;
int enter = 8;





void setup() {
  // put your setup code here, to run once:
  lcd.createChar(0, ue);
pinMode(up, INPUT_PULLUP);
pinMode(right, INPUT_PULLUP);
pinMode(down, INPUT_PULLUP);
pinMode(left, INPUT_PULLUP);
pinMode(enter, INPUT_PULLUP);
pinMode(esc, INPUT_PULLUP);



lcd.begin(16,2);
lcd.clear();
}

void loop() {
  // put your main code here, to run repeatedly:
while(Layer == 0 && uLayer == 0){
  lcd.setCursor(0,0);
  lcd.print(" -> Uhr");
  lcd.setCursor(0,1);
  lcd.write("    W"); lcd.write(byte(0)); lcd.write("rfel");
  if(digitalRead(enter) == HIGH){
    uLayer = 1;
    Wuerfel();
  }

}
if(digitalRead(enter == HIGH)){
  uLayer = 1;
}




}


void Wuerfel(){


int count;
int ergebnis1;
int ergebnis2;
int ergebnis3;


  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  lcd.setCursor(2,0);
  lcd.write("+");
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  lcd.setCursor(6,0);
  lcd.print("=");
  lcd.setCursor(8,0);
  lcd.print(ergebnis3);
  while(Layer == 0 && uLayer ==1){
  if(digitalRead(down) == HIGH){
  lcd.clear();
  ergebnis1 = 0;
  ergebnis2 = 0;
  ergebnis3 = 0;
  count = 0;
  delay(300);
}
if(digitalRead(enter) == HIGH){
  if(count >= 2){
    ergebnis1 = 0;
    ergebnis2 = 0;
    ergebnis3 -= ergebnis3;
    count = 0;
    lcd.setCursor(9,0);
    lcd.print(" ");
    lcd.setCursor(5,0);
    lcd.print(" ");
    lcd.setCursor(1,0);
    lcd.print(" ");
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  lcd.setCursor(2,0);
  lcd.write("+");
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  lcd.setCursor(6,0);
  lcd.print("=");
  lcd.setCursor(8,0);
  lcd.print(ergebnis3);
  delay(250);
  }
else{
  if (count != 1){ergebnis1 = (TrueRandom.random(1, 6));
  delay(10);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  delay(20);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  delay(30);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  delay(40);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  delay(50);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  delay(100);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  delay(150);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  delay(200);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  delay(300);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  delay(10);
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  ergebnis1 = (TrueRandom.random(1, 6));
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  
  count = 1;
  ergebnis3 = ergebnis1 + ergebnis2;
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  lcd.setCursor(2,0);
  lcd.write("+");
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  lcd.setCursor(6,0);
  lcd.print("=");
  lcd.setCursor(8,0);
  lcd.print(ergebnis3);
  delay(500);
}
else{ergebnis2 = (TrueRandom.random(1, 6));
  delay(10);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  delay(20);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  delay(30);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  delay(40);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  delay(50);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  delay(100);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  delay(150);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  delay(200);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  delay(300);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  delay(10);
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  ergebnis2 = (TrueRandom.random(1, 6));
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  count = 2;
  ergebnis3 = 0;
  ergebnis3 = ergebnis1 + ergebnis2;
  lcd.setCursor(0,0);
  lcd.print(ergebnis1);
  lcd.setCursor(2,0);
  lcd.write("+");
  lcd.setCursor(4,0);
  lcd.print(ergebnis2);
  lcd.setCursor(6,0);
  lcd.print("=");
  lcd.setCursor(8,0);
  lcd.print(ergebnis3);
  delay(500);
}
}
}
}
}

thanks for the help :slight_smile:

maybe describe how you wish the program to work.

"I push a button and....

well i want it to work like an menu layer is up and down ulayer is left and right thats how the programms should be called then but i got the issue that if i call a programm it will exit from the programm or atleast half the screen will turn blank :confused:

let’s get your buttons working first! Try this on your rig and be sure to check the pins on your buttons.

Compiles but not tested…

const byte buttonPins[] = {12, 13, 11, 10, 9, 8};
const char* const pinText[] = { "UpButton", "RightButton", "DownButton", "LeftButton", "Enter", "Escape"};

void setup() 
{
  Serial.begin(115200);
  Serial.println(F("Start"));
  for (byte i = 0; i < sizeof(buttonPins); i++)
  {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
}

void loop() 
{
  int8_t pressedButton = checkMyButtons(buttonPins, sizeof(buttonPins));
  if(pressedButton != -1) 
  {
    Serial.print(pinText[pressedButton]);
    Serial.println(F(" pressed!"));
  }
  switch (pressedButton)  // set up for six pins
  {
    case 0:  // UpButton
      //
      break;
    case 1:  // RightButton
      //
      break;
    case 2:  // DownButton
      //
      break;
    case 3:  // LeftButton
      //
      break;
    case 4:  // Enter
      //
      break;
    case 5:  // Escape
      //
      break;
    default:
      // do nothing
      ;
  }
  // Put your loop() code here
}

int8_t checkMyButtons(const byte* pins, const size_t numPins)
{
  static int8_t lastButtonState[sizeof(buttonPins)] = {1};
  static unsigned long lastMillis[sizeof(buttonPins)] = {0};
  int8_t pressedButton = -1;
  for(byte i = 0; i < numPins; i++)
  {
    if(millis() - lastMillis[i] > 15UL)  // debounce
    {
      lastMillis[i] = millis();
    }
    else 
    {
      continue;
    }
    byte state = digitalRead(pins[i]);
    if (state != lastButtonState[i])
    {
      if(state == 0)
      {
        pressedButton = i;
      }
    }
    lastButtonState[i] = state;
  }
  return pressedButton;
}

Just open and look at the serial monitor when you press the buttons.

We can then get your code working depending on what button is pressed… We will then introduce a state machine and change states on button presses that are developed in the Switch/Case in loop() function.

ok well this doesnt work that good uhm have you got time for me for a skype call or something? :o

it should only tell you that a button is pressed... did yo look at the Serial monitor while pressing the buttons?

Post any output you get on Serial... remember to set right baud rate:

Serial.begin(115200);

well this is what i got in the serial monitor seems to be some thing wrong with the code
but also i got the buttons work in a other project before so the build up is deffinetly working

serial monitor :smiley:

çÐøYãrÿšzþÉú˜ûøÍørÿrÿørÿ˜û˜ûúþrÿrÿ�ø˜û˜û˜ûzÿøøzþ�øøÍøøø¹ørÿ

check the baud rate on the Serial Monitor!!!

Should be 115200…

Check attachment

ohh yeah im sorry man havent seen that one

ok yeah all buttons exept up and down are working i'll check them now

GlueMen:
ohh yeah im sorry man havent seen that one

ok yeah all buttons exept up and down are working i'll check them now

cool

so what do you want to do on your button press?

I think you was to Roll the Dice and display the results...

Can you describe what you want to do? Maybe show what the display looks like?

We can work through the sequence of LCD output you want to do...

well first of all im using an 16 by 2 lcd 16 pin.

ok so i want to programm a small menu for some programms rightnow a clock and a dice

i ahve got clock and dice working in other sketches and they are working pretty good

but i want to make a menu ich is like this

--> clock
dice

now pushing enter will enter the clock skatch

clock
--> dice

now pushing enter will start the dice skatch

using escape will close the skatches and return to the menu

as "simple" as that

so here comes the problem when i am starting the dice(the only sketch implemented yet) it will go into that sketch but it will not show up all the time it just blinks up for some seconds(the time it uses to roll out the numer) set the number in to result and then the lcd turns "black" again

normaly i can push the button agin to roll out the next dice but it resets everytime it finishes the first roll out

btw im not from an english county so misspelling are specialeffects! :slight_smile:

GlueMen:
btw im not from an english county so misspelling are specialeffects! :slight_smile:

no worries.

let me try something for you!

ok so i fixed the up button but i cant find any fails at the donw button but still doesnt work BUT if i attach and detatch the donw button it will give a signal to the serial monitor with donwbutton was pressed =O

ok nope fixed donw button too

GlueMen:
ok nope fixed donw button too

OK, so I didn’t do this on an LCD… but you can do that easily…

take a look and work with it on the Serial Monitor. I know it isn’t EXACTLY what you want, but it moves through the states and I think you can make it work for your needs.

Displays to Serial Monitor:

enum State {
  STAND_BY,
  MENU_CLOCK,
  MENU_DICE,
  ROLL_ONE,
  ROLL_TWO,
  RESULTS,
  CLOCK
};
const char* const menuState[] = { "Stand By", "Menu Clock", "Menu Dice", "Roll One", "Roll Two", "Results", "Clock"};


State myState = STAND_BY;

const byte buttonPins[] = {12, 13, 11, 10, 9, 8};
const char* const pinText[] = { "UpButton", "RightButton", "DownButton", "LeftButton", "Enter", "Escape"};
int rollOne, rollTwo;
void setup()
{
  Serial.begin(115200);
  Serial.println(F("Start"));
  for (byte i = 0; i < sizeof(buttonPins); i++)
  {
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
  randomSeed(analogRead(A4));
}

void loop()
{
  int8_t pressedButton = checkMyButtons(buttonPins, sizeof(buttonPins));
  if (pressedButton != -1)
  {
    Serial.print(pinText[pressedButton]);
    Serial.println(" pressed!");
  }
  switch (pressedButton)  // set up for six pins
  {
    case 0:  // UpButton
      switch (myState){
        case STAND_BY:
          myState = MENU_CLOCK;
          break;
        case MENU_CLOCK:
          myState = MENU_CLOCK;
          break;
        case MENU_DICE:
          myState = MENU_CLOCK;
          break;
        case ROLL_ONE:
          //
          break;
        case ROLL_TWO:
          //
          break;
        case RESULTS:
          myState = STAND_BY;
          //
          break;
        default:
          Serial.println("default");
      }
      break;
    case 1:  // RightButton
      //
      break;
    case 2:  // DownButton
      switch (myState){
        case STAND_BY:
          myState = MENU_CLOCK;
          break;
        case MENU_CLOCK:
          myState = MENU_DICE;
          break;
        case MENU_DICE:
          myState = MENU_DICE;
          break;
        case ROLL_ONE:
          //
          break;
        case ROLL_TWO:
          //
          break;
        case RESULTS:
          myState = STAND_BY;
          //
          break;
        default:
          Serial.println("default");
      }
      break;
    case 3:  // LeftButton
      //
      break;
    case 4:  // Enter
      switch (myState){
        case STAND_BY:
          myState = MENU_CLOCK;
          break;
        case MENU_CLOCK:
          myState = CLOCK;
          break;
        case MENU_DICE:
          myState = ROLL_ONE;
          break;
        case ROLL_ONE:
          myState = ROLL_TWO;
          break;
        case ROLL_TWO:
          myState = RESULTS;
          break;
        case RESULTS:
          myState = STAND_BY;
          //
          break;
        case CLOCK:
          myState = STAND_BY;
          break;
        default:
          Serial.println("default");
      }
      break;
    case 5:  // Escape
      //
      break;
    default:
      // do nothing
      ;
  }
  lcdUpdate();
}

int8_t checkMyButtons(const byte* pins, const size_t numPins)
{
  static int8_t lastButtonState[sizeof(buttonPins)] = {1};
  static unsigned long lastMillis[sizeof(buttonPins)] = {0};
  int8_t pressedButton = -1;
  for (byte i = 0; i < numPins; i++)
  {
    if (millis() - lastMillis[i] > 15UL) // debounce
    {
      lastMillis[i] = millis();
    }
    else
    {
      continue;
    }
    byte state = digitalRead(pins[i]);
    if (state != lastButtonState[i])
    {
      if (state == 0)
      {
        pressedButton = i;
      }
    }
    lastButtonState[i] = state;
  }
  return pressedButton;
}

void lcdUpdate()
{
  static State lastState = STAND_BY;
  static unsigned long lastMillis = 0;
  if (myState != lastState)
  {
    Serial.println(" ");
    //Serial.println(menuState[myState]);
    switch (myState)
    {
      case STAND_BY:
        Serial.println();
        Serial.println("Clock Dice Game");
        Serial.println("Ready");
        break;
      case MENU_CLOCK:
        Serial.println();
        Serial.println("--> Clock");
        Serial.println("    Dice");
        break;
      case MENU_DICE:
        Serial.println();
        Serial.println("    Clock");
        Serial.println("--> Dice");
        break;
      case ROLL_ONE:
        Serial.println();
        rollOne = random(1,7);
        Serial.print("Roll One = "); Serial.println(rollOne);
        Serial.println("");
        break;
      case ROLL_TWO:
        Serial.println();
        rollTwo = random(1,7);
        Serial.print("Roll One = "); Serial.println(rollOne);
        Serial.print("Roll Two = "); Serial.println(rollTwo);
        break;
      case RESULTS:
        Serial.println();
        Serial.print("   You Rolled: "); Serial.println(rollOne+rollTwo);
        Serial.print("Dealer Rolled:"); Serial.println(random(2,13));
        break;
    }
  }
  lastState = myState;
  if(myState == CLOCK)
  {
    if(millis() - lastMillis >= 1000)
    {
      Serial.println();
      Serial.print("elapsed seconds:");
      Serial.println(millis()/1000);
      lastMillis = millis();
    }
  }
}