2016-09-30 2 views
0

우리는 현재 Arduino와 LCD를 사용하여 4x3 계산기를 만들고 있습니다. 버튼이 없기 때문에 버튼 하나 하나에 모든 버튼을 사용할 수 있습니다. 지금까지는 추가 만합니다. 당신은 내가 한 번 작동 버튼을 누를 경우에있어서, 일을 어떻게, 당신이 이러한 목표를 달성하기 위해 배열을 사용할 수Arduino 계산기 (복수 버튼 누르기)

#include <Keypad.h> 
#include <LiquidCrystal.h> //import lcd library 


LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //lcd pins 

//LiquidCrystal lcd(5,4,3,2,1,0); 
const byte ROWS = 4; // four rows 
const byte COLS = 3; 

//define the keymap 
char keys [ROWS] [COLS] = { 
    {'1', '2', '3'}, 
    {'4', '5', '6'}, 
    {'7', '8', '9'}, 
    {'+', '0', '='} 
}; 

byte rowPins[ROWS] = { 
    9 ,8 ,7 ,6}; //connect keypad ROW1, ROW2, ROW3, ROW4 to these arduino pins 
byte colPins[COLS] = { 
    13, 10, 1}; 
//create the keypad 
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

//variables declaration 
boolean valOnePresent = false; 
boolean next = false; 
boolean final = false; 
String num1, num2; 
int ans; 
char op; 

void setup(){ 
    lcd.begin(16,2); 
    lcd.setCursor(2,0); 
    lcd.print("Calculator"); 
    delay(2500); 
    lcd.clear(); //clears the LCD screen and positions the cursor in the upper-left corner. 
} 

void loop(){ 
    char key = myKeypad.getKey(); 

    if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){ 
    if (valOnePresent != true){ 
     num1 = num1 + key; 
     int numLength = num1.length(); 
     lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator 
     lcd.print(num1); 
    } 
    else { 
     num2 = num2 + key; 
     int numLength = num2.length(); 
     lcd.setCursor(15 - numLength, 1); 
     lcd.print(num2); 
     final = true; 
    } 
    } 

    else if (valOnePresent == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')){ 
    if (valOnePresent == false){ 
     valOnePresent = true; 
     op = key; 
     lcd.setCursor(15,0); 
     lcd.print(op); 
    } 
    } 

    else if (final == true && key != NO_KEY && key == '='){ 
    if (op == '+') 
    { 
     ans = num1.toInt() + num2.toInt(); 
    } 
    else if (op == '='){ 
     ans = num1.toInt() + num2.toInt(); 
    } 
    /* else if (op == '+') 
    { 
     answ = num1.toInt() - num2.toInt(); 
    } 
    */ 



     lcd.clear(); 
     lcd.setCursor(15,0); 
     lcd.autoscroll(); 
     lcd.print(ans); 
     lcd.noAutoscroll(); 
    } 

} 

답변

0

등 또한, 만약 두 번, 뺄셈을한다. 작은 지연을 가진 while 루프를 구현하면 배열 시간이 초과 될 때까지 버튼을 누를 때마다 배열의 위치를 ​​반복 할 수 있습니다. 다음은이를 구현할 수있는 몇 가지 예입니다.

char ops [4] = {'+','-','/','*'}; 
int del = 2500; 
int strt = millis(); 
int location = 0; 
while (millis() - strt < del) { 
    key = myKeypad.getkey(); 
    if (key == '+') { 
    if (loc == 3) { 
     location = 0; 
    } 
    else { 
     location += 1; 
    } 
    strt = millis(); 
    } 
} 
op = ops(location);