/**
Arduino Calculator
Copyright (C) 2020, Uri Shaked.
Released under the MIT License.
*/// include the library code:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Keypad.h>
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
boolean flag_state = true;
int domestic_stamp = 1000;
int zone1_stamp = 1000;
int zone2_stamp = 1000;
float domestic_price = 0.50;
float zone1_price = 1.50;
float zone2_price = 2.00;
bool main_disp = false;
bool sel_domestic = false;
bool sel_inter = false;
int qty = 0;
String qty_str = "";
String is_selected = "";
bool disp_internationl = false;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
}
void loop() {
if (main_disp == false)
{
char key = keypad.getKey();
MainDisplay();
switch (key)
{
case 'A':
lcd.clear();
sel_domestic = true;
main_disp = true;
break;
case 'B':
lcd.clear();
sel_inter = true;
main_disp = true;
break;
case '#':
main_disp = true;
remainingStamp();
main_disp = false;
break;
default:
break;
}
}
while (sel_domestic == true)
{
char key = keypad.getKey();
SelectDomestic();
switch (key )
{
case '*':
lcd.clear();
qty_str = "";
qty = 0;
main_disp = false;
sel_domestic = false;
break;
case '#':
sel_domestic = false;
lcd.clear();
domestic_stamp = domestic_stamp - qty;
WholeAmount(domestic_price);
delay(5000);
qty_str = "";
qty = 0;
lcd.clear();
main_disp = false;
break;
default:
if ((key != 'A') && (key != 'B') && (key != 'C') && (key != 'D') && (qty <= domestic_stamp))
{
qty_str += String(key);
qty = qty_str.toDouble();
Serial.println(qty);
lcd.setCursor(5, 1);
lcd.print(qty_str);
}
else
{
lcd.clear();
qty_str = "";
qty = 0;
lcd.setCursor(5, 1);
lcd.print(qty);
}
break;
}
}
while (sel_inter == true)
{
char key = keypad.getKey();
if (disp_internationl == false)
{
SelectInternational();
}
switch (key )
{
case '*':
lcd.clear();
qty_str = "";
qty = 0;
is_selected = "";
disp_internationl = false;
main_disp = false;
sel_inter = false;
break;
case '#':
if (is_selected != "")
{
if (qty > 0)
{
lcd.clear();
if ( is_selected == "A.Zone 1 Intl")
{
zone1_stamp = zone1_stamp - qty;
WholeAmount(zone1_price);
}
else
{
zone2_stamp = zone2_stamp - qty;
WholeAmount(zone2_price);
}
delay(5000);
main_disp = false;
sel_inter = false;
qty_str = "";
qty = 0;
disp_internationl = false;
is_selected = "";
}
else
{
disp_internationl = true;
lcd.clear();
Sel_International(is_selected);
}
}
// else
// {
// disp_internationl = false;
// qty_str = "";
// qty = 0;
// }
break;
case 'A':
if (disp_internationl == false)
{
is_selected = "A.Zone 1 Intl";
lcd.setCursor(14, 0);
lcd.print("*" );
lcd.setCursor(14, 1);
lcd.print(" " );
}
break;
case 'B':
if (disp_internationl == false)
{
is_selected = "B.Zone 2 Intl";
lcd.setCursor(14, 0);
lcd.print(" " );
lcd.setCursor(14, 1);
lcd.print("*" );
}
break;
default:
if (disp_internationl == true )
{
if ( is_selected == "A.Zone 1 Intl")
{
if ((key != 'A') && (key != 'B') && (key != 'C') && (key != 'D') && (qty < zone1_stamp))
{
qty_str += String(key);
qty = qty_str.toInt();
Serial.println(qty);
lcd.setCursor(5, 1);
lcd.print(qty_str);
}
else
{
lcd.clear();
Sel_International(is_selected);
qty_str = "";
qty = 0;
lcd.setCursor(5, 1);
lcd.print(qty);
}
}
else
{
if ((key != 'A') && (key != 'B') && (key != 'C') && (key != 'D') && (qty < zone2_stamp))
{
qty_str += String(key);
qty = qty_str.toInt();
Serial.println(qty);
lcd.setCursor(5, 1);
lcd.print(qty_str);
}
else
{
lcd.clear();
Sel_International(is_selected);
qty_str = "";
qty = 0;
lcd.setCursor(5, 1);
lcd.print(qty);
}
}
}
}
}
}
void MainDisplay()
{
lcd.setCursor(0, 0);
lcd.print("A. Domestic");
lcd.setCursor(0, 1);
lcd.print("B. International");
}
void SelectDomestic()
{
lcd.setCursor(0, 0);
lcd.print("Domestic:$" + String(domestic_price));
lcd.setCursor(0, 1);
lcd.print("QTY.:");
}
void Sel_International(String msg)
{
lcd.setCursor(0, 0);
lcd.print(msg);
lcd.setCursor(0, 1);
lcd.print("QTY.:");
}
void SelectInternational()
{
lcd.setCursor(0, 0);
lcd.print("A.Zone 1 Intl" );
lcd.setCursor(0, 1);
lcd.print("B.Zone 2 Intl" );
}
void WholeAmount(float price)
{
lcd.setCursor(0, 0);
lcd.print(" Whole Amount ");
lcd.setCursor(0, 1);
lcd.print("Amount: $" + String(qty * price));
qty_str = "";
qty = 0;
}
void remainingStamp()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Remaining Stamp ");
lcd.setCursor(0, 1);
lcd.print("Domestic:" + String(domestic_stamp));
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Z1 Intl.:" + String(zone1_stamp));
lcd.setCursor(0, 1);
lcd.print("Z2 Intl.:" + String(zone2_stamp));
delay(2000);
lcd.clear();
}