【问题标题】:Keypad calculator (multi-function button)键盘计算器(多功能按钮)
【发布时间】:2018-03-24 19:32:40
【问题描述】:

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

当我按下时:

* this will be (+)
** this will be (-)
*** this will be(*)
**** this will be (/)

我做了一个开关盒,但它只适用于第一个案例。在其他情况下则不然。这是我遇到问题的代码部分:

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;
}

【问题讨论】:

    标签: arduino keypad


    【解决方案1】:

    这里的问题是每次按下操作符时,并不是一组操作符进入开关盒。每次只有 '*' 进入开关盒,因为 getkey function 接收键盘值 一次一个 而不是 '***'。

    因此,对于上述情况,您将不得不使用 4x4 键盘,该键盘将具有您可以使用的其他不同字符。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 2020-01-30
      • 2020-04-11
      相关资源
      最近更新 更多