#include <Keypad.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
const int Ctrl = 13;
int currentState;
int answ;
char oper = ' ';
boolean firstNumState = true;
String firstNum = "";
String secondNum = "";
float answer = 0.0;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'C', '0', '=', '/'}
};
char Ctrlkeys[ROW_NUM][COLUMN_NUM] = {
{'(', ')', 's', '+'},
{'4', '5', 'x', '-'},
{'^', 'c', 't', '*'},
{'C', '0', '=', '/'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
Keypad Ctrlkeypad = Keypad( makeKeymap(Ctrlkeys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
#include <Wire.h> // Wire Bibliothek einbinden
#include <LiquidCrystal_I2C.h> // Vorher hinzugefügte LiquidCrystal_I2C Bibliothek einbinden
LiquidCrystal_I2C lcd(0x27, 16, 2); //Hier wird festgelegt um was für einen Display es sich handelt. In diesem Fall eines mit 16 Zeichen in 2 Zeilen und der HEX-Adresse 0x27. Für ein vierzeiliges I2C-LCD verwendet man den Code "LiquidCrystal_I2C lcd(0x27, 20, 4)"
void setup() {
Serial.begin(115200);
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop() {
char key;
currentState = digitalRead(Ctrl);
if (currentState == LOW) { //Wenn Ctrl nicht gedrückt -> primäres Keypad
key = keypad.getKey();
}
else { //Wenn Ctrl gedrückt -> sekundäres Keypad
key = Ctrlkeypad.getKey();
}
if (key) {
Serial.println(key);
}
char newKey = key;
// Was tun, wenn er eine Nummer bekommt?
if ((newKey != NO_KEY) && (newKey == '1' || newKey == '2' || newKey == '3' || newKey == '4' || newKey == '5' || newKey == '6' || newKey == '7' || newKey == '8' || newKey == '9' || newKey == '0')) {
if (firstNumState == true) {
firstNum = firstNum + newKey;
lcd.print (newKey);
}
else {
secondNum = secondNum + newKey;
lcd.print (newKey);
}
}
// Was tun, wenn er einen Operator bekommt?
if (newKey != NO_KEY && (newKey == '+' || newKey == '-' || newKey == '*' || newKey == '/' || newKey == '^' || newKey == 'x' || newKey == 's' || newKey == 'c' || newKey == 't')) {
// code to execute if the condition is true
}
if (firstNumState == true) {
oper = newKey;
firstNumState = false;
lcd.print (oper);
}
if (newKey != NO_KEY && newKey == '=') {
if (oper == '+') {
answ = firstNum.toFloat () + secondNum.toFloat ();
}
if (oper == '-') {
answ = firstNum.toFloat () - secondNum.toFloat ();
}
if (oper == '*') {
answ = firstNum.toFloat () * secondNum.toFloat ();
}
if (oper == '/') {
answ = firstNum.toFloat () / secondNum.toFloat ();
}
if (oper == '^') {
answ = pow(firstNum.toFloat (),secondNum.toFloat () );
}
if (oper == 'x') {
answ = sqrt(firstNum.toFloat() );
}
if (oper == 's') {
answ = sin(firstNum.toFloat() );
}
if (oper == 'c') {
answ = cos(firstNum.toFloat() ) ;
}
if (oper == 't') {
answ = tan(firstNum.toFloat() );
}
}
lcd.setCursor (0, 0);
lcd.print (firstNum);
lcd.setCursor (0,1);
lcd.print (oper);
lcd.setCursor (0,2);
lcd.print (secondNum);
lcd.setCursor (0,3);
lcd.print ("=");
lcd.setCursor (1,1);
lcd.print (answ);
firstNumState = true;
delay(100);
}