|
我做一个超声波控制门的,以及LED灯,程序刚开始时,只要不用Blinker控制,传感器可以一直控制,但是只要Blinker随便控制任意设备时,传感器能运行,但是就是控制不了电机,是Blinker会和传感器冲突吗?还是我代码写的有问题?请大佬指点一下。
#define BLINKER_WIFI
#include <Ticker.h>
#include <Servo.h>
#include <analogWrite.h>
#include <Blinker.h>
Ticker ticker2;
Servo myServo;
BlinkerButton Button1("btn-2gv");//车库
BlinkerSlider Slider1("ran-kh2");//红色
BlinkerSlider Slider2("ran-7yh");//绿色
BlinkerSlider Slider3("ran-03a");//蓝色
/*********************************************连接网络***********************************************/
char auth[] = "e02c47aaf828";
char ssid[] = "ABGOGO";
char pswd[] = "ABAB414AAA";
/*********************************************连接网络***********************************************/
int R = 0;
int G = 0;
int B = 0;
int States_door = 0;
const int rLedPin = 2; //引脚R
const int gLedPin = 4; //引脚G
const int bLedPin = 16; //引脚B
const int TrigPin = 23; //发射触发引脚
const int EchoPin = 22;//超声波接收引脚
float distval;//距离
void setup()
{
Serial.begin(115200);
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
Blinker.begin(auth, ssid, pswd);
WiFi.begin(ssid, pswd);
myServo.attach(15);
myServo.write(0);
//Button1.attach(button1_callback);
ticker2.attach(9,door_close);//
Slider1.attach(slider1_callback);
Slider2.attach(slider2_callback);
Slider3.attach(slider3_callback);
while (WiFi.status() != WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
Serial.print(".");
}
}
//#调节LED灯的亮度和颜色
void slider1_callback(int32_t value)//处理 slider 收到数据的回调函数
{
R = value;
analogWrite(rLedPin,R);
}
void slider2_callback(int32_t value)
{
G= value;
analogWrite(gLedPin, G);
}
void slider3_callback(int32_t value)
{
B = value;
analogWrite(bLedPin, B);
}
void button1_callback(const String & state) //控制是开门与关门
{
BLINKER_LOG("get button state: ", state);
if(state == "on")
{
myServo.attach(15);
Button1.print("on");
myServo.write(110);
Blinker.delay(1000);
myServo.detach();
}
else if(state == "off")
{
myServo.attach(15);
Button1.print("off");
myServo.write(0);
Button1.text("关门");
Blinker.delay(1000);
myServo.detach();
}
}
void door_open()
{
digitalWrite(TrigPin,LOW);
delayMicroseconds(2);
digitalWrite(TrigPin,HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin,LOW);//触发一个10us的高电平启动
distval = pulseIn(EchoPin,HIGH)/58;//PulseIn是一个脉冲宽度读取信号,(引脚,读取高/低电平)
if(distval < 16 && States_door = 0)
{
myServo.write(110);
States_door = 1;
}
}
void door_close()
{
/*digitalWrite(TrigPin,LOW);
delayMicroseconds(2);
digitalWrite(TrigPin,HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin,LOW);//触发一个10us的高电平启动
distval = pulseIn(EchoPin,HIGH)/58;//PulseIn是一个脉冲宽度读取信号,(引脚,读取高/低电平)
if(distval > 16 && States_door == 1)*/
if( States_door == 1)
{ myServo.write(0);
States_door = 0;
}
}
void loop()
{
Blinker.run();
door_open();
Serial.println(distval);
Serial.println(States_door);
}
|
|