本贴已作废,请看最新的开发文档
本示例将展示如何制作一个WiFi遥控的小车,制作本项目前,你最好已经掌握一定的Arduino小车制作基础。
演示视频,稍后上传
硬件准备
WiFiduino 或其他 esp8266开发板
小车套件
将WiFiduino与小车电机驱动相连
软件准备
下载并安装Arduino IDE
点击去下载
下载并安装esp8266扩展
点击去下载
下载并加载Blinker-library
点击去下载
下载并安装blinker APP
android下载:点击下载
IOS下载:app store中搜索“blinker”下载
编译上传示例程序
将示例程序上传到Arduino中
其中ssid和pswd需写入你当前WiFi的ssid和密码
[mw_shl_code=cpp,true]
char ssid[] = "<Your WiFi network SSID or name>";
char pswd[] = "<Your WiFi network WPA password or WEP key>";[/mw_shl_code]
完整示例程序如下:
[mw_shl_code=cpp,true]/*
* Blinker WiFi CAR, support WiFiduino
*/
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#include <Blinker.h>
char ssid[] = "<Your WiFi network SSID or name>";
char pswd[] = "<Your WiFi network WPA password or WEP key>";
#define L_DIR_PIN 14
#define L_PWM_PIN 16
#define R_DIR_PIN 4
#define R_PWM_PIN 5
#define C_BAR_PIN 12
uint32_t car_os_time = millis();
bool isWarn = false;
void blinker_car_init()
{
pinMode(L_DIR_PIN, OUTPUT);
pinMode(L_PWM_PIN, OUTPUT);
pinMode(R_DIR_PIN, OUTPUT);
pinMode(R_PWM_PIN, OUTPUT);
pinMode(C_BAR_PIN, INPUT_PULLUP);
// #if defined(ESP8266)
analogWriteRange(255);
// #endif
}
void blinker_car_parse(uint8_t &_L_PWM, bool &_L_DIR, uint8_t &_R_PWM, bool &_R_DIR)
{
uint8_t xAxis = Blinker.joystick(J_Xaxis);
uint8_t yAxis = Blinker.joystick(J_Yaxis);
bool isStop = false;
if (yAxis < 128) {
_L_DIR = true;
_R_DIR = true;
_L_PWM = map(abs(128-yAxis), 0, 128, 128, 255);
_R_PWM = map(abs(128-yAxis), 0, 128, 128, 255);
}
else if (yAxis > 128) {
_L_DIR = false;
_R_DIR = false;
_L_PWM = map(abs(128-yAxis), 0, 128, 128, 255);
_R_PWM = map(abs(128-yAxis), 0, 128, 128, 255);
}
else {
isStop = true;
_L_PWM = 0;
_R_PWM = 0;
}
if (xAxis < 128) {
if (isStop) {
_L_DIR = false;
_R_DIR = true;
_L_PWM = map(abs(128-xAxis), 0, 128, 128, 255);
_R_PWM = map(abs(128-xAxis), 0, 128, 128, 255);
}
else {
_L_PWM -= map(abs(128-xAxis), 0, 128, 0, 128);
}
}
else if (xAxis > 128) {
if (isStop) {
_L_DIR = true;
_R_DIR = false;
}
else {
_R_PWM -= map(abs(128-xAxis), 0, 128, 0, 128);
}
}
else {
if (isStop) {
_L_PWM = 0;
_R_PWM = 0;
}
}
}
void blinker_car_control(uint8_t cl_pwm, bool cl_dir, uint8_t cr_pwm, bool cr_dir)
{
digitalWrite(L_DIR_PIN, cl_dir);
digitalWrite(R_DIR_PIN, cr_dir);
analogWrite(L_PWM_PIN, cl_pwm);
analogWrite(R_PWM_PIN, cr_pwm);
}
void blinker_car_detect()
{
if (!digitalRead(C_BAR_PIN)) {
if (!isWarn) {
Blinker.vibrate();
isWarn = true;
car_os_time = millis();
}
else if (millis() - car_os_time >= 5000) {
isWarn = false;
}
}
else {
isWarn = false;
}
}
void setup()
{
Serial.begin(115200);
blinker_car_init();
Blinker.begin(ssid, pswd);
}
void loop()
{
Blinker.run();
uint8_t L_PWM = 0;
uint8_t R_PWM = 0;
bool L_DIR = false;
bool R_DIR = false;
blinker_car_parse(L_PWM, L_DIR, R_PWM, R_DIR);
blinker_car_control(L_PWM, L_DIR, R_PWM, R_DIR);
blinker_car_detect();
#if defined(BLINKER_CAR_DEBUG)
BLINKER_LOG4("L_PWM: ", L_PWM, " | L_DIR: ", L_DIR);
BLINKER_LOG4("R_PWM: ", R_PWM, " | R_DIR: ", R_DIR);
Blinker.delay(2000);
#endif
}[/mw_shl_code]
添加设备
确保开发板和手机在同一局域网下
在blinker app中,点击右上角的“+”进入添加设备页面
点击选择 Arduino > WiFi接入
等待搜索设备
点击选择要接入的设备
控制设备
在 我的设备 页面点击设备,进入控制面板
点击右上角 编辑界面 按钮
添加 摇杆组件
点击界面右上角保存界面布局
操作组件即可控制小车移动了
其他
你可以添加其他组件,给小车定制更多功能
还有问题可以回帖了解
|