本帖最后由 桥边等 于 2019-6-8 21:50 编辑
代码如下 为什么 如果我把中断 //attachInterrupt(D1,buttonTick, RISING);前面//去掉,整个程序不能运行![mw_shl_code=arduino,true]#define BLINKER_WIFI
#define BLINKER_ALIGENIE_LIGHT
#include <Blinker.h>
char auth[] = "fb843f67ea98";
char ssid[] = "ChinaUnicom";
char pswd[] = "3ezb-mknc-49w5";
boolean pushButton,outButton;
// 新建组件对象
BlinkerButton Button1("btn-abc");
BlinkerButton Button2("btn-abd");
// 按下按键即会执行该函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state=="on") {
digitalWrite(D2, LOW);
// 反馈开关状态
Button1.print("on");
} else if(state=="off"){
digitalWrite(D2, HIGH);
// 反馈开关状态
Button1.print("off");
}
}
void button2_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state=="on") {
digitalWrite(D2, LOW);
// 反馈开关状态
Button2.print("on");
} else if(state=="off"){
digitalWrite(D2, HIGH);
// 反馈开关状态
Button2.print("off");
}
}
void aligeniePowerSate(const String & state) //台灯开关处理
{
Serial.print(state);
if (state == BLINKER_CMD_ON) {
button1_callback("on");
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
}
else if (state == BLINKER_CMD_OFF) {
button1_callback("off");
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
}
}
void setup() {
// 初始化串口
Serial.begin(115200);
// 初始化有LED的IO
pinMode(D2, OUTPUT);
digitalWrite(D2, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
BlinkerAliGenie.attachPowerState(aligeniePowerSate);
//attachInterrupt(D1,buttonTick, RISING);
}
void loop() {
Blinker.run();
}
void buttonTick() {
pushButton = digitalRead(D1); //读取引脚2电平状态并将其赋值给布尔变量
outButton = digitalRead(D2);
if (pushButton){
delay (40);
while(pushButton){
pushButton = digitalRead(D1);
delay (40);
}
outButton=!outButton;
if (outButton){
button1_callback("on");
Button1.print("on");
}
else if (!outButton) {
button1_callback("off");
Button1.print("off");
}
}
}[/mw_shl_code]
|