如何每次只执行一个回调函数 button
#define BLINKER_WIFI#include <Blinker.h>
int motorPin1 = D3;//电机正转开窗
int motorPin2 = D4;//电机反转关窗
int switchPin1 = D5;//开窗到位的限位
int switchPin2 = D6;//关窗到位的限位
int val1 = 0;
int val2 = 0;
BlinkerText text1("text1");//文本提示1
BlinkerText text2("text2");//文本提示2
BlinkerText text3("text3");
BlinkerText text4("text4");
char auth[] = "xxxxxxxxx;
char ssid[] = "123";
char pswd[] = "00000000";
// 新建组件对象
BlinkerButton Button1("btn-n3f");//电机正转开窗
BlinkerButton Button2("btn-31j");//电机反转关窗
BlinkerButton Button3("btn-6lo");//电机停止
// 按下按键即会执行该函数
void button1_callback(const String & state1)
{
BLINKER_LOG("get button state: ", state1);
if(state1 == "tap"){
digitalWrite(motorPin2,LOW);
do{
val1 = !digitalRead(switchPin1);
val2 = !digitalRead(switchPin2);
if(val1 == 0&&val2 == 0){
digitalWrite(motorPin1,HIGH);
text1.print("窗户正在打开!");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
else if(val1 == 0&&val2 == 1){
digitalWrite(motorPin1,HIGH);
text1.print("窗户正在打开!!");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
else if(val1 == 1&&val2 == 0){
digitalWrite(motorPin1,LOW);
text1.print("窗户已经完全打开");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
else{
digitalWrite(motorPin1,LOW);
text1.print("微动开关出现故障");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
Blinker.delay(2000);
}
while(val1 == 0);
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
}
void button2_callback(const String & state2)
{
BLINKER_LOG("get button state: ", state2);
if(state2 == "tap"){
digitalWrite(motorPin1,LOW);
do{
val1 = !digitalRead(switchPin1);
val2 = !digitalRead(switchPin2);
if(val1 == 0&&val2 == 0){
digitalWrite(motorPin2,HIGH);
text2.print("窗户正在关闭!");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
else if(val1 == 0&&val2 == 1){
digitalWrite(motorPin2,LOW);
text2.print("窗户已完全关闭");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
else if(val1 == 1&&val2 == 0){
digitalWrite(motorPin1,HIGH);
text2.print("窗户正在关闭!!");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
else{
digitalWrite(motorPin2,LOW);
text2.print("微动开关出现故障");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
Blinker.delay(2000);
}
while(val2 == 0);
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
}
void button3_callback(const String & state3)
{
BLINKER_LOG("get button state: ", state3);
if(state3 == "tap"){
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
val1 = !digitalRead(switchPin1);
val2 = !digitalRead(switchPin2);
text3.print("电机已停止!!");
Blinker.delay(5000);
}
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}
}
// 如果未绑定的组件被触发,则会执行其中内容
//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
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
}
void loop() {
Blinker.run();
}
这个我按了按钮1 执行了button1 再按按钮2 执行button2 可是等到button2 执行条件达到停止执行后又开始执行button1 了 按了按钮3也是的 执行一遍button3然后又去执行button1或者button2了
怎么去控制让它每次只执行一个呢? 求大神指点啊 ........
在回调中调用Blinker.delay(2000);和while死循环,都会造成阻塞。
你程序逻辑问题,和blinker无关了,建议自己理清逻辑再编程。 好的 谢谢那是不是这个不适合用回调函数了啊
页:
[1]