Arduino RichShield 计算器-我需要这样做,所以当我按住按钮时没有效果

Arduino RichShield calculator- I need to make it so when I hold down the button there is no effect

所以我正在制作一个 Arduino RichShield 计算器,它的工作原理如下:

  1. 我选择第一个数字的值
  2. 我按下按钮,它进入第二个状态——选择一个运算符(+ - * /)
  3. 我再次按下按钮,我可以选择第二个数字的值
  4. 我再次按下按钮,结果出现了
  5. 当我再次按下它时,它会重置,我可以做另一个方程式

我需要解决的是,如果我按住按钮,它会不断切换状态。我需要这样做,以便只有正常的按钮按下才能改变状态。这是代码:

    #include "Display.h"
//declaring the button and LEDs
const int PIN_BUTTON = 9;
int buttonState = 0;
const int PIN_LEDR = 4;
const int PIN_LEDG = 5;
const int PIN_LEDB = 6;
const int PIN_LEDY = 7;

//declaring potentiometer variables
const int PIN_POTMETER = 14;
int MAX_NUM = 20;
int MIN_NUM = -20;
int potValue = 0;
int potValueForCalculations = 0;

//setting up inputs/outputs for button, LEDs, potentiometer
void setup() {
  pinMode(PIN_LEDR, OUTPUT);
  pinMode(PIN_LEDG, OUTPUT);
  pinMode(PIN_LEDB, OUTPUT);
  pinMode(PIN_LEDY, OUTPUT);
  pinMode(PIN_POTMETER, INPUT);
  pinMode(PIN_BUTTON, INPUT_PULLUP);
  Display.show("----");
  delay(3000);
}

//declaring variables for mathematical operations/button counter
int buttonCounter = 0;
float inputValue1;
float inputValue2;
float result;
char mathOperator;

void loop() {
  buttonState = digitalRead(PIN_BUTTON);
  //state 1- choosing the first value
  if (buttonCounter == 0) {
    
    //mapping and showing potValueForCalculations on the display
    potValue = analogRead(PIN_POTMETER);
    potValueForCalculations = map(potValue, 0, 1023, MIN_NUM, MAX_NUM);
    Display.show(potValueForCalculations);
    delay(100);

    //if we press the button we go into state 2
    if (buttonState == LOW) {
      inputValue1 = potValueForCalculations;
      buttonCounter = 1;
      delay(1000);
    }
  }
  //state 2- choosing the sign for the mathematical operation
  else if (buttonCounter == 1) {
    potValue = analogRead(PIN_POTMETER);
    if (potValue <= 256) {
      Display.show("a");
      mathOperator = 'a';
    }
    else if (potValue > 256 && potValue <= 512) {
      Display.show("s");
      mathOperator = 's';
    }
    else if (potValue > 512 && potValue <= 768) {
      Display.show("t");
      mathOperator = 't';
    }
    else {
      Display.show("d");
      mathOperator = 'd';
    }
    //if we press button we go into state 3
    if (buttonState == LOW) {
      buttonCounter = 2;
      delay(500);
    }
  }
  //state 3- choosing the second value
  else if (buttonCounter == 2) {
    potValue = analogRead(PIN_POTMETER);
    potValueForCalculations = map(potValue, 0, 1023, MIN_NUM, MAX_NUM);
    Display.show(potValueForCalculations);
    delay(100);

    //if we press button we go into state 4
    if (buttonState == LOW) {
      inputValue2 = potValueForCalculations;
      buttonCounter = 3;
      delay(500);
    }
  }
  
  //state 4- showing the result
  else if (buttonCounter == 3) {

    digitalWrite(PIN_LEDG, HIGH);
    digitalWrite(PIN_LEDR, LOW);

    if (mathOperator == 'a') {
      result = inputValue1 + inputValue2;
      Display.show(result);
    }
    else if (mathOperator == 's') {
      result = inputValue1 - inputValue2;
      Display.show(result);
    }
    else if (mathOperator == 't') {
      result = inputValue1 * inputValue2;
      Display.show(result);
    }
    else if (mathOperator == 'd') {
      //if we try to divide by 0 the red LED turns on and the display shows "Err"
      if (inputValue2 == 0) {
        digitalWrite(PIN_LEDR, HIGH);
        digitalWrite(PIN_LEDG, LOW);
        Display.show("Err");
      }
      else {
        result = inputValue1 / inputValue2;
        Display.show(result);
      }
    }
    //if we press the button again the counter enters the reset threshold- the calculator restarts
    if (buttonState == LOW) {
        buttonCounter = 4;
        delay(500);
      }
  }
  else if (buttonCounter >= 4) {
    digitalWrite(PIN_LEDR, LOW);
    digitalWrite(PIN_LEDG, LOW);
    digitalWrite(PIN_LEDB, LOW);
    digitalWrite(PIN_LEDY, LOW);
    Display.show("");
    buttonCounter = 0;
    delay(500);
  }
}

您应该检查 buttonState 更改为 LOW,而不是检查 buttonState 是否为 LOW

它大概看起来像这样:

int prevButtonState = 0;
int newButtonState = 0;

void loop() {
  newButtonState = digitalRead(PIN_BUTTON);
  if( newButtonState == LOW && prevButtonState != LOW) {
  // ...
  }
  prevButtonState = newButtonState;
}

N.B。根据所涉及的硬件,您有时还需要考虑按钮在释放时会“弹跳”一下。您可以通过检查按钮在将其视为“按下”之前保持低电平一段时间来做到这一点。