Return() in Control Structure

I have hit another brick wall with my Menu Structures. As part of mu options I want the opportunity to go back to the previous menu. In old money I would use an instruction, 'Return' But something peculiar is preventing my from firstly compiling or Arduino works in some other weird and wonderful way.

Here is my code:

As before I am working this in blocks in a scratchpad away from the main program which is quite huge now.

  
  #include <Wire.h>
  #include <LiquidCrystal_I2C.h>
  #include <EEPROM.h>
  
  LiquidCrystal_I2C lcd(0x3F, 20, 4); // 20x4 LCD Display
  
  // constants won't change. They're used here to set pin numbers:
  const int P_0 = 10;     // the number of the pushbutton pin
  const int P_1 = 9;
  const int P_2 = 8;
  const int P_3 = 7;
  const int P_4 = 6;
  const int ledPin =  13;      // the number of the LED pin
  
  
  // variables will change:
  
  int buttonState = 0;         // variable for reading the pushbutton status
  int var = 0;
  int Count = 0;
  int ButtonPress = 0;
  int menu = 0;
  int XStepsPermm = 0;
  int YStepsPermm = 0;
  int ZStepsPermm = 0;
  int DropNumber = 0;
  int PassNumber = 0;
  void setup() 
{
  
    lcd.begin();
    lcd.backlight();
    lcd.clear();
  
    // initialize the LED pin as an output:
    pinMode(ledPin, OUTPUT);
  
    // initialize the pushbutton pin as an input:
    pinMode(P_0, INPUT);    // Select
    pinMode(P_1, INPUT);    // Left
    pinMode(P_2, INPUT);    // Up
    pinMode(P_3, INPUT);    // Down
    pinMode(P_4, INPUT);    // Right
  
    pinMode(P_0, INPUT_PULLUP);
    pinMode(P_1, INPUT_PULLUP);
    pinMode(P_2, INPUT_PULLUP);
    pinMode(P_3, INPUT_PULLUP);
    pinMode(P_4, INPUT_PULLUP);
    
    PassNumber = EEPROM.read(0);
    XStepsPermm = EEPROM.read(1);
    YStepsPermm = EEPROM.read(2);
    ZStepsPermm = EEPROM.read(3);
    DropNumber  = EEPROM.read(4);
}
  void loop() 
{


    OptionSelect();



}
 void OptionSelect()
{
 //************** OPTIONS SELECT ****************   
//  lcd.clear();
 
  if (digitalRead(P_3) == LOW)    //Down
{
    Count++;
    delay (300);
}
//    lcd.setCursor(5,3);
//    lcd.print(Count);
  if (Count >=3) {
      Count= 0;
    
}
  switch (Count) 
  {
  case 0:
    lcd.setCursor(4, 0);
    lcd.print("OPTIONS MENU");
    lcd.setCursor(0, 2);
    lcd.print("  >EDIT PARAMETERS  ");
  if (digitalRead(P_0) == LOW)
{   
    lcd.clear(); 
//    EditParams();                      // Call EditParams Routine
}    break;
  case 1:
    lcd.setCursor(4, 0);
    lcd.print("OPTIONS MENU");
    lcd.setCursor(0, 2);
    lcd.print("  >SETUP            ");
  if (digitalRead(P_0) == LOW)
{   
    lcd.clear(); 
//    Setup();                      // Call EditParams Routine
}   break;

  case 2:
    lcd.setCursor(4, 0);
    lcd.print("OPTIONS MENU");
    lcd.setCursor(0,2);
    lcd.print("  >RETURN           ");
  if (digitalRead(P_0) == LOW)
{  
//    Return();                     // Return to previous Menu
    break;
    lcd.clear();
}
 //   lcd.setCursor(4, 3);
//    lcd.print("COMMIT (Y/N)");
//    if (digitalRead(P_0) == LOW)    // check if P0 (Select) is pressed.
//{
//    RunSys();   

}

Use

return;

to exit the function currently being executed.

So you are trying to call a function named "Return". Did you define that function in one of your source files? What is the exact goes wrong when you try to compile?

I get no errors when I compile your code except that the LiquidCrystal_I2C library I have doesn't have a 'begin' function and there was one '}' missing at the end.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>

LiquidCrystal_I2C lcd(0x3F, 20, 4); // 20x4 LCD Display

// constants won't change. They're used here to set pin numbers:
const int P_0 = 10;     // the number of the pushbutton pin
const int P_1 = 9;
const int P_2 = 8;
const int P_3 = 7;
const int P_4 = 6;
const int ledPin =  13;      // the number of the LED pin


// variables will change:

