如何检测Adafruit nrf52840上的按钮按下

问题描述

我正在使用Feather Express nrf52840,并试图利用预先提供的用户按钮。读取引脚说明:https://learn.adafruit.com/introducing-the-adafruit-nrf52840-feather/pinouts它说

提供了一个可在您的项目中使用的触觉开关,该触觉开关已连接到P1.02,并且可以在代码中以Arduino中的D7和CircuitPython中的SWITCH进行访问。

我从此处使用示例代码https://learn.adafruit.com/make-it-switch/debouncing来读取按钮状态的变化。

我遇到的问题是,当我在原始代码中使用D7而不是A1时,代码将无法编译。我得到的错误是:

sketch_nov04a:1:23: error: 'D7' was not declared in this scope; did you mean 'A7'?
    1 | const int buttonPin = D7;   // the number of the pushbutton pin
      |                       ^~
      |                       A7
exit status 1
'D7' was not declared in this scope; did you mean 'A7'?

我考虑使用A7认为错误消息可能是正确的,但是已经使用了读取引脚AREF(A7 / P0.31)的信息。

我想念什么?

const int buttonPin = D7;   // the number of the pushbutton pin
int buttonState;             // the current reading from the input pin
int lastButtonState = HIGH;  // the prevIoUs reading from the input pin

void setup() {
  // put your setup code here,to run once:
  Serial.begin(115200);
  delay(100);
  pinMode(buttonPin,INPUT_PULLUP);
}

void loop() {

  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState) {
    Serial.println("I ATE THE PIE!!");
  }

}

解决方法

尝试:

const int buttonPin = 7;

可能有一些宏定义了A0等,但是据我所知,D0等都没有。对于那些您只使用数字的人。