The program flow doesn't return control to the loop

Hi,

I have interfaced a keypad, a LCD and established serial communication successfully to arduino mega 2560.

I set limits through the keypad and compare the limit value with the value read through serial port.

After the comparison, the process doesn't repeat. The LCD displays the result and doesn't display the menu for next cycle and repeat the operation.
How do i keep the arduino active using the loop?

#include <Keypad.h>
#include <LiquidCrystal.h>

const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'\n','0','\e','\b'}
};
byte rowPins[ROWS] = {30,31,32,33}; //connect to row pinouts 
byte colPins[COLS] = {34,35,36,37}; //connect to column pinouts

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char UL[6];
char LL[6];
int k = 0;
int l = 0;
long int uvalue;
long int lvalue;
int i = 0;

const int RLED = 26;
const int YLED = 27;
const int GLED = 28;

char inByte[6] = {0};
long int inValue = 12345;
int m = 0;

int n = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup(){
  Serial.begin(9600);

  lcd.begin(20,4);
  lcd.cursor();
pinMode(RLED, OUTPUT);
pinMode(YLED, OUTPUT);
pinMode(GLED, OUTPUT);
 
displayMenu();
}

void displayMenu(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("'A'-Enter the Limits");
lcd.setCursor(0, 2);
lcd.print("'B'-Reset");
}

void loop()
{
 
//displayMenu();
while (n<0) {
char key1 = keypad.getKey();
 
switch(key1) {
     
case 'A': 
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Upper Limit");
lcd.setCursor(0, 1);

while (k<5) {
     char key = keypad.getKey();
     if (key) {
            if(key == '\b'){
              lcd.rightToLeft();
              lcd.write(" ");
              lcd.leftToRight();
        UL[k]=0;
  k--;
    }
    else if(key == '\e'){
goto here;
    }
    else if(key != '\b'||'\e') {
       
       lcd.print(key);
       UL[k] = key;
    
   k++;
    }
  }
}
goto here;
here:
uvalue = atol(UL);
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Lower Limit");
lcd.setCursor(0, 1);
while(l<5) {
     char key = keypad.getKey();
     if (key) {
            if(key == '\b'){
              lcd.rightToLeft();
              lcd.write(" ");
              lcd.leftToRight();
        LL[l]=0;
  l--;
    }
    else if(key == '\e'){
    goto there;
    }
    else if(key != '\b'||'\e')
   {
       // Print a message to the LCD.  
       lcd.print(key);
       LL[l] = key;
    //Serial.println(key);
   l++;
    }
  }
}
lvalue = atol(LL);
delay(1000);
lcd.clear();
goto there;

there:

lvalue = atoi(LL);
delay(1000);

lcd.print("Select the Range");

while(i<1){

char key2 = keypad.getKey();
switch(key2)
{

case '0':
{
lcd.setCursor(0, 1);

Serial.println(key2);
serialread();
lcd.print("read value is");
inValue = atol(inByte);
lcd.println(inValue);
compare();
m=0;
inValue=0;
inByte[6]=0;

}

break;
case '1':
{
lcd.setCursor(0, 0);

Serial.println(key2);
serialread();
lcd.setCursor(0, 2);
lcd.print("read value is");
inValue = atol(inByte);
lcd.println(inValue);
compare();
//displayMenu();
//m=0;
//inValue=0;
//inByte[6]=0;
//displayMenu();

}
break;

}
}


delay(1000);
lcd.clear();

inValue=0;
uvalue=0;
lvalue=0;
k=0;
l=0;
i=0;
m=0;
inByte[6]=0;
displayMenu();

}
break;

case 'B':
{
lcd.clear();
k=0;
l=0;
i=0;
uvalue=0;
lvalue=0;
inByte[6] = 0;
m=0;
displayMenu();
}
break;

}
}
n++;
}

void serialread()
{
 while (m<5){
 while (Serial.available() > 0){
   inByte[m] = Serial.read();
  m++;
}
}
}

void compare()

{
  if(inValue > uvalue)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Read R value is ");
    lcd.print(inValue);
    lcd.setCursor(0, 2);
    lcd.print("R > Upperlimit");

  }
  else if(inValue < lvalue)
  {
     lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Read R value is ");
    lcd.print(inValue);
    lcd.setCursor(0, 2);
    lcd.print("R < Lowerlimit");

 
  } 

else
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Read R value is ");
    lcd.print(inValue);
    lcd.setCursor(0, 2);
    lcd.print("R within Range");

  }
}

Please check the code and help me..How do i repeat the process?

With the wild indentation and the gotos it's hard to tell what's going on.

One problem I see:

  char inByte[6] = {0};
  
[...]
        inByte[6]=0;

You only have 6 elements in your array, numbered 0-5. You're writing outside of the array.

But I believe the reason you never get back to the top of the loop is this while:

        while(i<1){

          char key2 = keypad.getKey();
          switch(key2)
          {

          case '0':
            {
[...]
            }

            break;
          case '1':
            {
[...]
            }
            break;
          }

Once you enter the loop you never set "i" again so you're stuck.

Also you don't need the extra brackets in your switch/case statement.

Hope this helps,

Brad
KF7FER

copy & cut code without a strong organization in mind is not going to work
specially with goto :wink:
redo your copy & cut but one related thing after one
or ask for a code that do what you want

@vector0

Thanks a lot :stuck_out_tongue: I would rather like to see something technical..

vathsa:
@vector0

Thanks a lot :stuck_out_tongue: I would rather like to see something technical..

You got the technical answer I reply #2, that is why it never completes the loop function.

grumpy he prefer you to make the program :wink:

@vector0

I have built this code block by block.. Yes I have taken some help from the forum..

I am not an expert like you..

For proof that i built this code block by block check the links below

http://forum.arduino.cc/index.php?topic=219538.msg1600101#msg1600101

http://forum.arduino.cc/index.php?topic=223027.msg1616897#msg1616897

Yes I don't mind if you give an efficient code than mine :stuck_out_tongue:

@ Brad Burleson

Thanks for the reply..

I made the changes to the while loop..Now it is working in the loop as expected..