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