Simple Menu System not so simple....

ok, I am trying to make a simple menu system and it isn't cooperating. It should go check the button status on four buttons pulled high. With nothing down, it'll return 1111. With the 3rd button pressed it returns 1101. This works. If I comment the code to run the DEBUG section, it shows my presses correctly. But I cannot get the ifthens to react to it. I think its a variable type issue, but I'm really not sure since I have tried every combination I can think of.

And yes, it's poorly structured, and uses gotos... At this point though, I haven't really even decided how I want it to work in the end. Since I plan to have this 16x2 or 16x4 lcd, or maybe a better display with touch, as well as output the menu system via video signal to my 7in rearview mirror display, plus buttons and bluetooth and maybe even GSM controls, I imagine I'll be rebuilding it several times before I like it anyway.

Thanks in advance for any input!

//LCD D7 pin to digital pin 2
//LCD D6 pin to digital pin 3
//LCD D5 pin to digital pin 4
//LCD D4 pin to digital pin 5
//LCD Enable pin to digital pin 11
//LCD RS pin to digital pin 12
//LCD R/W pin to ground
//LCD VSS pin to ground
//LCD VCC pin to 5V
//10K resistor:
//ends to +5V and ground
//wiper to LCD VO pin (pin 3)

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

void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Avalanche Master");
lcd.setCursor(0, 1);
lcd.print(" Control System ");
delay(5000); // Holds LCD splash, but mainly halts code for 5 seconds b4 serial start to avoid programming issues.
Serial.begin(9600);
Serial.println("Avalanche Master Control System");
lcd.clear();
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A5, OUTPUT);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
digitalWrite(A3, HIGH);
digitalWrite(A4, HIGH);
digitalWrite(A5, HIGH);
}
int mmitemnum = 1;
int avitemnum = 1;
int LTitemnum = 1;
String CurMenuItem = " ";
int cargolts = 0;
int interiorlts = 0;
String btread = "";
String button1 = "1";
String button2 = "1";
String button3 = "1"; 
String button4 = "1";
String buttonstate = "1111";

void loop() {

//goto MainMenu;
goto DEBUG;

CheckButtons:
button1 = digitalRead(A1);
button2 = digitalRead(A2);
button3 = digitalRead(A3);
button4 = digitalRead(A4);
buttonstate = button1 + button2 + button3 + button4;
return;

DEBUG:
  lcd.setCursor(0, 0);
  lcd.print("ButtonState is:");
  lcd.setCursor(0, 1);
  lcd.print("      " + buttonstate + "      ");
  buttonstate = "1111";
  delay(100);
  goto CheckButtons;
goto DEBUG;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
MainMenu:
  if (mmitemnum == 1) {CurMenuItem = "  A/V Options   ";}
  if (mmitemnum == 2) {CurMenuItem = "  Accessories   ";}
  if (mmitemnum == 3) {CurMenuItem = "    Lighting    ";}
  lcd.setCursor(0, 0);
  lcd.print("   MAIN MENU:   ");
  lcd.setCursor(0, 1);
  lcd.println(CurMenuItem);
buttonstate = "1111";
goto CheckButtons;
Serial.println(buttonstate);
  if (buttonstate = "1101"){
    if (mmitemnum < 3) {
      mmitemnum = mmitemnum + 1;
    }
    delay(100);
    }
  if (buttonstate = "1011"){
    if (mmitemnum > 1) {
      mmitemnum = mmitemnum - 1;
    }
    delay(100);
  }
  if (buttonstate = "1110"){
    delay(100);
    if (mmitemnum == 1) {goto AVMENU;}
     if (mmitemnum == 3) {goto LIGHTMENU;}
  }
goto Check4BT;

AVMENU:
  if (avitemnum == 1) {CurMenuItem = " 1.             ";}
  if (avitemnum == 2) {CurMenuItem = " 2.             ";}
  if (avitemnum == 3) {CurMenuItem = " 3. Radio Reset ";}
  lcd.setCursor(0, 0);
  lcd.print("  AUDIO/VIDEO   ");
  lcd.setCursor(0, 1);
  lcd.print(CurMenuItem);
buttonstate = "1111";
goto CheckButtons;
  if (buttonstate == "0111"){
    delay(100);
    goto MainMenu;
  }
  if (buttonstate == "1101"){
    if (avitemnum < 3) {
      avitemnum++;
    }
    delay(100);
    }
  if (buttonstate == "1011"){
    if (avitemnum > 1) {
      avitemnum--;
    }
    delay(100);
  }  
  if (buttonstate == "1110"){
    delay(100);
    if (avitemnum == 3) {goto RadioReset;}
  }
goto AVMENU;

ACCMENU:
  goto MainMenu;
  
LIGHTMENU:
  if (LTitemnum == 1) {
    if (interiorlts == 0) {
      CurMenuItem = " INT Lights OFF ";
    }
    if (interiorlts == 1) {
      CurMenuItem = " INT Lights ON  ";
    }
  }
  if (LTitemnum == 2) {
    if (cargolts == 0) {
      CurMenuItem = "Cargo Lights OFF";
    }
    if (cargolts == 1) {
      CurMenuItem = "Cargo Lights ON ";
    }
  }
  if (LTitemnum == 3) {CurMenuItem = " 3.             ";}
  lcd.setCursor(0, 0);
  lcd.print("    LIGHTING    ");
  lcd.setCursor(0, 1);
  lcd.print(CurMenuItem);
  buttonstate = "1111";
  if (buttonstate == "0111"){
    delay(100);
    goto MainMenu;
  }
  if (buttonstate == "1101"){
    if (LTitemnum < 3) {
      LTitemnum++;
    }
    delay(100);
    }
  if (buttonstate == "1011"){
    if (LTitemnum > 1) {
      LTitemnum--;
    }
    delay(100);
  }  
  if (buttonstate == "1110"){
    delay(100);
    if (LTitemnum == 1) {
      if (interiorlts == 0) {
        interiorlts = 1;
        digitalWrite(12, HIGH);
        goto LIGHTMENU;
      }
      if (interiorlts == 1) {
        interiorlts = 0;
        digitalWrite(12, LOW);
        goto LIGHTMENU;
      }
    }
    if (LTitemnum == 2) {
      if (cargolts == 0) {
        cargolts = 1;
        digitalWrite(13, HIGH);
        goto LIGHTMENU;
      }
      if (cargolts == 1) {
        cargolts = 0;
        digitalWrite(13, LOW);
        goto LIGHTMENU;
      }
    }
  }
goto LIGHTMENU;

RadioReset:
  digitalWrite(11, HIGH);
  delay(3000);
  digitalWrite(11, LOW);
  lcd.setCursor(0, 1);
  lcd.print("   COMPLETE!    ");
  delay(5000);
return;
 
Check4BT:
  if (Serial.available() > 0)
    {char btread = Serial.read();
    }
  if (btread == "L")
    {
      digitalWrite(A0, LOW);
      delay(500);
      digitalWrite(A0, HIGH);
      lcd.setCursor(0, 0);
      lcd.print("   DOORS ARE    ");
      lcd.setCursor(0, 1);
      lcd.print("    LOCKED!     ");
      delay(4000);
  }
  if (btread == "U")
    {
      digitalWrite(A5, LOW);
      delay(500);
      digitalWrite(A5, HIGH);
      lcd.setCursor(0, 0);
      lcd.print("   DOORS ARE    ");
      lcd.setCursor(0, 1);
      lcd.print("   UNLOCKED!    ");
      delay(4000);
  }
btread = "";
return;
}
buttonstate = button1 + button2 + button3 + button4;

