Ви не увійшли.
завести счётчик нажатий.
Нужно при введении num1 и num2 ограничивать количество символов до 6 но как это сделать
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(14,15,16,17,18,19);
long num1,num2 ;
double total;
char operation,button;
int n=1;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'C','0','=','/'}
};
byte rowPins[ROWS] = {9,8,7,6};
byte colPins[COLS] = {5,4,3,2};
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
}
void loop()
{
while(1)
{
button = customKeypad.getKey();
if (button=='C')
{
num1=0;
num2=0;
total=0;
operation=0;
lcd.clear();
}
if (button >='0' && button <='9')
{
num1 = num1*10 + (button -'0');
lcd.setCursor(0,0);
lcd.print(num1);
}
if (num1 !=0 && (button=='-' || button=='+' || button=='*' || button=='/'))
{
operation = button;
lcd.setCursor(7,0);
while ((num1/=10) > 0) n++;
Serial.println(n);
if(n >7)
{
num1=0;
num2=0;
total=0;
operation=0;
lcd.clear();
lcd.print("Error");
break;
}
n=0;
lcd.print(operation);
break;
}
}
while(1)
{
if (button =='C')
break;
button = customKeypad.getKey();
if (button=='C')
{
num1=0;
num2=0;
total=0;
operation=0;
lcd.clear();
break;
}
if (button >='0' && button <='9')
{
num2 = num2*10 + (button -'0');
int n=1;
while ((num1/=10) > 0) n++;
Serial.println(n);
if(n >6)
{
num1=0;
num2=0;
total=0;
operation=0;
lcd.clear();
lcd.print("Error");
break;
}
lcd.setCursor(8,0);
lcd.print(num2);
}
if (button == '=' && num2 !=0)
{
domath();
break;
}
}
while(1)
{
if (button =='C')
break;
button = customKeypad.getKey();
if (button =='C')
{
lcd.clear();
lcd.setCursor(0,0);
num1=0;
num2=0;
total=0;
operation=0;
break;
}
}
}
void domath()
{
switch(operation)
{
case '+':
total = num1+num2;
break;
case '-':
total = num1-num2;
break;
case '/':
if(num2 == 0)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("ERROR");
}
total = (float)num1/(float)num2;
break;
case '*':
total = num1*num2;
break;
}
lcd.setCursor(0,1);
lcd.print('=');
lcd.setCursor(1,1);
lcd.print(total);
}