为什么按下按钮后我无法调用此方法? [英] Why am I not able to call this method after I push a button?
本文介绍了为什么按下按钮后我无法调用此方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用按钮作为拨动开关.按下它,它会显示东西A".再按一次,它会显示东西B".为什么我不能调用方法checkButtons_slow()
?
I'm using a pushbutton as a toggle switch. Press it and it does "stuff A." Press it again and it does "stuff B." Why am I not able to call my method 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();
我缺少什么?
So the commented out //test: ZServo.write(110);
works. What am I missing with the checkButtons_slow();
?
推荐答案
如果将void loop()
更改为此,则可以启用和禁用该方法.
If you change void loop()
to this then it will work to toggle the method on and off.
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);
}
}
这篇关于为什么按下按钮后我无法调用此方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文