Huh?

buttonstate you have declared as a String

digitalRead() returns ints

OK. But if I declare buttonstate as an int will it not return 4 as opposed to 1111? And isn't it converted to a string at this point anyway? I still don't get why I can't make the ifthens "see" it.

I know I have 1111 or 1101 or 1011, etc. at this point, but what do I really have?
Do I have a string of "1101"?
or a stack of integers like INT1INT1INT0INT1 ?
or a stack of independent strings of single 1's ?
or a big integer of 1101(one thousand, one hundred, one.)

And WHATEVER it ends up, why can't I get the ifthens to see it?

  1. get rid of the gotos
  2. if you want to represent the state of a single bit, use an appropriate data type. If you want to represent the state of several bits, pack them into an appropriate data type. String is not an appropriate data type.

For starters, get rid of the String class (note the capital 'S'). It can fragment memory and is wasteful. Replace all Strings with char arrays. Next, use functions in your code. For example, checking the buttons should be a function, not a BASIC gosub, which you seem to use:

#define BUTTONSUSED    4    // The number of buttons you are reading

#define ASCIIONE      49    // The ASCII code for the '1' digit character

char buttonstate[BUTTONSUSED + 1};  // Need an extra element to add null termination character '\0'

int buttonPins[] = {A1, A2, A3, A4};


void CheckButtons(char buttonState[], int buttonPins[])
{
  int onOff[BUTTONSUSED];
  int i;

  memset(buttonState, ASCIIONE, sizeof(buttonState));   // Set all string elements to '1'
  
  for (i = 0; i < BUTTONSUSED; i++) {
    onOff[i] = digitalRead(buttonPins[i]);
    if (onOff[i] == HIGH) {           // Return High or Low?
      buttonState[i] = '0';           // Why not '1' to be consistent with logic state?
    } else {
      buttonState[i] = '1';           // Why not '0' to be consistent with logic state?
    }
  }
}

In setup(), you can use the array of buttons to make things simpler:

  for (i = 0; i < BUTTONSUSED; i++) {
    pinMode(buttonPins[i], INPUT);
    digitalWrite(buttonPins[i], HIGH);
  }

Now in loop(), you can start it with:

void loop() {

  CheckButtons(buttonState, buttonPin]);   // Read the buttons and fill the string...
  ShowButtonState(buttonState);            // Show what we found...

  // The rest of your code, which is so convoluted I can't follow it...
}

You need to spend some time with a good C tutorial book written for the Arduino. Then come back and try to purge all of the Strings and goto's

EDIT: the onOff[] array in CheckButtons() could be a simple int .

thasatelliteguy:
And WHATEVER it ends up, why can't I get the ifthens to see it?

Because the if/else is never executed. Get rid of the gotos and use functions. This is what happens:

When loop starts, you jump to DEBUG; near the end of DEBUG, you jump to CheckButtons. And at the end of CheckButtons you use 'return'. Any idea what 'return' does? It definitely does not get you to the next line in DEBUG. No, it exits the loop().

And the next iteration of loop you start all over again.

Use functions and you will get a lot further !!