Integrating and LCD menu into sketch

Hello,

I am new to the Arduino and this forum.

I have put together a sketch that reads an analog signal from a potentiometer to determine the position of a radial stacking conveyor, and also reads an analog signal from a time-of-flight laser to determine the lever in a bin(s). The signals are programmed to light a series of indicator lights to alert the operator. I have a 2 line LCD button shield connected that reports the Conveyor Position on line 1, and the Bin Level on line 2. This sketch and system works well (Conveyor Control Sketch Below- some functions left out because too long to post).

/*
 * COASTAL PIPELINE PRODUCTS
 * CONVEYOR BIN POSITION AND BIN LEVEL CONTROL
 */

int ConveyorPot = A0;            //variable for storing the conveyor potentiometer value
int BinLevel = A1;             //variable for storing the bin level value

//LEDs are connected to these pins
int BlueBin1 = 13;
int BlueBin2 = 12;
int BlueBin3 = 11;
int BlueBin4 = 10;

int Red100 = 7;
int Yellow75 = 6;
int Green50 = 5;
int Green25 = 4;

int LaserOn = 9;

int ConvStop = 3;

int P1 = 100; //Bin position 1 start point
int P2 = 200; //Bin position 1 stop point
int P3 = 300; //Bin position 2 sart poing
int P4 = 400; //Bin position 2 stop point
int P5 = 500; //Bin position 3 start point
int P6 = 600; //Bin position 3 stop point
int P7 = 700; //Bin position 4 start point
int P8 = 800; //Bin position 4 stop point

int BL1 = 204;
int BL2 = 411;
int BL3 = 613;
int BL4 = 818;

void setup() {
  Serial.begin(9600);           //start a serial connection with the computer

  //set the LED pins to output
  pinMode(BlueBin1, OUTPUT);
  pinMode(BlueBin2, OUTPUT);
  pinMode(BlueBin3, OUTPUT);
  pinMode(BlueBin4, OUTPUT);

  pinMode(Red100, OUTPUT);
  pinMode(Yellow75, OUTPUT);
  pinMode(Green50, OUTPUT);
  pinMode(Green25, OUTPUT);

  pinMode(LaserOn, OUTPUT);

  pinMode(ConvStop, OUTPUT);

}

void loop() {

  ConveyorPot = analogRead(A0);           //read the value of the conveyor potentiometer
  // delay(10);                           //might be needed if analog signal wanders
  // ConveyorPot = analogRead(A0);        //might be needed if analog signal wanders
  // delay(10);                           //might be needed if analog signal wanders

  BinLevel = analogRead(A1);              //read the value of the laser bin level output
  // delay(10);                           //might be needed if analog signal wanders
  //BinLevel = analogRead(A1);            //might be needed if analog signal wanders
  // delay(10);                           //might be needed if analog signal wanders

  Serial.print("ConveyorPot value:");
  Serial.print(ConveyorPot);              //print the conveyor potentiometer value to the serial monitor
  Serial.print("  BinLevel value:");
  Serial.println(BinLevel);               //print the bin level value to the serial monitor

  // Control the conveyor position lights
  
  if (ConveyorPot >= 0 &&  ConveyorPot < P1)
    BinOff();
  if (ConveyorPot >= P1 && ConveyorPot <= P2)
    Blue1();
  if (ConveyorPot > P2 && ConveyorPot < P3)
    BinOff();
  if (ConveyorPot >= P3 && ConveyorPot <= P4)
    Blue2();
  if (ConveyorPot > P4 && ConveyorPot < P5)
    BinOff();
  if (ConveyorPot >= P5 && ConveyorPot <= P6)
    Blue3();
  if (ConveyorPot > P6 && ConveyorPot < P7)
    BinOff();
  if (ConveyorPot >= P7 && ConveyorPot <= P8)
    Blue4();
  if (ConveyorPot > P8 && ConveyorPot <= 1023)
    BinOff();

//Control the bin level lights

if (BinLevel >= 0 &&  BinLevel < BL1)
  Level1();
if (BinLevel >= BL1 && BinLevel < BL2)
  Level2();
if (BinLevel >= BL2 && BinLevel < BL3)
  Level3();
if (BinLevel >=  BL3&& BinLevel < BL4)
  Level4();
if (BinLevel >= BL4 && BinLevel <= 1023)
  Level5();

delay(100);                             //short delay so that the printout is easier to read
}

//_____ Functions for controlling the conveyor position lights_________

void Blue1 () {
  //turn on BlueBin1 output light
  digitalWrite(BlueBin1, HIGH);
  digitalWrite(BlueBin2, LOW);
  digitalWrite(BlueBin3, LOW);
  digitalWrite(BlueBin4, LOW);
  digitalWrite(LaserOn, HIGH);

}

