Do you want to know how to make an Electronic Calculator with Arduino mega ? Here it is.
// Electronic Calculator
//Written by Matt Kan
//Instagram : matt_kan1122
//Email : kmat111222333@gmail.com
//Reuse or redistribution is allowed, so long as credit has been given to the original maker
#include <LiquidCrystal.h>
const int RS = 12, EN = 11, D4 = 5, D5 = 4, D6 = 3, D7 = 2;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
int readButtonState[16]; //to initialize all the 16 input buttons from digits 0 to 9 to + - x / = CLR
long output[2]; // the total result = 1st output + 2nd output , for example, result = 111+41 = 152
int digit[7]; // to store the input digits
int addFlag=0; int subFlag=0; int multiFlag=0; int divFlag=0;
//These flags are for the recongition of which button(+ - x /) the user has entered
void setup() {
Serial.begin(9600); // You need to enter this code for applying the Serial Monitor,very convenient
for(int k=14;k<=30;k++)
{
pinMode(k,INPUT_PULLUP);
}
lcd.begin(16, 2); }
void loop() {
int i=0;
long total = 0;
for(int n=0;n<=16;n++)
readButtonState[n]=digitalRead(n+14); //THIS LINE OF CODE MUST BE PUT INSIDE loop()
while((readButtonState[10]==1)&&(readButtonState[11]==1)&&(readButtonState[12]==1)&&(readButtonState[13]==1)&&(readButtonState[14]==1))
//THIS IS FOR THE STORAGE OF DIGITS INTO AN ARRAY
{
for(int n=0;n<=16;n++)
readButtonState[n]=digitalRead(n+14);
//THIS LINE OF CODE MUST BE RUN CONTINUOUSLY
if(readButtonState[15]==0)
lcd.clear();
for(int a=0;a<=9;a++) //Element 0 to 9 : digit 0 to 9
{ //Element 10 to 13: + - x / ,ie digital pins 24 to 27
if(readButtonState[a]==0) //Element 14 to 15 : = CLR ,ie digital pins 28 to 29
{
lcd.print(a);
digit[i]=a;
++i;
delay(330);
}//End of if
}//End of For loop
}//End of While loop for the storage of digits into an array
if(readButtonState[10]==0)
{
lcd.print("+");
for(int m=0;m<i;m++)
{
output[0]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
addFlag=1;
}//End of + If Statement
if(readButtonState[11]==0)
{
lcd.print("-");
for(int m=0;m<i;m++)
{
output[0]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
subFlag=1;
}//End of - If Statement
if(readButtonState[12]==0)
{
lcd.print("x");
for(int m=0;m<i;m++)
{
output[0]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
multiFlag=1;
}//End of x If Statement
if(readButtonState[13]==0)
{
lcd.print("/");
for(int m=0;m<i;m++)
{
output[0]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
divFlag=1;
}//End of / If Statement
if(readButtonState[14]==0) //If Statement of =
{
for(int m=0;m<i;m++)
{
output[1]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
if(addFlag==1)
total=output[0] + output[1];
if(subFlag==1)
total=output[0] - output[1];
if(multiFlag==1)
total=output[0] * output[1];
if(divFlag==1)
total=output[0] / output[1];
lcd.setCursor(0,1);
lcd.print("=");
lcd.print(total);
addFlag=0; subFlag=0; multiFlag=0; divFlag=0;
total=0; output[0]=0; output[1]=0;
}}
long power(int base,int exponent)
{
long result=10;
for(int i=1;i<exponent;i++){
result=result*base;}
if(exponent==0)
result=1;
return result;
}
// Electronic Calculator
//Written by Matt Kan
//Instagram : matt_kan1122
//Email : kmat111222333@gmail.com
//Reuse or redistribution is allowed, so long as credit has been given to the original maker
#include <LiquidCrystal.h>
const int RS = 12, EN = 11, D4 = 5, D5 = 4, D6 = 3, D7 = 2;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);
int readButtonState[16]; //to initialize all the 16 input buttons from digits 0 to 9 to + - x / = CLR
long output[2]; // the total result = 1st output + 2nd output , for example, result = 111+41 = 152
int digit[7]; // to store the input digits
int addFlag=0; int subFlag=0; int multiFlag=0; int divFlag=0;
//These flags are for the recongition of which button(+ - x /) the user has entered
void setup() {
Serial.begin(9600); // You need to enter this code for applying the Serial Monitor,very convenient
for(int k=14;k<=30;k++)
{
pinMode(k,INPUT_PULLUP);
}
lcd.begin(16, 2); }
void loop() {
int i=0;
long total = 0;
for(int n=0;n<=16;n++)
readButtonState[n]=digitalRead(n+14); //THIS LINE OF CODE MUST BE PUT INSIDE loop()
while((readButtonState[10]==1)&&(readButtonState[11]==1)&&(readButtonState[12]==1)&&(readButtonState[13]==1)&&(readButtonState[14]==1))
//THIS IS FOR THE STORAGE OF DIGITS INTO AN ARRAY
{
for(int n=0;n<=16;n++)
readButtonState[n]=digitalRead(n+14);
//THIS LINE OF CODE MUST BE RUN CONTINUOUSLY
if(readButtonState[15]==0)
lcd.clear();
for(int a=0;a<=9;a++) //Element 0 to 9 : digit 0 to 9
{ //Element 10 to 13: + - x / ,ie digital pins 24 to 27
if(readButtonState[a]==0) //Element 14 to 15 : = CLR ,ie digital pins 28 to 29
{
lcd.print(a);
digit[i]=a;
++i;
delay(330);
}//End of if
}//End of For loop
}//End of While loop for the storage of digits into an array
if(readButtonState[10]==0)
{
lcd.print("+");
for(int m=0;m<i;m++)
{
output[0]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
addFlag=1;
}//End of + If Statement
if(readButtonState[11]==0)
{
lcd.print("-");
for(int m=0;m<i;m++)
{
output[0]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
subFlag=1;
}//End of - If Statement
if(readButtonState[12]==0)
{
lcd.print("x");
for(int m=0;m<i;m++)
{
output[0]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
multiFlag=1;
}//End of x If Statement
if(readButtonState[13]==0)
{
lcd.print("/");
for(int m=0;m<i;m++)
{
output[0]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
divFlag=1;
}//End of / If Statement
if(readButtonState[14]==0) //If Statement of =
{
for(int m=0;m<i;m++)
{
output[1]+=digit[m]*power(10,i-1-m); //output is tested ok
delay(300);
}
if(addFlag==1)
total=output[0] + output[1];
if(subFlag==1)
total=output[0] - output[1];
if(multiFlag==1)
total=output[0] * output[1];
if(divFlag==1)
total=output[0] / output[1];
lcd.setCursor(0,1);
lcd.print("=");
lcd.print(total);
addFlag=0; subFlag=0; multiFlag=0; divFlag=0;
total=0; output[0]=0; output[1]=0;
}}
long power(int base,int exponent)
{
long result=10;
for(int i=1;i<exponent;i++){
result=result*base;}
if(exponent==0)
result=1;
return result;
}
留言
張貼留言