Setting up and adjusting values within a menu

Hello,

I’ll just briefly explain my project and what I’m trying to achieve.

My project is an adjustable Voltage Sensing Relay working injunction with an LCD and buttons (the cheap copy of DFROBOT Shield).

eventually I’ll change the coding to use with a rotatory encoder and do away with the shield.

The way my project works is by measuring the value of 1 input, comparing it a threshold and depending on the value of input 1 & the threshold set, the output relay will be energised until certain conditions are met.

The options I want in the menu are:

1: Home > Shows “/ voltage1 / voltage2 / Current”

2: Resting Voltage> “Adjusts the threshold in the code”

3: Running Voltage > “Adjusts the threshold in the code”

4: Off Voltage > “Adjusts the off point threshold in the code”

I’ve achieved showing the voltage’s and current, but when I try to integrate options 2,3 & 4 this is where I face problems and I’ve realised my understanding of menu’s and how they function isn’t right.

I’d like it to work by having my main code run all the time and I use the menu code to pick out bits I want to display on the LCD or access part of the code I want to change… e.g the threshold points.
this way of thinking doesn’t seem to work.

It’s hard to explain, I’m fairly new to arduino and have been working on this project for MUCH longer than I’d like to admit.

I thought id ask for some help seen as I’m so stuck.

Please could you look at my poor excuse for code and try to help me understand what I’m doing wrong / right!

Thanks for your help and suggestions.

VSR_WITH_MENU.ino (17 KB)

You seem to have created an infinite loop in:

void menuItem1() { // Function executes when you select the 2nd item from main menu
  int activeButton = 0;

  menustate = true;
  lcd.clear();


  while (menustate == true) {

    //Setup LCD ready for printed values
    lcd.setCursor(0, 0); // set cursor 0 space in, on line 0 (top)
    lcd.print("V1");// Print a message to the LCD.
    lcd.setCursor (0, 1); //
    lcd.print("V2");// Print a message to the LCD.
    lcd.setCursor(9, 1); //
    lcd.print("A"); // print amp




    // Setup ACS712 current module
    RawValue = analogRead(analogIn);
    Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
    Amps = ((Voltage - ACSoffset) / mVperAmp);
    readKey = analogRead(0);

    // voltage Setup input 1 & 2
    int sensorValue1 = analogRead(A2);
    float voltage1 = sensorValue1 * (50.00 / 1024); // resistor divider, 50volt max input / 1024

    int sensorValue2 = analogRead(A3); // sensorvalue2 is A2
    float voltage2 = sensorValue2 * (50.00 / 1024); // resistor divider, 50volt max input / 1024


    lcd.setCursor(3, 0); //set coursor on 3 spaces in on the top line
    if (voltage1 >= 1) {
      lcd.print(voltage1, 2); // print the value of voltagelcd.print (voltage1,2);
    }
    else {
      lcd.print ("0.000"); // otherwise print 0.000
    }

    if (digitalState == true) {   // digitalWrite the output dependent on the digitalState
      digitalWrite(output, HIGH); // turn the relay on if digitalState == true
    }
    else                          // otherwise
      digitalWrite(output, LOW); // turn the relay off

    lcd.setCursor(3, 1); //set coursor on 3 spaces in on the top line
    if (voltage2 >= 1)  // if voltage2 is greater or equal to 1
    {
      lcd.print(voltage2, 2); // print the value of voltage2 to the LCD
    }
    else
    {
      lcd.print ("0.000"); // otherwise print 0.000
    }
    if (RawValue <= 512) // AMPS - if the RAW value is less than 512,
    {
      lcd.setCursor(11, 1);
      lcd.print("0.00"); //  print 0.00 to the LCD
    }
    else                  // otherwise
    {
      lcd.setCursor(11, 1); //set coursor
      lcd.print (Amps, 2); // print the value of Amps to 3 deciamal places 00.00
    }
    delay(300); // delay 300ms


    // The Switching.... ( code running all the time )


    if (voltage1 <= 13.6) {    // if the voltage is below 13.6v (THRESHOLD)...  MENU OPTION 2 " Resting V "
      loopTime = millis();     // save the loopTime as the millis at that point in time
      digitalState = false;   // write the digitalState to false... Output == OFF
    }
    else if (voltage1 >= 13.7) // Otherwise if the voltage is above 13.7 (THRESHOLD)... MENU OPTION 2 " Running V "
      currentTime = millis(); //the currentTime is millis at that point in time
    if (currentTime >= (loopTime + 4000)) // if currentTime is equal or greater than the loopTime + 4 seconds , wait 4 second .....
    {
      digitalState = true;  // write the digitalState as true, (Output == ON)
      loopTime = currentTime;  // save the loopTime as the currentTime
      if ((voltage1 < 13.7) or (voltage2 >= 14.3)) //if voltage1 is LOWER than 13.7v ( "Running V") or if voltage2 is EQUAL or HIGHER than 14.3v ( Battery 2, OFF Voltage )
        digitalState = false; // Wright the digitalState to false (output == OFF)
      (loopTime + 4000);
    }

The while loop runs infinite since “menustate == true” forever.

Yeah that’s right, one more line down from that code I’ve done an if break statement… so if I hold the back button down for .5sec it returns to the menu list

  else if (voltage1 >= 13.7) // Otherwise if the voltage is above 13.7 (THRESHOLD)... MENU OPTION 2 " Running V "
      currentTime = millis(); //the currentTime is millis at that point in time
    if (currentTime >= (loopTime + 4000)) // if currentTime is equal or greater than the loopTime + 4 seconds , wait 4 second .....
    {
      digitalState = true;  // write the digitalState as true, (Output == ON)
      loopTime = currentTime;  // save the loopTime as the currentTime
      if ((voltage1 < 13.7) or (voltage2 >= 14.3)) //if voltage1 is LOWER than 13.7v ( "Running V") or if voltage2 is EQUAL or HIGHER than 14.3v ( Battery 2, OFF Voltage )
        digitalState = false; // Wright the digitalState to false (output == OFF)
      (loopTime + 4000);
    }
     if (readKey < 790) {
      break;
    }

  }
  {