我把一个基本程序加载到我的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
谢谢!
答案 0 :(得分:1)
如果你想在按钮从关闭转换为开启时打招呼,你需要记住变量中的先前状态。
然后,只有在按下当前状态并且未按下先前状态时才会打招呼。
这就像是:
int curr, prev = HIGH;
void loop () {
curr = digitalRead (2);
if ((prev == HIGH) && (curr == LOW)) {
Keyboard.print("Hello!");
}
prev = curr;
}