Else if problem
im having trouble craeting simple calculator can perform 4 basic operations. mistake must with if-else statements. have 4 conditions addition subtraction multiplication , division, associated 4 letters (a, b, c , d) on keypad in order. if pressed 2 numbers inputed user added if b subtracted , on. (addition) used if , others used else if. , same test (==) all. now, proble, no matter key pressed addition runs. dont know problem might be. thinkin maybe same test cannot ran statements, might right there not meny other tests use [i if (input == 'a') {call addition} , else if (input != a) && (input != a) && (input != a){call subtraction}- == b ==c , == d] still dont understand why cant run same test many times want?
i've been trying figure out past 2 days. appreciated.
thank in advance,
i've been trying figure out past 2 days. appreciated.
thank in advance,
code: [select]
#include <keypad.h>
#include <liquidcrystal.h>
liquidcrystal lcd (7,8,9,10,11,12);
int welcometext(){
lcd.begin(16,2);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("hello");
delay(2000);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("i calcuator");
delay(1750);
lcd.clear();
delay(1000);
lcd.begin(16,2);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("press...");
delay(750);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("'a' add");
delay(500);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("'b' subtract");
delay(500);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("'c' multiply");
delay(500);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("'d' divide");
delay(500);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("then...");
delay(500);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("enter 2 numbers");
delay(600);
};
int addition(int x, int y){
lcd.begin(16,2);
lcd.clear();
lcd.setcursor(0,0);
lcd.print(x, dec);
lcd.setcursor(2,0);
lcd.write("+");
lcd.setcursor(4,0);
lcd.print(y, dec);
lcd.setcursor(6,0);
lcd.write("=");
int z;
z = x+y;
lcd.setcursor(8,0);
lcd.print(z,dec);
}
int subtraction (int x, int y){
lcd.begin(16,2);
lcd.clear();
lcd.setcursor(0,0);
lcd.print(x, dec);
lcd.setcursor(2,0);
lcd.write("-");
lcd.setcursor(4,0);
lcd.print(y, dec);
lcd.setcursor(6,0);
lcd.write("=");
int z;
z = x-y;
lcd.setcursor(8,0);
lcd.print(z,dec);
}
int multiplication (int x, int y){
lcd.begin(16,2);
lcd.clear();
lcd.setcursor(0,0);
lcd.print(x, dec);
lcd.setcursor(2,0);
lcd.write("*");
lcd.setcursor(4,0);
lcd.print(y, dec);
lcd.setcursor(6,0);
lcd.write("=");
int z;
z = x*y;
lcd.setcursor(8,0);
lcd.print(z,dec);
}
int division (int x, int y){
lcd.begin(16,2);
lcd.clear();
lcd.setcursor(0,0);
lcd.print(x, dec);
lcd.setcursor(2,0);
lcd.write("/");
lcd.setcursor(4,0);
lcd.print(y, dec);
lcd.setcursor(6,0);
lcd.write("=");
int z;
z = x/y;
lcd.setcursor(8,0);
lcd.print(z,dec);
}
void setup() {
welcometext();
}
const byte rows =4;
const byte cols =4;
char keys[rows][cols]={
{1,2,3,'a'},
{4,5,6,'b'},
{7,8,9,'c'},
{'*',0,'#','d'}
};
byte rowpins[rows] = {0,1,2,3};
byte colpins[cols] = {4,5,6,13};
keypad keypad = keypad(makekeymap(keys), rowpins, colpins, rows, cols);
void loop(){
int a;
int b;
int operator;
operator = keypad.getkey();
while (operator != no_key) {
if (operator == 'a'){
a = keypad.getkey();
if (a != no_key){
{
b = keypad.getkey();
}while (b == no_key);
addition(a,b);
delay(100);
}
}
else if (operator == 'b'){
a = keypad.getkey();
if (a != no_key){
{
b = keypad.getkey();
}while (b == no_key);
subtraction(a,b);
delay(100);
}
}
else if (operator == 'c'){
a = keypad.getkey();
if (a != no_key){
{
b = keypad.getkey();
}while (b == no_key);
multiplication(a,b);
delay(100);
}
}
else if (operator == 'd'){
a = keypad.getkey();
if (a != no_key){
{
b = keypad.getkey();
}while (b == no_key);
division(a,b);
delay(100);
}
}
else{
lcd.begin(16,2);
lcd.clear();
lcd.setcursor(0,0);
lcd.write("invalid operator");
}
}
}
post code here (within code tags), pointing outside discourage many willing help...
check code second if, missing 'b' in code...
check code second if, missing 'b' in code...
code: [select]
else if (operator == ){
Arduino Forum > Using Arduino > Programming Questions > Else if problem
arduino
Comments
Post a Comment