Local menu problem

I Have an issue where for the first time after a reset when I press the up arrow panel button which I have assigned an interupt it is supposed to display the main menu but actually displays the second menu. I have to press back menu option to get to the main menu. Once I do this after startup it works as it should. Maybe someone can see the problem.

The readbuttons function is exactly the same as the demo

Any ideas are appreciated.

Here I assign the interupts

  attachInterrupt(25, upPress, FALLING);
  attachInterrupt(24, enterPress, FALLING);


Here is the upPress function. As you can see I am trying to reset all of the menu option to defaults.

void upPress()
{
  if (runMode)
  {
    //    SerialUSB.println("runMode to 0");
    MenuLevel = 0; //menu tree depth -> second level
    MenuID = 10; //unique menu id -> has to be unique for each menu on the same menu level.
    buttonUpState = 0;
    prevBtnUp = 0;
    lastBtnUp = 0;
    enterPressed = 0;
    buttonEnterState = 0;
    prevBtnEnt = 0;
    lastBtnEnt = 0;
    backLightDimmed = 0;
    analogWrite(backlightPin, (map(5, 5, 0, 255, 0)));
    glBackLightDelayTime = millis();
    lRunModeTimeout = millis();
    MenuMain();
    runMode = 0;
  }
}

The enterPress function.

void enterPress()
{
  if (runMode)
  {
    backLightDimmed = 0;
    analogWrite(backlightPin, (map(5, 5, 0, 255, 0)));
    glBackLightDelayTime = millis();
  }
}

The loop.

void loop()
{
  if (runMode)
  {

    //    if (bAuthenticated)
    //    {
    //      if ((millis() - lLogintimeOut) > 10000)
    //      {
    //        bAuthenticated = false;
    //        loggedIn = 0;
    ////        currPage = 1;
    //      }
    //    }
    
    listenForEthernetClients();

    if ((millis() - lSpLoop) > 5000 && sMode > 0)
    {
      lSpLoop = millis();
      occProcess();
      outputProcess();
      monitorProcess();
      outputProcessPrint();
    }

    if ((millis() - glBackLightDelayTime) > 30000 && backLightDimmed == 0)
    {
      //isLocked = true;
      backLightDimmed = 1;
      analogWrite(backlightPin, (map(0, 5, 0, 255, 0)));
    }
  }
  else // use local menu buttons
  {
    ReadButtons(); //check buttons
    Navigate(); //update menus and perform actions

    if ((millis() - lRunModeTimeout) > 30000)
    {
      setRunMode();
    }
  }
}

Here are the menus.
This is the menu I want on the first press of the up arrow.

void MenuMain()
{
  //menu inintialisers
  channel = 0; //starting row position of the cursor (top row) - controlled by the button panel counter
  channelUpLimit = 6; //upper row limit
  channelLowLimit = 0;
  MenuLevel = 0; //menu tree depth -> second level
  MenuID = 10; //unique menu id -> has to be unique for each menu on the same menu level.
  enterPressed = 0; //clears any possible accidental "Enter" presses that could have been caried over from the previous menu
  lcd.clear(); //clear the screen
  //actual user content on the screen
  lcd.setCursor(6, 0); //set the cursor to the sixth pixel from the left edge, first row.
  lcd.print("Main Opt"); //print text on screen
  lcd.setCursor(6, 1); //set the cursor to the sixth pixel from the left edge, first row.
  lcd.print("Temperature Opt"); //print text on screen
  lcd.setCursor(6, 2); //set the cursor to the sixth pixel from the left edge, first row.
  lcd.print("Voltage Opt"); //print text on screen
  lcd.setCursor(6, 3); //set the cursor to the sixth pixel from the left edge, first row.
  lcd.print("Percent Opt"); //print text on screen
  lcd.setCursor(6, 4); //set the cursor to the sixth pixel from the left edge, third row.
  lcd.print("Save Settings"); //print text on screen
  lcd.setCursor(6, 5); //set the cursor to the sixth pixel from the left edge, second row.
  lcd.print("Run Program"); //print text on screen
  lcd.setCursor(6, 6);
  lcd.print("About");
  ScrollCursor(); //enable the moving cursor (note that this function is not called in the splash screen, thus disabling the cursor)
}

This is the menu that I get on the first press of the up arrow.

void MenuSetupPg1()
{
  channel = 0;
  channelUpLimit = 3;
  channelLowLimit = 0;
  MenuID = 11;
  MenuLevel = 1;
  enterPressed = 0;
  lcd.clear();
  lcd.setCursor(6, 0);
  lcd.print("Mode(0,1,2");
  lcd.setCursor(80, 0);
  lcd.print(sMode, 1);
  lcd.setCursor(6, 1);
  lcd.print("Occ Mode(0,1");
  lcd.setCursor(80, 1);
  lcd.print(sModeOcc, 1);
  lcd.setCursor(6, 2);
  lcd.print("Reset");
  lcd.setCursor(6, 3);
  lcd.print("Back");
  ScrollCursor();
}

