非常感谢奈何col的指点,终于可以勉强控制窗户的开和关了
本帖最后由 落叶控制 于 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{
}
}
//已经可以勉强控制了,就是关窗到位后,会一直重复显示窗户已经完全关闭,这个还在想办法!!!
下一步就是把雨感器接进去,就万事大吉了:lol 硬件呢? tigerzq 发表于 2019-4-30 18:06
硬件呢?
板子用的是esp8266的基于arduino UNO的 再加几个微动开关
落叶控制 发表于 2019-5-4 14:23
板子用的是esp8266的基于arduino UNO的 再加几个微动开关
微动开关能开窗吗? mark。不知道你的电机怎么选型 同求开关窗户的机械设计和电机选型 tigerzq 发表于 2019-5-4 16:34
微动开关能开窗吗?
还有个370的电机,12v的96r/min的 开关电源 机械加工件(自己的3D打印机打印的) GT2同步带以及同步轮,雨感器等等
目前正在试验中,好了的话再上图啊 lovedrose 发表于 2019-5-5 10:19
mark。不知道你的电机怎么选型
370的直流电机,12v的96r/min的 cheng5319052 发表于 2019-5-5 23:31
同求开关窗户的机械设计和电机选型
还有个370的电机,12v的96r/min的 开关电源 机械加工件(自己的3D打印机打印的) GT2同步带以及同步轮,雨感器等等
目前正在试验中,好了的话再上图啊
页:
[1]
2