// C++ code
//
#include <Keypad.h>
		// number of the pin
int a= 7; //7
int b= 8; //6
int c= 9; //4
int d= 10; //2
int e= 11; //1
int f= 12; //9
int g= 13; //10


void number0(){
  digitalWrite(a, HIGH); //0
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}
void number1(){
  digitalWrite(a, LOW); //1
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void number2(){
  digitalWrite(a, HIGH); //2
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
void number3(){
  digitalWrite(a, HIGH); //3
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
void number4(){
  digitalWrite(a, LOW); //4
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void number5(){
  digitalWrite(a, HIGH); //5
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  /*
  delay(1000);
  */
}
void number6(){
  digitalWrite(a, HIGH); //6
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void number7(){
  digitalWrite(a, HIGH); //7
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void number8(){
  digitalWrite(a, HIGH); //8
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void number9(){
  digitalWrite(a, HIGH); //9
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
void blank(){
  digitalWrite(a, LOW); //blank
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

const byte Rows = 4; //four rows
const byte Cols = 4; //three columns

char keys[Rows][Cols] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'C','0','#','/'}
};

byte rowPins[Rows] = {6,5,4,3}; //connect to the row pinouts of the keypad
byte colPins[Cols] = {2,A2,A1,A0}; //connect to the column pinouts of the keypad

Keypad myKeypad = Keypad(makeKeymap(keys),rowPins,colPins,Rows,Cols);

boolean presentValue = false;
boolean next = false;
boolean final = false;
String num1, num2;
int answer;
char op;

void setup()
{
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);

  /*
  // The digits are working
  delay(500);
  number0();  delay(500);
  number1();  delay(500);
  number2();  delay(500);
  number3();  delay(500);
  number4();  delay(500);
  number5();  delay(500);
  number6();  delay(500);
  number7();  delay(500);
  number8();  delay(500);
  number9();  delay(500);
  blank();    delay(500);
  */


  Serial.begin(9600);
  while (!Serial);
  Serial.println("Enter two distinct numbers from (1 - 5)");  
}

void loop()
{
  char key = myKeypad.getKey();
  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){

    /*
    // The keypad is working
    Serial.print( "Key : ");
    Serial.println( key);
    */

    if (presentValue != true){
      num1 = num1 + key;
      int numLength = num1.length();
      //
    }
    else 
    {
      num2 = num2 + key;
      int numLength = num2.length();
      //
      final = true;
    }
  }
  else if (final == true && key != NO_KEY && key == '='){
    if (op == '+'){
      answer = num1.toInt() + num2.toInt();
    }
    else if (op == '-'){
      answer = num1.toInt() - num2.toInt();
    }
    else if (op == '*'){
      answer = num1.toInt() * num2.toInt();
    }
    else if (op == '/'){
      answer = num1.toInt() / num2.toInt();
    }
  }
  else if (key != NO_KEY && key == 'C'){
    //
    presentValue = false;
    final = false;
    num1 = "";
    num2 = "";
    answer = 0;
    op = ' ';
  }
}