|
#define BLINKER_WIFI
#include <Blinker.h>
char auth[] = "15991537557UF";
char ssid[] = "sxbctvnet-张军";
char pswd[] = "13689216174";
//新建组件对象
BlinkerButton Button1("btn-a"); //前进
BlinkerButton Button2("btn-1"); //左转
BlinkerButton Button3("btn-r"); //右转
BlinkerButton Button4("btn-b"); //后退
BlinkerButton Button5("btn-t"); //停车
BlinkerButton Button6("num-abc");
BlinkerButton Button7("btn-q");
BlinkerButton Button8("btn-h");
BlinkerButton Button9("btn-y");
BlinkerButton Button10("btn-z");
int counter = 0;
#define IN1 D5 //左转引脚
#define IN2 D6 //左转引脚调值
#define IN3 D7 //右转引脚
#define IN4 D8 //右转引脚调值
#define TIME_TAP 100 //延时参数
void blinker_car_init()
{
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
//按下按键会立即执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state=="tap")
{
digitalWrite(IN1,HIGH);
digitalWrite(IN3,HIGH);
delay(TIME_TAP);
digitalWrite(IN1,LOW); //右轮前进
digitalWrite(IN3,LOW); //左轮前进
}
if(state=="press")
{
digitalWrite(IN1,HIGH);
digitalWrite(IN3,HIGH);
}
if(state=="pressup")
{
digitalWrite(IN1,LOW); //右轮前进
digitalWrite(IN3,LOW); //左轮前进
}
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state=="tap")
{
digitalWrite(IN1,HIGH);
delay(180);
digitalWrite(IN1,LOW); //右轮前进
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW); //左轮不动
}
}
void button3_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state=="tap")
{
digitalWrite(IN3,HIGH);
delay(180);
digitalWrite(IN1,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN2,LOW); //右轮不动
digitalWrite(IN4,LOW); //左轮前进
}
if(state=="press")
{
digitalWrite(IN3,HIGH);
}
if(state=="pressup")
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW); //右轮不动
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW); //左轮前进
}
}
void button4_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state=="tap")
{
digitalWrite(IN2,HIGH);
digitalWrite(IN4,HIGH);
delay(TIME_TAP);
digitalWrite(IN2,LOW); //右轮不动
digitalWrite(IN4,LOW); //左轮前进
}
if(state=="press")
{
digitalWrite(IN2,HIGH); //右轮后退
digitalWrite(IN4,HIGH); //左轮后退
}
if(state=="pressup")
{
digitalWrite(IN2,LOW);
digitalWrite(IN4,LOW); //右轮不动
digitalWrite(IN3,LOW);
digitalWrite(IN1,LOW); //左轮前进
}
}
void button5_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state=="tap")
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN4,LOW);
digitalWrite(IN3,LOW);
}
if(state=="press")
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN4,LOW);
digitalWrite(IN3,LOW);
}
if(state=="pressup")
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN4,LOW);
digitalWrite(IN3,LOW);
}
}
void button7_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state=="tap")
{
digitalWrite(IN1,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN4,LOW);
}
}
void button8_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state=="tap")
{
digitalWrite(IN2,HIGH);
digitalWrite(IN4,HIGH);
digitalWrite(IN1,LOW);
digitalWrite(IN3,LOW);
}
}
void button9_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state=="tap")
{
analogWrite(IN1,1023);
analogWrite(IN3,500);
delay(500);
digitalWrite(IN1,LOW);
digitalWrite(IN3,LOW);
}
if(state=="press")
{
analogWrite(IN1,1023);
analogWrite(IN3,500);
}
if(state=="pressup")
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN4,LOW);
digitalWrite(IN3,LOW);
}
}
void button10_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if(state=="tap")
{
analogWrite(IN3,1023);
analogWrite(IN1,500);
delay(500);
digitalWrite(IN1,LOW);
digitalWrite(IN3,LOW);
}
if(state=="press")
{
analogWrite(IN3,1023);
analogWrite(IN1,500);
}
if(state=="pressup")
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN4,LOW);
digitalWrite(IN3,LOW);
}
}
//如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Number6.print(counter);
}
void setup(){
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
//初始化有LED的IO
blinker _car_init();
pinMode(LED_BULLTIN,OUTPUT);
//初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Button2.attach(button1_callback);
Button3.attach(button1_callback);
Button4.attach(button1_callback);
Button5.attach(button1_callback);
Button6.attach(button1_callback);
Button7.attach(button1_callback);
Button8.attach(button1_callback);
Button9.attach(button1_callback);
Button10.attach(button1_callback);
}
void loop()
{
Blinker.run();
}
|
|