本帖最后由 海神 于 2021-10-11 09:42 编辑
硬件准备 小车车体 电机驱动模块 连接线 若干
软件准备
Blinker App
代码
- #define BLINKER_PRINT Serial
- #define BLINKER_WIFI
- #include <Blinker.h>
- char auth[] = "873sxxxxx10";
- char ssid[] = "mxxxxxn";
- char pswd[] = "cdxxxxx51";
- #define IN4 D7
- #define IN3 D6
- #define IN1 D4
- #define IN2 D5
- #define tap_time 300
- #define press_time 3000
- #define press_time_zy 1000
- unsigned long t = 0;
- unsigned long timeout = 2000;
- #define BUTTON_0 "btn-t"
- #define BUTTON_1 "btn-s"
- #define BUTTON_2 "btn-x"
- #define BUTTON_3 "btn-z"
- #define BUTTON_4 "btn-y"
- #define Txt_1 "tex"
- BlinkerText Text1(Txt_1);
- BlinkerButton Button0(BUTTON_0);
- BlinkerButton Button1(BUTTON_1);
- BlinkerButton Button2(BUTTON_2);
- BlinkerButton Button3(BUTTON_3);
- BlinkerButton Button4(BUTTON_4);
- void button0_callback(const String &state)
- {
- t = millis();
- timeout = 2000;
- key_parse(0);
- }
- void button1_callback(const String &state)
- {
- t = millis();
- if (state == BLINKER_CMD_BUTTON_TAP)
- {
- timeout = tap_time;
- key_parse(1);
- }
- else if (state == BLINKER_CMD_BUTTON_PRESSED)
- {
- timeout = press_time;
- key_parse(5);
- }
- else if (state == BLINKER_CMD_BUTTON_RELEASED)
- key_parse(0);
- }
- void button2_callback(const String &state)
- {
- t = millis();
- if (state == BLINKER_CMD_BUTTON_TAP)
- {
- timeout = tap_time;
- key_parse(2);
- }
- else if (state == BLINKER_CMD_BUTTON_PRESSED)
- {
- timeout = press_time;
- key_parse(6);
- }
- else if (state == BLINKER_CMD_BUTTON_RELEASED)
- key_parse(0);
- }
- void button3_callback(const String &state)
- {
- t = millis();
- if (state == BLINKER_CMD_BUTTON_TAP)
- {
- timeout = tap_time;
- key_parse(3);
- }
- else if (state == BLINKER_CMD_BUTTON_PRESSED)
- {
- timeout = press_time_zy;
- key_parse(7);
- }
- else if (state == BLINKER_CMD_BUTTON_RELEASED)
- key_parse(0);
- }
- void button4_callback(const String &state)
- {
- t = millis();
- if (state == BLINKER_CMD_BUTTON_TAP)
- {
- timeout = tap_time;
- key_parse(4);
- }
- else if (state == BLINKER_CMD_BUTTON_PRESSED)
- {
- timeout = press_time_zy;
- key_parse(8);
- }
- else if (state == BLINKER_CMD_BUTTON_RELEASED)
- key_parse(0);
- }
- void key_parse(uint8_t car_mode)
- {
- uint8_t L_PWM = 0;
- uint8_t R_PWM = 0;
- bool L_DIR = 0;
- bool R_DIR = 0;
- switch (car_mode)
- {
- case 1: //上tap
- L_PWM = 127;
- R_PWM = 127;
- L_DIR = 1;
- R_DIR = 1;
- break;
- case 2: //下
- L_PWM = 127;
- R_PWM = 127;
- L_DIR = 0;
- R_DIR = 0;
- break;
- case 3: //左
- L_PWM = 127;
- R_PWM = 127;
- L_DIR = 0;
- R_DIR = 1;
- break;
- case 4: //右
- L_PWM = 127;
- R_PWM = 127;
- L_DIR = 1;
- R_DIR = 0;
- break;
- case 5: //上press
- L_PWM = 127;
- R_PWM = 127;
- L_DIR = 1;
- R_DIR = 1;
- break;
- case 6: //下
- L_PWM = 127;
- R_PWM = 127;
- L_DIR = 0;
- R_DIR = 0;
- break;
- case 7: //左
- L_PWM = 127;
- R_PWM = 127;
- L_DIR = 0;
- R_DIR = 1;
- break;
- case 8: //右
- L_PWM = 127;
- R_PWM = 127;
- L_DIR = 1;
- R_DIR = 0;
- break;
- // case 9://左上press
- // break;
- // case 10://左下
- // break;
- // case 11://右上
- // break;
- // case 12://右下
- // break;
- }
- analogWrite(IN3, L_PWM);
- analogWrite(IN2, R_PWM);
- digitalWrite(IN4, L_DIR);
- digitalWrite(IN1, R_DIR);
- Text1.print("run time", t);
- }
- void blinker_car_init()
- {
- pinMode(IN4, OUTPUT);
- pinMode(IN3, OUTPUT);
- pinMode(IN2, OUTPUT);
- pinMode(IN1, OUTPUT);
- analogWrite(IN3, 255);
- analogWrite(IN2, 255);
- digitalWrite(IN4, 1);
- digitalWrite(IN1, 1);
- // pinMode(C_BAR_PIN, INPUT_PULLUP);
- }
- void blinker_car_control(uint8_t cl_pwm, bool cl_dir, uint8_t cr_pwm, bool cr_dir)
- {
- digitalWrite(IN4, cl_dir);
- digitalWrite(IN1, cr_dir);
- analogWrite(IN3, cl_pwm);
- analogWrite(IN2, 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()
- {
- analogWriteRange(255);
- blinker_car_init();
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- Blinker.begin(auth, ssid, pswd);
- Button0.attach(button0_callback);
- Button1.attach(button1_callback);
- Button2.attach(button2_callback);
- Button3.attach(button3_callback);
- Button4.attach(button4_callback);
- }
- void loop()
- {
- Blinker.run();
- if ((millis() - t) > timeout)
- {
- t = millis();
- timeout = 2000;
- blinker_car_init();
- //Serial.println("timeout");
- }
- // 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*/
- }
复制代码
## 编辑blinker手机app界面 1. 点击进入设备界面,点击右上角编辑键,切换到界面编辑模式
2. 添加一个开关组件,点击按键组件, 举例:添加一个 “上键”的按键
选择按键样式,可以选择按键的图标大小。点击“显示图标”可以修改“上键”的图标为一个向上的箭头 随后修改 数据键名、显示文本如图所示: 同时添加布置“下键”“左键”“右键”“中键” 分别修改其键值为“btn-x” “btn-z”“btn-z”“btn-t” 点击右上角解锁,即可完成编辑。
下载完成后打开串口窗口,会出现如下提示,表明wifiduino小车可以通过app控制了。
硬件连接
接线逻辑图 1、Wifiduino与电机驱动板L298模块的连线
wifiduino引脚 | L298电机驱动模块引脚 | 7 | IN4 | 6 | IN3 | 4 | IN1 | 5 | IN2 | VIN | 12V | GND | GND | VCC | 5V |
2、左边电机接L298电机驱动模块的 OUT1 OUT2 右边电机接OUT3 OUT4 3、电池7.4V(2节锂电池)输入给wifiduino DC口供电
确认连线无误后,给小车上电,等待blinker中设备连接在线随后操作手机app的按键,观察小车运动情况。
|