[SOLVED]"Return;" not returning value.

Hi,

I am developing a system with a LCD and keypad, integrated with this I have IR sensors continuously looking for infrared interference. My problem is that I can not use code with any delay or while loop which might interfere with the IR sensors.

In the code below I have a keypad and LCD, a value is entered into an array and I am capable of converting the array into an int to be able to return the value for further processing. My problem is that my value is not returning, it is however displayed in the serial monitor as an int where specified. See code:

[LCD and Keypad setup not included] 

int SavedNew = 0;
int SavedOld = 0;
int SavedAcc = 0;
char inData[9];
int index;
int amount = 0;


char key = keypad.getKey();


boolean TxtConValue = false;
boolean ArrivalValue = true;
boolean started = false;
boolean ended = false;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.noAutoscroll();
  lcd.print("Arrival Amount:");
  lcd.setCursor(0,1);
  lcd.print("Enter->");
  Serial.println("Enter Arrival Amount");
 
  keypad.setDebounceTime(20);  
}



//Save Arrival Amount

int ArrivalAmount()
{ 
  
  
 if(ArrivalValue == true)
{ 
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
   lcd.print(key);
    if(started == false && key != '*')          //ensure first key pressed is *
    {
    TxtArrivalAmount();
    key = 0;
    }
    
    else 
    {
      if (key == '*')
         {
          started = true;
          index = 0 ;
          inData[index] = '\0';
           
         }
     else if(key == 'A')
         {
          TxtArrivalAmount();
          key = 0;
         } 
     else if(key == '#')
          {
            ended = true;
          }
     else if(started)
          {
            inData[index] = key;
            index++;
            inData[index] = '\0';
          }
       } 
  }   
  
 
    
  if(started && ended)
  {
    
   int amount = atoi(inData);             
    
   Serial.print(amount);                        // variable displays correctly 
   started = false;
   ended = false;
   index = 0;
   inData[index] = '\0';
   TxtConValue = true; 
   //Serial.print(amount); 
    
  }
   return amount;                                  [b]//this is the variable I want[/b]
}

}
//Save Accumulated Amount

  void AccumulatedAmount()
  {
      
      SavedNew = ArrivalAmount();
      SavedAcc = SavedNew + SavedOld ;
      SavedOld = SavedNew ;
      
  }


//TextDisplay - Confrim Amount
      
  void  TxtConfirmAmount()
     {
       if(TxtConValue == true )
       {
         AccumulatedAmount();
         lcd.clear();
         lcd.print("Confirm Input");
         lcd.setCursor(0,1);
         lcd.print("Amount = ");
         lcd.print(SavedNew);
         Serial.println();
         Serial.println("Awaiting Confirmation");
         TxtConValue = false;
         ArrivalValue = false; 
         key = 0;  
       }
       else
       ConfirmValue();
    }
      
      
   void ConfirmValue()
  {  
    
    key = keypad.getKey();
    if(key == 'D')      
    { 
      Serial.print("Value Confirmed: ");
      Serial.println(SavedNew);
      Serial.print("Accumulated Value: ");
      Serial.println(SavedAcc);
      TxtArrivalAmount();
      key = 0;
      ArrivalValue = true; 
    } 
    else if(key == 'A')                          //Reset 
    {
    TxtArrivalAmount();
    ArrivalValue = true;
    ArrivalAmount();
    key = 0;
    }    
  } 

//Text Display - Arrival Amount 

  void  TxtArrivalAmount()  
     {
        lcd.clear();
        lcd.print("Arrival Amount:");
        lcd.setCursor(0,1);
        lcd.print("Enter->");
        lcd.setCursor(7,1);
        Serial.println("Enter Arrival Amount");  
      }
   
  
  void loop()
  { 
    
      ArrivalAmount();
      TxtConfirmAmount();
  }

I have spend the whole day trying different ways and manners, please help. I am truly a newbie at this.

Thank you in advance

You need to learn about the life of variables and what happens when you create them in a function as opposed to creating them as a global variable.

Instead of creating amount here

int amount = atoi(inData);

, make it before your setup() function and just have amount = atoi(inData);

You have more than one variable called amount. One global and one local to the ArrivalAmount() function. When the function returns amount which version of the variable will the calling routine see I wonder ?

As already said, the variable goes out of scope before the return ( it is masking the global version of the same name because it is the closer scope ). You already have a global, which is returned, so either remove the global version, or move the declaration above the if, like this snippet:

int amount;

if(started && ended){

amount = atoi(inData);

Serial.print(amount); // variable displays correctly
started = false;
ended = false;
index = 0;
inData[index] = '\0';
TxtConValue = true;
}
return amount; //this is the variable I want

Thank you very much, it works when I leave the variable "amount" as global. When I create it as a local variable, the variable equates to 256 as standard. I will try and figure out why... in the meanwhile it works. Thank you so much, truly.

Chris2:
I will try and figure out why...

A really good sign of your potential there.