/*
固件集成了小爱、天猫和小度.....亲测中断只有D1’D3'D7可以用...亲测中断只有D1’D3'D7可以用...亲测中断只有D1’D3'D7可以用
卷帘器开机后电机自动转动无法解决,原因是手机上滑动条的值不能发送过来(串口监视数值总是1),只有滑动条数值刷新才能接受新的收据。
光栅码盘接到用来计数d1,按键开关接到d3,电机接到d4,d5。 看看哪位大侠能帮我到问题解决下!!!!!!!!!!!
*/
#define BLINKER_WIFI
#define BLINKER_ALIGENIE_OUTLET //天猫
#define BLINKER_MIOT_OUTLET //小爱
#define BLINKER_DUEROS_OUTLET //小度
#define BLINKER_ESP_SMARTCONFIG //智能配网
#include <Blinker.h> //放这里编译不出错
#include "OneButton.h"
bool oState = false;
char auth[] = "a90291932382"; //手机上获得的密钥号码
BlinkerButton Button1("btn_1"); //定义一个按钮名称
volatile int btn;
volatile boolean mp_zd;
int32_t j;
volatile int m;
volatile int mp;
volatile boolean btn_zd;
const int buttonPin = D3;
int32_t s1pos;
int last_value1 ;
#define Slider_1 "a"
BlinkerSlider Slider1(Slider_1);
//转动角度
void slider1_callback(int32_t value1) //定义转动角度滑动条数值为value
{
int reading = digitalRead(last_value1);
BLINKER_LOG("get slider value: ", value1);
if (reading != value1)
{
s1pos = value1;
}
else {
s1pos=last_value1;
}
Serial.println(s1pos);
}
ICACHE_RAM_ATTR void Function_btn_zd()
{
btn_zd = true;
}
ICACHE_RAM_ATTR void Function_mp_zd()
{
mp_zd = true;
}
//按键on的动作
void button1_callback(const String & state1) //操作按钮
{
BLINKER_LOG("get button state1: ", state1);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
main1();
}
//按键off的动作
void button2_callback(const String & state2) //操作按钮
{
BLINKER_LOG("get button state2: ", state2);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
main1();
}
//机械按键主函数
void main1() {
mapan();
btn_zd=true;
mp_zd=true;
Key1();
if (j == s1pos) {
stop();
} else if (j >= s1pos*2) {
stop();
j = 0;
}
else if (btn == 2 ) {
stop();
}
else if (btn == 1 || btn == 3) {
two();
last_value1= s1pos;
}
}
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
//按键被单击 这里用了去抖程序
void Key1() {
long debounceDelay = 50;
btn_zd=true;
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
btn=btn+1;
Serial.println(btn);
if (btn == 4) {
btn = 0;}
}
}
}
lastButtonState = reading;
}
//(判断按键是否被按压1次或者单击按键次数为3次,正转或者反转)
void two() {
mapan();
if (j < s1pos) {
go(); }
else if (j > s1pos && j < s1pos*2) {
back(); }
else if (j == s1pos) {
stop(); }
else if (j = s1pos*2) {
stop();
j=0; }
else if (j > s1pos*2) {
stop(); }
last_value1= s1pos;
}
//码盘计数函数
void mapan() {
mp = digitalRead(D1);
if (mp == HIGH) {
m = m + 1;
} else if (mp == LOW && m >= 1) {
j = j + 1;
m = 0;
Serial.println(j);
}
}
//电机正转函数
void go() {
digitalWrite(14,HIGH);
digitalWrite(12,LOW);
}
//电机倒转函数
void back() {
digitalWrite(14,LOW);
digitalWrite(12,HIGH);
}
//停止函数
void stop() {
btn_zd=true;
mp_zd=true;
digitalWrite(14,LOW);
digitalWrite(12,LOW);
}
void miotPowerState(const String & state)//小爱函数
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
main1();
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
main1();
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = false;
}
}
/*IO操作函数*/
void duerPowerState(const String & state)//小度操作函数
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
main1();
BlinkerDuerOS.powerState("on");
BlinkerDuerOS.print();
}
else if (state == BLINKER_CMD_OFF) {
main1();
BlinkerDuerOS.powerState("off");
BlinkerDuerOS.print();
oState = false;
}
}
void aligeniePowerState(const String & state)//天猫开机函数
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
main1();
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
main1();
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
oState = false;
}
}
/*反馈函数不用动*/
void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
}
}
void duerQuery(int32_t queryCode)
{
BLINKER_LOG("DuerOS Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("DuerOS Query power state");
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
break;
case BLINKER_CMD_QUERY_TIME_NUMBER :
BLINKER_LOG("DuerOS Query time");
BlinkerDuerOS.time(millis());
BlinkerDuerOS.print();
break;
default :
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
break;
}
}
void aligenieQuery(int32_t queryCode)
{
BLINKER_LOG("AliGenie Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("AliGenie Query All");
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
default :
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
}
}
/*反馈函数不用动*/
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial); //初始化上网账号
Blinker.begin(auth);
Blinker.attachData(dataRead);//调用id
BlinkerAliGenie.attachPowerState(aligeniePowerState);
BlinkerAliGenie.attachQuery(aligenieQuery);//天猫精灵
BlinkerMIOT.attachPowerState(miotPowerState);//小爱
BlinkerMIOT.attachQuery(miotQuery);
BlinkerDuerOS.attachPowerState(duerPowerState);//小度
BlinkerDuerOS.attachQuery(duerQuery);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN,LOW);
btn = 0; //按键次数归零
m = 0; //码盘m次数归零
j = 0; //码盘j次数归零 高低电平变化为一次计数
btn_zd = false; //初始化中断
mp_zd = false; //初始化中断
pinMode(D1,INPUT); //D1 可用
pinMode(buttonPin, INPUT);
pinMode(14,OUTPUT); //D5
pinMode(12,OUTPUT); //D4
attachInterrupt(D1,Function_mp_zd,CHANGE); //D1'D7
attachInterrupt(D3,Function_btn_zd,CHANGE); // HIGH
Button1.attach(button1_callback);
Slider1.attach(slider1_callback); // 收取滑动条信号
}
void loop()
{
Blinker.run();
main1();
}
|