为什么我按下按钮后无法调用此方法?

时间:2017-07-03 17:37:26

标签: arduino

我正在使用按钮作为拨动开关。按它,它会“填充A.”再次按它,​​它会“填充B”。为什么我无法调用我的方法checkButtons_slow()

int prev = 0;
int current = 0;
int val4 = 0;
int val5 = 0;
int ledPin = 13;
int prev = 0;
int current = 0;

Servo ZServo;

void setup() {
  ZServo.attach(9);
  pinMode(pushD3, INPUT_PULLUP);
  digitalWrite(3, HIGH);
  pinMode(pushD4, INPUT_PULLUP);
  digitalWrite(4, HIGH);
  pinMode(pushD5, INPUT_PULLUP);
  digitalWrite(5, HIGH);
  pinMode(pushD6, INPUT_PULLUP);
  digitalWrite(6, HIGH);  
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if(digitalRead(3) == LOW) {
    current = 1 - current;
  }
  if(current == 1 && prev == 0) {
    checkButtons_slow();
    //test: ZServo.write(110);
    delay(500); //half a second
  }
  if(current == 0 && prev == 1) {
    ZServo.write(80);
    delay(500); //half a second
  }
  prev = current;
}

这是我的方法:

void checkButtons_slow() {
  val4 = digitalRead(pushD4);
  val5 = digitalRead(pushD5);
  if (val4 == LOW) {
    ZServo.write(88);
  } else if (val5 == LOW) {
    ZServo.write(99);
  } else {
    ZServo.write(91); //GUESSED ON 92; SHOULD TECHNICALLY BE 90
  }
}

因此注释掉的//test: ZServo.write(110);可行。我对checkButtons_slow();错过了什么?

1 个答案:

答案 0 :(得分:0)

如果您将void loop()更改为此,则可以打开和关闭此方法。

void loop() {
  if (digitalRead(3) == LOW) {
    num_presses++;
    delay(500);
  }
  if ((num_presses % 2) == 0) {
    //even
    checkButtons_slow();
  }
  else if(num_presses == 0) {
    ZServo.write(90);
  }
  else {
    ZServo.write(85);
  }
}