Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 20, 4);
- const byte ROWS = 4;
- const byte COLS = 4;
- char keys [ROWS] [COLS] = {
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', '+'},
- {'C', '0', '=', '-'}
- };
- byte rowPins[ROWS] = {2, 3, 4, 5}; //{9, 8, 7, 6};
- byte colPins[COLS] = {6, 7, 8, 9}; //{5, 4, 3, 2};
- Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- boolean presentValue = false;
- boolean next = false;
- boolean final = false;
- String num1, num2;
- int answer;
- int AValue = 0;
- int BValue = 0;
- char op;
- void setup()
- {
- Serial.begin(9600);
- lcd.init(); // initialize the lcd
- lcd.init();
- // Print a message to the LCD.
- lcd.backlight();
- /*lcd.setCursor(3, 0);
- lcd.print("Hello, world!");
- lcd.setCursor(2, 1);
- lcd.print("Ywrobot Arduino!");
- lcd.setCursor(0, 2);
- lcd.print("Arduino LCM IIC 2004");
- lcd.setCursor(2, 3);
- lcd.print("Power By Ec-yuan!");*/
- delay(3000);
- lcd.clear();
- lcd.setCursor(7, 1);
- lcd.print("A = ");
- lcd.print(AValue);
- lcd.setCursor(7, 3);
- lcd.print("B = ");
- lcd.print(BValue);
- }
- void loop() {
- char key = myKeypad.getKey();
- //(key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')
- //(key == 'A' || key == 'B')
- if (key != NO_KEY && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0'))
- {
- if (presentValue != true)
- {
- num1 = num1 + key;
- int numLength = num1.length();
- //lcd.clear();
- lcd.setCursor(13 - numLength, 0); //to adjust one whitespace for operator
- lcd.print(num1);
- Serial.print(num1);
- }
- /*else
- {
- num2 = num2 + key;
- int numLength = num2.length();
- //lcd.setCursor(15 - numLength, 1);
- //lcd.print(num2);
- Serial.print(num2);
- final = true;
- }*/
- }
- else if (presentValue == false && key != NO_KEY && (key == '-' || key == '+'))
- {
- if (presentValue == false)
- {
- presentValue = true;
- op = key;
- lcd.setCursor(13, 0);
- lcd.print(op);
- Serial.print(op);
- final = true;
- }
- }
- else if (final == true && key != NO_KEY && (key == 'A' || key == 'B')) {
- if (op == '+') {
- answer = num1.toInt();
- if (key == 'A') {
- AValue = AValue + answer;
- }
- else {
- BValue = BValue + answer;
- }
- }
- else if (op == '-') {
- answer = num1.toInt();
- if (key == 'A') {
- AValue = AValue - answer;
- }
- else {
- BValue = BValue - answer;
- }
- }
- //lcd.clear();
- //lcd.setCursor(15,0);
- //lcd.autoscroll();
- //lcd.print(answer);
- lcd.clear();
- lcd.setCursor(7, 1);
- lcd.print("A = ");
- lcd.print(AValue);
- lcd.setCursor(7, 3);
- lcd.print("B = ");
- lcd.print(BValue);
- Serial.print(" = ");
- Serial.println(answer);
- Serial.print("A = ");
- Serial.println(AValue);
- Serial.print("B = ");
- Serial.println(BValue);
- Serial.println("--------------");
- presentValue = false;
- final = false;
- num1 = "";
- num2 = "";
- answer = 0;
- op = ' ';
- //lcd.noAutoscroll();
- }
- else if (key != NO_KEY && key == 'C') {
- //lcd.clear();
- /*lcd.clear();
- lcd.setCursor(7, 1);
- lcd.print("A = ");
- lcd.print(AValue);
- lcd.setCursor(7, 3);
- lcd.print("B = ");
- lcd.print(BValue);*/
- Serial.println("--------------");
- presentValue = false;
- final = false;
- num1 = "";
- num2 = "";
- //AValue = 0;
- //BValue = 0;
- answer = 0;
- op = ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement