本帖最后由 落叶控制 于 2019-4-29 11:12 编辑
#define BLINKER_WIFI
#include <Blinker.h>
int motorPin1 = D3;//电机正转开窗
int motorPin2 = D4;//电机反转关窗
int switchPin1 = D5;//开窗到位的限位
int switchPin2 = D6;//关窗到位的限位
int val1 = 0;
int val2 = 0;
int val3 = 0;
int i;
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);
val1 = !digitalRead(switchPin1);
val2 = !digitalRead(switchPin2);
val3 = 1;
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));
}
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);
val1 = !digitalRead(switchPin1);
val2 = !digitalRead(switchPin2);
val3 = 2;
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));
}
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);
val3 = 3;
text3.print("电机已停止!!");
}
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();
val1 = !digitalRead(switchPin1);
val2 = !digitalRead(switchPin2);
if(val1 == 1&&val3 == 1 ){
digitalWrite(motorPin1,LOW);
text4.print("窗户已经完全打开");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}else if(val2 == 1&&val3 == 2){
digitalWrite(motorPin2,LOW);
text4.print("窗户已经完全关闭");
Serial.print("val1=");
Serial.println(!digitalRead(switchPin1));
Serial.print("val2=");
Serial.println(!digitalRead(switchPin2));
}else{
}
}
//已经可以勉强控制了,就是关窗到位后,会一直重复显示窗户已经完全关闭,这个还在想办法!!!
|