用的blinker做了个小车,为啥按下开关按钮,就永远往前冲...
感觉像直接一直进入while循环了,开关都失效了e#define BLINKER_WIFI
#define BLINKER_ESP_SMARTCONFIG
#include <Blinker.h>
char auth[] = "004d8f720f4e";
int n =digitalRead(D9);
// 新建组件对象
BlinkerButton Button1("btn-qian");
BlinkerButton Button2("btn-hou");
BlinkerButton Button3("btn-zuo");
BlinkerButton Button4("btn-you");
BlinkerButton Button5("btn-zuoqian");
BlinkerButton Button6("btn-zuohou");
BlinkerButton Button7("btn-youqian");
BlinkerButton Button8("btn-youhou");
BlinkerButton Button9("btn-zuozhuuan");
BlinkerButton Button10("btn-youzhuan");
BlinkerButton Button11("btn-xunhang");
BlinkerNumber Number1("num-abc");
int counter = 0;
#define ZSQ D1
#define ZSH D2
#define YSQ D3
#define YSH D4
#define ZXQ D5
#define ZXH D6
#define YXQ D7
#define YXH D8
#define TIME_TAP 100
void blinker_car_init()
{
pinMode(ZSQ, OUTPUT);
pinMode(ZSH, OUTPUT);
pinMode(YSQ, OUTPUT);
pinMode(YSH, OUTPUT);
pinMode(ZXQ, OUTPUT);
pinMode(ZXH, OUTPUT);
pinMode(YXQ, OUTPUT);
pinMode(YXH, OUTPUT);
pinMode(D9, INPUT);
digitalWrite(ZSQ, LOW);
digitalWrite(ZSH, LOW);
digitalWrite(YSQ, LOW);
digitalWrite(YSH, LOW);
digitalWrite(ZXQ, LOW);
digitalWrite(ZXH, LOW);
digitalWrite(YXQ, LOW);
digitalWrite(YXH, LOW);
}
// 按下按键即会执行该函数
void button11_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
while(state=="on"){
Button11.print("on");
while(n==LOW && state=="on"){
digitalWrite(ZSQ,HIGH);
digitalWrite(YSH,HIGH);
digitalWrite(ZXQ,HIGH);
digitalWrite(YXH,HIGH);
Blinker.delay(50);
digitalWrite(ZSQ,LOW);
digitalWrite(YSH,LOW);
digitalWrite(ZXQ,LOW);
digitalWrite(YXH,LOW);
}
digitalWrite(ZSQ,HIGH);
digitalWrite(YSQ,HIGH);
digitalWrite(ZXQ,HIGH);
digitalWrite(YXQ,HIGH);
Blinker.delay(50);
digitalWrite(ZSQ,LOW);
digitalWrite(YSH,LOW);
digitalWrite(ZXQ,LOW);
digitalWrite(YXH,LOW);
}
if(state=="off"){
Button11.print("off");
}
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
Number1.print(counter);
}
void setup() {
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化有LED的IO
blinker_car_init();
pinMode(LED_BUILTIN, OUTPUT);
// 初始化blinker
Blinker.begin(auth);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
Button6.attach(button6_callback);
Button7.attach(button7_callback);
Button8.attach(button8_callback);
Button9.attach(button9_callback);
Button10.attach(button10_callback);
Button11.attach(button11_callback);
}
void loop() {
Blinker.run();
}
如果是想实现按下动,松手不动,可以使用 press和pressup 我想实现按下动,松手也没事,再次按下就不动,所以用到开关 奈何col 发表于 2020-11-19 00:16
如果是想实现按下动,松手不动,可以使用 press和pressup
我需要按下一次就动,再按一次就不动了 1.不要在回调函数中写需要长时间运行的代码
2.您的while就是个死循环
3.在回调中存一个标志flag,然后loop做对应判断 按键的IO设置高电平动作,低电平停止就好 奈何col 发表于 2020-11-19 08:57
1.不要在回调函数中写需要长时间运行的代码
2.您的while就是个死循环
3.在回调中存一个标志flag,然后loop ...
谢谢指导了
页:
[1]