Problem with button code.

I have a problem with my code.
I want the lcd show “value?” when a1 is not inserted ( it’s mean a1=0) and ‘=’ is pressed, we have to insert a1 to continue.
But when i use the code, without insert any a1’s value and press ‘=’, it just jump into “Number of Product”.
Please help me, thanks a lot.
Here is my code:

Code: [Select]

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

LiquidCrystal_I2C lcd(0x3F,20,4);

long a1 = 0;
long b1 = 0;
double total = 0;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’,’+’},
{‘4’,‘5’,‘6’,’-’},
{‘7’,‘8’,‘9’,’$’},
{’*’,‘0’,’=’,’/’}
};
byte rowPins[ROWS] = {0,1,2,3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4,5,6,7}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(“Welcome!”);
delay(1500);
lcd.clear();

}

void loop()
{
lcd.setCursor(0,0);
lcd.print("Press B to finish! ");
lcd.setCursor(0,1);
lcd.print(“Type of Product:”);
lcd.setCursor(0,3);
lcd.print(“1-18 only”);
customKey = customKeypad.getKey();

if( a1 == 0 && customKey == ‘=’ ) // The problem is here. LCD doesn’t show the “Value?” when a1 is not inserted and pressed “=”, please help.
{
lcd.setCursor(14,3);
lcd.print(“Value?”);
}

if(customKey == ‘-’) //reset a1 and b1 value
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print(“Done.”);
delay(1500);
a1=0,b1=0;
}

switch(customKey)
{
case ‘0’ … ‘9’: // This keeps collecting the first value until a operator is pressed
lcd.setCursor(17,1);
a1 = a1 * 10 + (customKey - ‘0’);
if(a1 >18) // insert a1, from 1-18, if a1>18 or a1<1, error.
{
lcd.setCursor(14,3);
lcd.print(“error!”);
a1=0;
}
else(a1 <= 18);
{
lcd.setCursor(17,1);
lcd.print(a1);
}
break;

case ‘=’:

lcd.clear();
lcd.setCursor(0,1);
lcd.print(“Number of Product:”);
lcd.setCursor(0,3);
lcd.print(“1-8 only”);
b1 = SecondNumber(); // get the collected the second number
lcd.clear();

case '’:
lcd.clear(); // "
" button is used to erase and set a1=0 again if we press wrong value
a1=0;
break;

}
}

long SecondNumber()
{
while( 1 )
{
customKey = customKeypad.getKey();
if(customKey >= ‘0’ && customKey <= ‘9’)
{
b1 = b1 * 10 + (customKey - ‘0’); // insert b1, from 1-18, if b1<1 or b1>8, error.
lcd.setCursor(18,1);

if(b1 > 8 )
{
lcd.setCursor(14,3);
lcd.print(“error!”);
b1=0;
}

else(b1 <= 8 );
{
lcd.setCursor(19,1);
lcd.print(b1);
}
}
if(customKey == ‘=’) break; //return second

}
return b1;

}[code][code][/code][/code]

See the strange smileys? Please edit your code and put it inside code tags as explained in the first few sticky posts in the forum.

Edited. Please help me :D

TLTN96: Edited. Please help me :D

Nothing changed. If you only edited the smiley, you may have wrecked the code.

aarg: Nothing changed. If you only edited the smiley, you may have wrecked the code.

The code is not wrecked. Thanks :D

TLTN96: The code is not wrecked. Thanks :D

Did you read the guide to forum posting as I suggested? There are good reasons for using the code tags or I wouldn't ask.

DUDE!!

Put all of your code in code tags.

like this

And use the format code tool in the IDE so that the indenting is standard and consistent.