键盘计算器(多功能按钮)

时间:2018-03-24 19:32:41

标签: arduino keypad

我正在尝试使用3x4键盘,LCD和Arduino制作计算器。我想制作一个多功能按钮,按顺序进行4个基本操作:

当我按下时:

customKey = keypad.getKey();
switch (customKey) {
  case '0'...'9': // This keeps collecting the first value until a operator 
    is pressed "+-*/"
    lcd.setCursor(0, 0);
    first = first * 10 + (customKey - '0');
    lcd.print(first);
    break;

  case '*':
    first = (total != 0 ? total : first);
    lcd.setCursor(8, 0);
    lcd.print("+");
    second = SecondNumber(); // get the collected the second number
    total = first + second;
    lcd.setCursor(0, 1);
    lcd.print(total);
    first = 0, second = 0; // reset values back to zero for next use
    break;

  case '**':

    first = (total != 0 ? total : first);
    lcd.setCursor(8, 0);
    lcd.print("-");
    second = SecondNumber();
    total = first - second;
    lcd.setCursor(0, 1);
    lcd.print(total);
    first = 0, second = 0;
    break;
  case '***':
    first = (total != 0 ? total : first);
    lcd.setCursor(0, 1);
    lcd.print("*");
    second = SecondNumber();
    total = first * second;
    lcd.setCursor(1, 0);
    lcd.print(total);
    first = 0, second = 0;
    break;
  case '****':
    first = (total != 0 ? total : first);
    lcd.setCursor(0, 1);
    lcd.print("/");
    second = SecondNumber();
    lcd.setCursor(1, 0);
    second == 0 ? lcd.print("Invalid") : total = (float) first / (float) second;
    lcd.print(total);
    first = 0, second = 0;
    break;
  case '#':
    keypad.setDebounceTime(100);
    total = 0;
    lcd.clear();
    break;

  }
}

long SecondNumber() {
  while (1) {
    customKey = keypad.getKey();
    if (customKey >= '0' && customKey <= '9') {
      second = second * 10 + (customKey - '0');
      lcd.setCursor(9, 0);
      lcd.print(second);
    }
    if (customKey == '#') break; //return second;
  }
  return second;
}

我做了一个开关盒,但它只适用于第一种情况。其他情况并非如此。以下是我遇到问题的代码部分:

y

1 个答案:

答案 0 :(得分:0)

这里的问题是每次按下操作员,而不是一组操作员进入开关盒。每次只有'*'进入开关案例,因为 getkey函数一次一个键盘值而不是'***'。

因此,对于上述情况,您必须使用4x4键盘,它可以使用其他不同的字符。

希望这有帮助。