这个怎么总是执行第一个Button1
#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 & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(val1,!digitalRead(switchPin1));
digitalWrite(val2,!digitalRead(switchPin2));
if(state == "on"){
digitalWrite(motorPin2,LOW);
do{
if(val1 == 0&&val2 == 0){
digitalWrite(motorPin1,HIGH);
text1.print("窗户正在打开!");
Serial.println("val1,val2");
}
else if(val1 == 0&&val2 == 1){
digitalWrite(motorPin1,HIGH);
text1.print("窗户正在打开!!");
Serial.println("val1,val2");
}
else if(val1 == 1&&val2 == 0){
digitalWrite(motorPin1,LOW);
text1.print("窗户已经完全打开");
Serial.println("val1,val2");
}
else{
digitalWrite(motorPin1,LOW);
text1.print("微动开关出现故障");
Serial.println("val1,val2");
}
Blinker.delay(500);
}
while(val1 == 0&&val2 == 0);
Serial.println("val1,val2");
}
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(val1,!digitalRead(switchPin1));
digitalWrite(val2,!digitalRead(switchPin2));
if(state == "on"){
digitalWrite(motorPin1,LOW);
do{
if(val1 == 0&&val2 == 0){
digitalWrite(motorPin2,HIGH);
text2.print("窗户正在关闭!");
Serial.println("val1,val2");
}
else if(val1 == 0&&val2 == 1){
digitalWrite(motorPin2,LOW);
text2.print("窗户已完全关闭");
Serial.println("val1,val2");
}
else if(val1 == 1&&val2 == 0){
digitalWrite(motorPin1,HIGH);
text2.print("窗户正在关闭!!");
Serial.println("val1,val2");
}
else{
digitalWrite(motorPin2,LOW);
text2.print("微动开关出现故障");
Serial.println("val1,val2");
}
Blinker.delay(500);
}
while(val1 == 0&&val2 == 0);
Serial.println("val1,val2");
}
}
void button3_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state == "on"){
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
}else{
}
}
// 如果未绑定的组件被触发,则会执行其中内容
//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);
// Blinker.attachData(dataRead);
Button1.attach(button1_callback);
}
void loop() {
Blinker.run();
}
你就只写了个 Button1.attach(button1_callback);,当然只会执行button1 奈何col 发表于 2019-4-26 17:08
你就只写了个 Button1.attach(button1_callback);,当然只会执行button1
谢谢....
页:
[1]