int buttonState = 0;         // variable for reading the pushbutton status
int var = 0;
int Count = 0;
int ButtonPress = 0;
int menu = 0;
int XStepsPermm = 0;
int YStepsPermm = 0;
int ZStepsPermm = 0;
int DropNumber = 0;
int PassNumber = 0;

void setup()
{
//  lcd.begin();
  lcd.backlight();
  lcd.clear();

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(P_0, INPUT);    // Select
  pinMode(P_1, INPUT);    // Left
  pinMode(P_2, INPUT);    // Up
  pinMode(P_3, INPUT);    // Down
  pinMode(P_4, INPUT);    // Right

  pinMode(P_0, INPUT_PULLUP);
  pinMode(P_1, INPUT_PULLUP);
  pinMode(P_2, INPUT_PULLUP);
  pinMode(P_3, INPUT_PULLUP);
  pinMode(P_4, INPUT_PULLUP);

  PassNumber = EEPROM.read(0);
  XStepsPermm = EEPROM.read(1);
  YStepsPermm = EEPROM.read(2);
  ZStepsPermm = EEPROM.read(3);
  DropNumber  = EEPROM.read(4);
}

void loop()
{
  OptionSelect();
}

void OptionSelect()
{
  //************** OPTIONS SELECT ****************
  //  lcd.clear();

  if (digitalRead(P_3) == LOW)    //Down
  {
    Count++;
    delay (300);
  }
  
  //    lcd.setCursor(5,3);
  //    lcd.print(Count);
  if (Count >= 3)
  {
    Count = 0;
  }
  
  switch (Count)
  {
    case 0:
      lcd.setCursor(4, 0);
      lcd.print("OPTIONS MENU");
      lcd.setCursor(0, 2);
      lcd.print("  >EDIT PARAMETERS  ");
      if (digitalRead(P_0) == LOW)
      {
        lcd.clear();
        //    EditParams();                      // Call EditParams Routine
      }    
      break;
      
    case 1:
      lcd.setCursor(4, 0);
      lcd.print("OPTIONS MENU");
      lcd.setCursor(0, 2);
      lcd.print("  >SETUP            ");
      if (digitalRead(P_0) == LOW)
      {
        lcd.clear();
        //    Setup();                      // Call EditParams Routine
      }   
      break;

    case 2:
      lcd.setCursor(4, 0);
      lcd.print("OPTIONS MENU");
      lcd.setCursor(0, 2);
      lcd.print("  >RETURN           ");
      if (digitalRead(P_0) == LOW)
      {
        //    Return();                     // Return to previous Menu
        break;
        lcd.clear();
      }
      //   lcd.setCursor(4, 3);
      //    lcd.print("COMMIT (Y/N)");
      //    if (digitalRead(P_0) == LOW)    // check if P0 (Select) is pressed.
      //{
      //    RunSys();

  } // End 'switch'
}

Keith
At the risk of offending, the Arduino Reference is your friend. Here's what it has to say about return, which is a control structure, or control element, and not a function, hence it has no ():

That is what I want it to do. when the 'Select' Button is hit on the 'Return Option I want it to go back to the previous menu. I have gone through the Arduino Reference Library and under the 'Return' keyword in the control structure section it does give an example but it is a bit vague and difficult for a 21 day newbie to get my head around.
However, removing both parenthesis brackets from behind the Return instruction has allowed me to compile so I need to put it into the main program and see what happens.

@jremington I mustn't have picked up the last closing Curly Bracket when I copied and pasted the code.

p.s. @camsysca Us Geordies have Rhino hide for skin. Takes a hell of a lot to offend me !

@keithfitch the word and concept of "return" is being used it seems with multiple meanings.

There's the meaning in the programming language, where return in the various ways it appears is used to return from a function that has been called from another function at a higher level usually.

Code needs some value or something done.

Code calls a function, which runs and computes a value or does something

Code returns to the caller, job done or here's your value sir!

But return in the context of a menu system means return to the previous menu. It may or may not have the least thing to do with how the program is presenting the menus, taking input on the choices provided by a menu or sub-menu, or taking action on the selected menu item.

Researching return the kind used in code in furtherance of figuring out how to do a multilevel menu is a distraction.

Even if you do end up with code that structurally resembles your menu system's layout, even if return does end up being implemented with actual return statements.

a7

When you enter a sub-menu, record what menu you came from. When you want to 'return' from the sub-menu, set the recorded menu as the current menu.

That is all done in the parts of the program you did not show so I can't be more specific.

{ braces }

[ brackets ]

( parentheses )

There is no need to ever use the word curly. Those symbols have unique and standard names.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.