The current code has the Conveyor Positions and Bin Level settings hard coded in the sketch, and I would like to include a menu system to allow me to change the settings by entering a menu after pushing the ‘Select’ button. I understand and experimented with saving these setting to the EEPROM. I have found a simple menu system that I think would work well (attached below), but I have spent days trying to integrate it into my original conveyor control sketch. I seem to be having trouble understanding how to “park” the conveyor control code and ‘step into’ the menu, then back. Especially since the menu code I am looking at begins to run immediately as it starts in “void setup”.

/*+++++++++++++++++++++++++++++++++++++++++++++++++++
Author      : Fahmi Ghani
Date        : 5 July 2015
Project     : LCD Menu Function
Component   : LCD Keypad shield
Description : Simple Menu function on LCD
              Button Function:
                Up/Down - change Menu Page
                Select - enter page
                Left - Exit
 
Video Link: http://youtu.be/cMqif5ICS5M
 
+++++++++++++++++++++++++++++++++++++++++++++++++++*/
 
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);  
 
int keypad_pin = A0;
int keypad_value = 0;
int keypad_value_old = 0;
 
char btn_push;
 
byte mainMenuPage = 1;
byte mainMenuPageOld = 1;
byte mainMenuTotal = 4;
 
void setup()
{
    lcd.begin(16,2);  //Initialize a 2x16 type LCD
 
    MainMenuDisplay();
    delay(1000);
}
void loop()
{
    btn_push = ReadKeypad();
   
    MainMenuBtn();
   
    if(btn_push == 'S')//enter selected menu
    {
        WaitBtnRelease();
        switch (mainMenuPage)
        {
            case 1:
              MenuA();
              break;
            case 2:
              MenuB();
              break;
            case 3:
              MenuC();
              break;
            case 4:
              MenuD();
              break;
        }
 
          MainMenuDisplay();
          WaitBtnRelease();
    }
   
 
 
    delay(10);
 
}//--------------- End of loop() loop ---------------------
void MenuA()
{  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Inside Menu A");
   
    while(ReadKeypad()!= 'L')
    {
        //Insert Task for Menu A here
       
    }
}
void MenuB()
{  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Inside Menu B");
   
    while(ReadKeypad()!= 'L')
    {
        //Insert Task for Menu B here
       
    }
}
void MenuC()
{  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Inside Menu C");
   
    while(ReadKeypad()!= 'L')
    {
        //Insert Task for Menu C here
       
    }
}
void MenuD()
{  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Inside Menu D");
   
    while(ReadKeypad()!= 'L')
    {
        //Insert Task for Menu D here
       
    }
}
 
void MainMenuDisplay()
{
    lcd.clear();
    lcd.setCursor(0,0);
    switch (mainMenuPage)
    {
        case 1:
          lcd.print("1. Menu A");
          break;
        case 2:
          lcd.print("2. Menu B");
          break;
        case 3:
          lcd.print("3. Menu C");
          break;
        case 4:
          lcd.print("4. Menu D");
          break;
    }
}
 
void MainMenuBtn()
{
    WaitBtnRelease();
    if(btn_push == 'U')
    {
        mainMenuPage++;
        if(mainMenuPage > mainMenuTotal)
          mainMenuPage = 1;
    }
    else if(btn_push == 'D')
    {
        mainMenuPage--;
        if(mainMenuPage == 0)
          mainMenuPage = mainMenuTotal;    
    }
   
    if(mainMenuPage != mainMenuPageOld) //only update display when page change
    {
        MainMenuDisplay();
        mainMenuPageOld = mainMenuPage;
    }
}
 
char ReadKeypad()
{
  /* Keypad button analog Value
  no button pressed 1023
  select  741
  left    503
  down    326
  up      142
  right   0
  */
  keypad_value = analogRead(keypad_pin);
 
  if(keypad_value < 100)
    return 'R';
  else if(keypad_value < 200)
    return 'U';
  else if(keypad_value < 400)
    return 'D';
  else if(keypad_value < 600)
    return 'L';
  else if(keypad_value < 800)
    return 'S';
  else
    return 'N';
 
}
 
void WaitBtnRelease()
{
    while( analogRead(keypad_pin) < 800){}
}

I could use some tips to get me in the right direction. Thanks - Don

another employee asking to get his work done

.

ieee48,

No, I am working on this because as a mechanical engineer, I like tinker with these things. This was the first real world example I could think of to try to use the Uno on, and I happen to have those analog devices.

If I was really going to build something for my employer, I would use a PLC not a toy.

Thanks for the help.

You can try using this menu system instead https://github.com/MajicDesigns/MD_Menu

It is made to keep out of the way and not run the application, like most others seem to want to do.

The examples with the library will show you how it can be used and there is a lot of documentation provided (docs folder).

You can see it being used in this sketch (https://github.com/MajicDesigns/PID-Fan-Control) where I was learning to tune a PID loop and used the menu system to enter the PID and other values for the experiment. This is explained on my blog at PID Control Experiment – Making the Testing Rig – Arduino++