电动玩具车改造

╄→гoц情女王★ 提交于 2020-01-27 08:48:28

 提要:

前两天买奶粉送了一辆电动玩具车,低配版,没有配遥控器,孩子还小,不会踩油门跟方向盘,所以想到自己添加一套来玩,网上查找相关资料,目前设定是  手机+blinker+nodemcu+Arduino+电动车

目前进度:

前后左右按键可以控制方向,中间停止按键停车使用,速度控制滑条来控制速度,当手动速度打开时可以来控制速度,关闭时使用默认速度行走

 

相关步骤:

  1.  待补充
  2.  使用L298n模块来作为电机控制器,参考https://blog.csdn.net/yyandad/article/details/103828020
  3.  待补充
  4. 相关代码参考如下:
/* *****************************************************************
 * *****************************************************************
 * 
 * Blinker 库下载地址:
 * https://github.com/blinker-iot/blinker-library/archive/master.zip
 * 
 * Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、
 * 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、
 * 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。
 * 
 * 如果使用 ESP8266 接入 Blinker,
 * 请确保安装了 2.5.0 或更新的 ESP8266/Arduino 支持包。
 * https://github.com/esp8266/Arduino/releases
 * 
 * 如果使用 ESP32 接入 Blinker,
 * 请确保安装了 1.0.2 或更新的 ESP32/Arduino 支持包。
 * https://github.com/espressif/arduino-esp32/releases
 * 
 * 文档: https://doc.blinker.app/
 *       https://github.com/blinker-iot/blinker-doc/wiki
 * 
 * *****************************************************************/

#define BLINKER_WIFI

#include <Blinker.h>
#define        STOP            0
#define        FORWARD         1
#define        BACKWARD        2
#define        TURNLEFT        3
#define        TURNRIGHT       4
#define        FORSPEED        0
#define        SETSPEED        1
 
//电机控制引脚
#define LEFT_MOTOR1        D1
#define LEFT_MOTOR2        D2
#define RIGHT_MOTOR1       D3
#define RIGHT_MOTOR2       D4
#define LEFT_ENA           D7
#define RIGHT_ENB          D8
#define Slider_1 "SliderKey"  //滑动条   20200103
char auth[] = "2168d652543de";
char ssid[] = "6666";
char pswd[] = "16666";
int cmd = 1;
int cmdspeed = 0;
int highSpeed = 200;
int lowSpeed = 100;
int kzSpeed = 100; 
bool change_flag = false;

// 新建组件对象
BlinkerButton Button0("btn-abc");
BlinkerButton Button1("btn-abd");
BlinkerButton Button2("btn-abe");
BlinkerButton Button3("btn-abf");
BlinkerButton Button4("btn-abg");
BlinkerButton Button5("btn-all");
BlinkerSlider Slider1(Slider_1);   //滑动条   20200103
//滑动条
void slider1_callback(int32_t value)
{
    //digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    //BLINKER_LOG("get slider value: ", value);
    kzSpeed = value;
    BLINKER_LOG("get speed value: ", kzSpeed);
    if (change_flag == true) {
        motor_speed(1);
    } else if(change_flag == false){
        motor_speed(0);     
    }
    
}
//滑动条   20200103
// 按下按键即会执行该函数
void button0_callback(const String & state) {
    BLINKER_LOG("get button0 state: ", state);
    if (state=="off") {
       // motor_speed(0);
        change_flag = false ;
        Button0.print("off");  // 反馈开关状态
        //Button0.color("#262626");
        Button0.text("手动控制速度");
    } else if(state=="on"){
       // motor_speed(1);
        change_flag = true ;
        Button0.print("on");
        Button0.text("固定速度");
    }
}
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);
    Button0.print("off");
    Button1.print("off");
    Button2.print("off");
    Button3.print("off");
    Button4.print("off");
    motor_run(0);
                        
}

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);
    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);
    Slider1.attach(slider1_callback);  //滑动条   20200103
}
/* *****************************************************************
 *
     电机控制函书
 * 
 * *****************************************************************/
void motor_speed(int cmdspeed)
{
        switch(cmdspeed)
        {
                case FORSPEED:
                        highSpeed = 200;
                        lowSpeed = 100;
                        BLINKER_LOG("set speed value: ", highSpeed);
                        BLINKER_LOG("set speed value: ", lowSpeed);
                break;
                case SETSPEED:
                        highSpeed = kzSpeed;
                        lowSpeed = highSpeed/2;
                        BLINKER_LOG("set speed value: ", highSpeed);
                        BLINKER_LOG("set speed value: ", lowSpeed);
                break;
        }
}
void motor_run(int cmd)
{
        switch(cmd)
        {
                case FORWARD:
                        Serial.println("FORWARD"); //输出状态
                        analogWrite(LEFT_ENA,highSpeed);
                        analogWrite(RIGHT_ENB,highSpeed);
                        digitalWrite(LEFT_MOTOR1, LOW);
                        digitalWrite(LEFT_MOTOR2, HIGH);
                        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, HIGH);
                        digitalWrite(LEFT_MOTOR2, HIGH);
                        digitalWrite(RIGHT_MOTOR1, HIGH);
                        digitalWrite(RIGHT_MOTOR2, HIGH);
                break;
        }
}

void loop() {
    Blinker.run();
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!