首页 文章

Arduino Leonardo的无限循环

提问于
浏览
0

我把一个基本程序加载到我的Arduino Leonardo:

void setup() {
  // make pin 2 an input and turn on the 
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  //if the button is pressed
  if(digitalRead(2)==LOW){
    //Send the message
    Keyboard.print("Hello!");
  }
}

此示例有效,但它会生成无限循环打印“Hello!” . 我该如何控制循环?

基本的例子是:http://arduino.cc/en/Reference/KeyboardPrint

谢谢 !

1 回答

  • 1

    如果您想在按钮从关闭转换为开启时打招呼,则需要记住变量中的先前状态 .

    然后,只有在按下当前状态并且未按下先前状态时才会打招呼 .

    这将是这样的:

    int curr, prev = HIGH;
    void loop () {
        curr = digitalRead (2);
        if ((prev == HIGH) && (curr == LOW)) {
            Keyboard.print("Hello!");
        }
        prev = curr;
    }
    

相关问题