Here is the navigate function.

  if (runMode == 0)
  {
    if (valueEditing != 1)
    {

      if (MenuLevel == 0)
      { //check if current activated menu is the 'Main menu' (first level)
        if (channel == 0 && enterPressed == 1) MenuSetupPg1();
        if (channel == 1 && enterPressed == 1) MenuSetupPg2();
        if (channel == 2 && enterPressed == 1) MenuSetupPg3();
        if (channel == 3 && enterPressed == 1) MenuSetupPg4();
        if (channel == 4 && enterPressed == 1) writeToEEprom();
        if (channel == 5 && enterPressed == 1) setRunMode();
        if (channel == 6 && enterPressed == 1) AboutPg1();
      }

      if (MenuLevel == 1)
      {

        if (MenuID == 11)
        {
          if (channel == 0 && enterPressed == 1) //using 'value editing mode' to edit a variable using the UI
          {
            targetMax = 2;
            targetMin = 0;
            TargetValue = sMode; //copy variable to be edited to 'Target value'
            sMode = EditValue();
          }
          if (channel == 1 && enterPressed == 1) //using 'value editing mode' to edit a variable using the UI
          {
            targetMax = 1;
            targetMin = 0;
            TargetValue = sModeOcc; //copy variable to be edited to 'Target value'
            sModeOcc = EditValue();
          }
          if (channel == 2 && enterPressed == 1) resetFunc();
          if (channel == 3 && enterPressed == 1) MenuMain();
        }

        if (MenuID == 12)
        {

          if (channel == 0 && enterPressed == 1)
          {
            targetMax = 90;
            targetMin = tMin[1] + 2;
            TargetValue = tMax[1]; //copy variable to be edited to 'Target value'
            tMax[1] = EditValue();
          }

          if (channel == 1 && enterPressed == 1)
          {
            targetMax = 90;
            targetMin = tMin[2] + 2;
            TargetValue = tMax[2]; //copy variable to be edited to 'Target value'
            tMax[2] = EditValue();
          }

          if (channel == 2 && enterPressed == 1)
          {
            targetMax = tMax[1] - 2;
            targetMin = 60;
            TargetValue = tMin[1]; //copy variable to be edited to 'Target value'
            tMin[1] = EditValue();
          }

          if (channel == 3 && enterPressed == 1)
          {
            targetMax = tMax[2] - 2;
            targetMin = 60;
            TargetValue = tMin[2]; //copy variable to be edited to 'Target value'
            tMin[2] = EditValue();
          }

          if (channel == 4 && enterPressed == 1) MenuMain();
        }// menu id 1


        if (MenuID == 13)
        {
          if (channel == 0 && enterPressed == 1) //using 'value editing mode' to edit a variable using the UI
          {
            targetMax = 10;
            targetMin = 4;
            TargetValue = vMax[1]; //copy variable to be edited to 'Target value'
            vMax[1] = EditValue();
          }

          if (channel == 1 && enterPressed == 1) //using 'value editing mode' to edit a variable using the UI
          {
            targetMax = 10;
            targetMin = 4;
            TargetValue = vMax[2]; //copy variable to be edited to 'Target value'
            vMax[2] = EditValue();
          }

          if (channel == 2 && enterPressed == 1)
          {
            targetMax = 8;
            targetMin = constrain(vMin[1], 0, (vMax[1] - 2));
            TargetValue = vMin[1]; //copy variable to be edited to 'Target value'
            vMin[1] = EditValue();
          }

          if (channel == 3 && enterPressed == 1)
          {
            targetMax = 8;
            targetMin = constrain(vMin[2], 0, (vMax[2] - 2));
            TargetValue = vMin[2]; //copy variable to be edited to 'Target value'
            vMin[2] = EditValue();
          }

          if (channel == 4 && enterPressed == 1) MenuMain();
        }// menu id 2

        if (MenuID == 14)
        {
          if (channel == 0 && enterPressed == 1) //using 'value editing mode' to edit a variable using the UI
          {
            targetMax = 100;
            targetMin = 20;
            TargetValue = pMax[1]; //copy variable to be edited to 'Target value'
            pMax[1] = EditValue();
          }

          if (channel == 1 && enterPressed == 1) //using 'value editing mode' to edit a variable using the UI
          {
            targetMax = 100;
            targetMin = 20;
            TargetValue = pMax[2]; //copy variable to be edited to 'Target value'
            pMax[2] = EditValue();
          }

          if (channel == 2 && enterPressed == 1)
          {
            targetMax = 80;
            targetMin = constrain(pMin[1], 20, (pMax[1] - 2));
            TargetValue = pMin[1]; //copy variable to be edited to 'Target value'
            pMin[1] = EditValue();
          }

          if (channel == 3 && enterPressed == 1)
          {
            targetMax = 80;
            targetMin = constrain(pMin[2], 20, (pMax[2] - 2));
            TargetValue = pMin[2]; //copy variable to be edited to 'Target value'
            pMin[2] = EditValue();
          }

          if (channel == 4 && enterPressed == 1) MenuMain();
        }// menu id 14

        if (MenuID == 15)
        {
          if (channel == 5 && enterPressed == 1) MenuMain();
        }
      }
    }

    //dont remove this part
    if (channel != lastChannel && valueEditing != 1)
    { //updates the cursor position if button counter changed and 'value editing mode' is not running
      ScrollCursor();
    }
  }
}

Dries Van Craen
Dries Van Craen
54
| 0 0 0
Asked on 6/15/21, 12:53 PM
0
vote
1088 Views

Your answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Ask a Question

Keep Informed

About This Forum

This community is for professionals and enthusiasts of our products and services.

Read Guidelines

Question tools

63 follower(s)

Stats

Asked: 6/15/21, 12:53 PM
Seen: 1088 times
Last updated: 6/15/21, 12:53 PM