模块引脚说明如下:
注:已经内置5V供电,不必从外面再接5V输入
输入信息:
ENA和ENB字样的针脚分别连接支持PWM输出的任意数字口,并在程序中通过AnalogWrite()语句实现速度快慢的调节。
电路连接示意图:
程序:
/* *****************************************************************
* *****************************************************************
*
*
* *****************************************************************/
#define BLINKER_WIFI
#include
#define STOP 0
#define FORWARD 1
#define BACKWARD 2
#define TURNLEFT 3
#define TURNRIGHT 4
//电机控制引脚
#define LEFT_MOTOR1 D1
#define LEFT_MOTOR2 D2
#define RIGHT_MOTOR1 D3
#define RIGHT_MOTOR2 D4
#define LEFT_ENA D7
#define RIGHT_ENB D8
char auth[] = "2168d525043de";
char ssid[] = "000";
char pswd[] = "000";
// 新建组件对象
BlinkerButton Button0("btn-abc");
BlinkerButton Button1("btn-abd");
BlinkerButton Button2("btn-abe");
BlinkerButton Button3("btn-abf");
BlinkerButton Button4("btn-abg");
BlinkerButton Button5("btn-all");
BlinkerNumber Number1("num-abc");
int cmd = 1;
int counter = 0;
// 按下按键即会执行该函数
void button0_callback(const String & state)
{
BLINKER_LOG("get button0 state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
void button1_callback(const String & state) {
BLINKER_LOG("get button1 state: ", state);
motor_run(1);
Button1.print("on");
Button2.print("off");
Button3.print("off");
Button4.print("off");
}
void button2_callback(const String & state) {
BLINKER_LOG("get button2 state: ", state);
motor_run(2);
Button1.print("off");
Button2.print("on");
Button3.print("off");
Button4.print("off");
}
void button3_callback(const String & state) {
BLINKER_LOG("get button3 state: ", state);
motor_run(3);
Button1.print("off");
Button2.print("off");
Button3.print("on");
Button4.print("off");
}
void button4_callback(const String & state) {
BLINKER_LOG("get button4 state: ", state);
motor_run(4);
Button1.print("off");
Button2.print("off");
Button3.print("off");
Button4.print("on");
}
void button5_callback(const String & state) {
BLINKER_LOG("get button5 state: ", state);
Button1.print("off");
Button2.print("off");
Button3.print("off");
Button4.print("off");
motor_run(0);
digitalWrite(LEFT_MOTOR1, HIGH);
digitalWrite(LEFT_MOTOR2, HIGH);
digitalWrite(RIGHT_MOTOR1, HIGH);
digitalWrite(RIGHT_MOTOR2, HIGH);
}
// 如果未绑定的组件被触发,则会执行其中内容
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(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(LEFT_MOTOR1, OUTPUT);
pinMode(LEFT_MOTOR2, OUTPUT);
pinMode(RIGHT_MOTOR1, OUTPUT);
pinMode(RIGHT_MOTOR2, OUTPUT);
pinMode(LEFT_ENA,OUTPUT);
pinMode(RIGHT_ENB,OUTPUT);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
motor_run(STOP);
Button0.attach(button0_callback);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
}
/* *****************************************************************
*
电机控制函书
*
* *****************************************************************/
int highSpeed = 200;
int lowSpeed = 100;
void motor_run(int cmd)
{
switch(cmd)
{
case FORWARD:
Serial.println("FORWARD"); //输出状态
analogWrite(LEFT_ENA,highSpeed);
digitalWrite(LEFT_MOTOR1, LOW);
digitalWrite(LEFT_MOTOR2, HIGH);
analogWrite(RIGHT_ENB,highSpeed);
digitalWrite(RIGHT_MOTOR1, LOW);
digitalWrite(RIGHT_MOTOR2, HIGH);
break;
case BACKWARD:
Serial.println("BACKWARD"); //输出状态
digitalWrite(LEFT_MOTOR1, HIGH);
digitalWrite(LEFT_MOTOR2, LOW);
digitalWrite(RIGHT_MOTOR1, HIGH);
digitalWrite(RIGHT_MOTOR2, LOW);
break;
case TURNLEFT:
Serial.println("TURN LEFT"); //输出状态
analogWrite(LEFT_ENA,lowSpeed);
analogWrite(RIGHT_ENB,highSpeed);
digitalWrite(LEFT_MOTOR1, LOW);
digitalWrite(LEFT_MOTOR2, HIGH);//C
digitalWrite(RIGHT_MOTOR1, LOW);
digitalWrite(RIGHT_MOTOR2, HIGH);
break;
case TURNRIGHT:
Serial.println("TURN RIGHT"); //输出状态
analogWrite(LEFT_ENA,highSpeed);
analogWrite(RIGHT_ENB,lowSpeed);
digitalWrite(LEFT_MOTOR1, LOW);
digitalWrite(LEFT_MOTOR2, HIGH);
digitalWrite(RIGHT_MOTOR1, LOW);//C
digitalWrite(RIGHT_MOTOR2, HIGH);
break;
case STOP:
default:
Serial.println("STOP"); //输出状态
analogWrite(LEFT_ENA,1);
analogWrite(RIGHT_ENB,1);
digitalWrite(LEFT_MOTOR1, LOW);
digitalWrite(LEFT_MOTOR2, LOW);
digitalWrite(RIGHT_MOTOR1, LOW);
digitalWrite(RIGHT_MOTOR2, LOW);
break;
}
}
void loop() {
Blinker.run();
}