CALCOLATRICE

 


#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'1','2','3','C'},
  {'4','5','6','D'},
  {'7','8','9','E'},
  {'A','0','B','F'}
};

byte rowPins[ROWS] = { 3, 2, 1, 0}; 
byte colPins[COLS] = { 7, 6, 5, 4}; 

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
static float lvalue;
static float rvalue;
static char oper;
static byte errcode;

void setup() 
{ lcd.begin(16,2);
  lcd.print("Calcolatrice 3.0");
  lcd.setCursor(0,1);
  lcd.print("PaulettGiankard");
  errcode = 0; }

float do_calc (float num1, float num2, char a_oper) 
    {if (errcode > 0)
        {return num1; }
      
     else switch (a_oper)
          { case 0:
              return num2;
              break;
            case 'C': 
              return (num1 + num2);
              break;
            case 'D': 
              return (num1 - num2);
              break;
            case 'E': 
              return (num1 * num2);
              break;
            case 'F':
              if (num2 != 0) 
                return (num1 / num2);   
              else 
                {errcode = 1;
                return 0;}
            break;}}

void print_result(float valore)
{ lcd.clear();
  if (errcode > 0)
      lcd.print("ERRORE");
  else
      lcd.print(valore); }

void loop() {
  lvalue = 0;
  rvalue = 0;
  oper = 'C';
  for (;;) 
  {
    char  key = keypad.getKey();
    if (key == 0) {
    } 
    else
      if ((key >= 'C') && (key <= 'F')) {
        lvalue = do_calc(lvalue, rvalue, oper);
        print_result(lvalue);
        rvalue = 0;
        oper = key;
      } 
      else
        if (key == 'B') {
          lvalue = do_calc(lvalue, rvalue, oper);
          print_result(lvalue);
          rvalue = 0;
          oper = 'C';
        }
        else
          if (key == 'A') { 
            lcd.clear();
            lvalue = 0;
            rvalue = 0;
            errcode = 0;
            oper = 'C';
            print_result(rvalue);
          }
          else {
            int number = key - 48;
            //qui andrebbe un controllo per evitare che l'utente
            //inserisca un numero eccessivo di cifre...
            rvalue = rvalue * 10 + number; 
            print_result(rvalue);
          }
  }
}
JSN Teki template designed by JoomlaShine.com