I am trying to build a calculator with an Arduino, an LCD display, and a keypad. Getting inputs from the keypad is not the issue. Before I can add, subtract, multiply, or divide any two numbers, I want to make sure the calculator registered the given number. I am trying to program it (so far) so that I type in a number and press enter (the @ sign I assigned to my keypad) and the LCD prints out that number. As you will see, I attempted to devise a logic mechanism for the Arduino to recognize the typed number.
I thought since as you type more numbers the value of the first number typed increases by 10 fold each time (if you start by pressing 4 then if you press another number such as 5 following it, that 4 will become 40, and 400 if another number is pressed, etc...) I could multiply that number (and every following number) by 10, 100, 100, etc...
But it doesn't work and I need help so that the number can be collected by the Arduino.
Here's my attempt at it:
#include <LiquidCrystal.h>
#include "Arduino.h"
#include "Keypad.h"
int count = 0;
int row = 0;
int column = 0;
//const int buzzer = 0;
int value1;
int value2 = 0;
int spot1 = 0;
int spot2 = 0;
int spot3 = 0;
int spot4 = 0;
int spot5 = 0;
int spot6 = 0;
int spot7 = 0;
int spot8 = 0;
LiquidCrystal lcd(1, 2, 3, 4, 5, 6 );
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'%','0','@'}
};
byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the
keypad
byte colPins[COLS] = {13, 12, 11}; //connect to the column pinouts of
the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS
);
void setup() {
//pinMode(buzzer, OUTPUT);
lcd.begin(16,2);
lcd.print("---Calculator---");
delay(2000);
lcd.clear();
lcd.print("Value1: ");
lcd.blink();
}
void loop() {
char key = keypad.getKey();
if(key != NO_KEY){
lcd.print(key);
if(key != '%' && key != '@'){
count++;
}
else if(count == 1){
spot1 = key;
}
else if(count == 2){
spot2 = key;
}
else if(count == 3){
spot3 = key;
}
else if(count == 4){
spot4 = key;
}
else if(count == 5){
spot5=key;
}
else if(count == 6){
spot6=key;
}
else if(count == 7){
spot7=key;
}
else if(count == 8){
spot8=key;
}
if(key=='@'){
if(count == 1){
value1 = spot1;
}
else if(count == 2){
value1 = (spot1*10)+(spot2);
}
else if(count == 3){
value1 = (spot1*100)+(spot2*10)+spot3;
}
if(count == 4){
value1 = (spot1*1000)+(spot2*100)+(spot3*10)+spot4;
}
if(count == 5){
value1 = (spot1*10000)+(spot2*1000)+(spot3*100)+(spot4*10)+spot5;
}
if(count == 6){
value1 = (spot1*100000)+(spot2*10000)+(spot3*1000)+(spot4*100)+(spot5*10)+spot6;
}
if(count == 7){
}
if(count == 8){
}
lcd.print(value1);
}
if(key == '%'){
count = 0;
lcd.clear();
lcd.print("Value1: ");
lcd.blink();
}
}
}
PS: If you know any better way to accomplish this goal, I would love to hear it.