|
本帖最后由 lslcxlsl 于 2020-10-14 21:18 编辑
小车硬件配置:WeMos(esp8266主板),
L298N电机控制板,
一个2路继电器,
小蚁摄像头,
移动充电宝,
一个12v电池,
履带车底盘
接线方法:esp82266 上D5,D6,D7,D8 接到L298N的控制接口上,56,78 两组成对接,
esp82266 上D2,D4 接继电器的两路控制接口上
小车配置了一个小蚁网络摄像头,可以用手机观看视频和对讲,可以用来看家,代码定义了8个按钮,其中 4,5是控制摄像头电源和led电源,应为摄像头不是夜视的,晚上需要加个灯
接电的方法是我是先接L298N然后通过它的5v输出接esp8266,虽然可以12v直接接esp8266我怕长时间用减少主板寿命。然后接了一个继电器,控制摄像头,和led灯这样摄像头电路和主板电路分开保证电源稳定,用L298N输出5v电源带不起来摄像头。下面是我代码
- #define BLINKER_WIFI
- #include <Blinker.h>
- char auth[] = "8bb519c05ee0";
- char ssid[] = "abc";
- char pswd[] = "chenxu123";
- // 新建组件对象
- BlinkerButton Button1("btn-a");
- BlinkerButton Button2("btn-l");
- BlinkerButton Button3("btn-r");
- BlinkerButton Button6("btn-p");
- BlinkerButton Button7("btn-ll");
- BlinkerButton Button8("btn-rr");
- BlinkerNumber Number1("num-abc");
- BlinkerButton Button4("btn-led");
- BlinkerButton Button5("btn-led1");
- int counter = 0;
- #define L_DIR_PIN D5
- #define L_PWM_PIN D6
- #define R_DIR_PIN D7
- #define R_PWM_PIN D8
- #define TIME_TAP 100
- #define LED0_PIN D2
- #define LED1_PIN D4
- 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(LED0_PIN, OUTPUT);
- pinMode(LED1_PIN, OUTPUT);
- digitalWrite(L_DIR_PIN, LOW);
- digitalWrite(L_PWM_PIN, LOW);
- digitalWrite(R_DIR_PIN, LOW);
- digitalWrite(R_PWM_PIN, LOW);
- digitalWrite(LED0_PIN, LOW);
- digitalWrite(LED1_PIN, LOW);
- }
- // 按下按键即会执行该函数
- void button1_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if(state=="tap"){
- digitalWrite(L_DIR_PIN,HIGH);
- digitalWrite(R_DIR_PIN,HIGH);
- // digitalWrite(LED0_PIN,HIGH);
- delay(TIME_TAP);
- digitalWrite(L_DIR_PIN,LOW);
- digitalWrite(R_DIR_PIN,LOW);
- // digitalWrite(LED0_PIN,LOW);
- }
- if(state=="press"){
- digitalWrite(L_DIR_PIN,HIGH);
- digitalWrite(R_DIR_PIN,HIGH);
- // digitalWrite(LED0_PIN,HIGH);
- }
- if(state=="pressup"){
- digitalWrite(L_DIR_PIN,LOW);
- digitalWrite(R_DIR_PIN,LOW);
- // digitalWrite(LED0_PIN,LOW);
- }
- }
- void button6_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if(state=="tap"){
- digitalWrite(L_PWM_PIN,HIGH);
- digitalWrite(R_PWM_PIN,HIGH);
- // digitalWrite(LED0_PIN,HIGH);
- delay(TIME_TAP);
- digitalWrite(L_PWM_PIN,LOW);
- digitalWrite(R_PWM_PIN,LOW);
- // digitalWrite(LED0_PIN,LOW);
- }
- if(state=="press"){
- digitalWrite(L_PWM_PIN,HIGH);
- digitalWrite(R_PWM_PIN,HIGH);
- // digitalWrite(LED0_PIN,HIGH);
- }
- if(state=="pressup"){
- digitalWrite(L_PWM_PIN,LOW);
- digitalWrite(R_PWM_PIN,LOW);
- // digitalWrite(LED0_PIN,LOW);
- }
- }
- void button7_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if(state=="tap"){
- digitalWrite(L_PWM_PIN,HIGH);
- delay(TIME_TAP);
- digitalWrite(L_PWM_PIN,LOW);
- }
- if(state=="press"){
- digitalWrite(L_PWM_PIN,HIGH);
- }
- if(state=="pressup"){
- digitalWrite(L_PWM_PIN,LOW);
- }
- }
- void button8_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if(state=="tap"){
- digitalWrite(R_PWM_PIN,HIGH);
- delay(TIME_TAP);
- digitalWrite(R_PWM_PIN,LOW);
- }
- if(state=="press"){
- digitalWrite(R_PWM_PIN,HIGH);
- }
- if(state=="pressup"){
- digitalWrite(R_PWM_PIN,LOW);
- }
- }
- void button2_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if(state=="tap"){
- digitalWrite(L_PWM_PIN,HIGH);
- digitalWrite(R_DIR_PIN,HIGH);
- // digitalWrite(LED1_PIN,HIGH);
- delay(TIME_TAP);
- digitalWrite(L_PWM_PIN,LOW);
- digitalWrite(R_DIR_PIN,LOW);
- // digitalWrite(LED1_PIN,LOW);
- }
- if(state=="press"){
- digitalWrite(L_PWM_PIN,HIGH);
- digitalWrite(R_DIR_PIN,HIGH);
- // digitalWrite(LED1_PIN,HIGH);
- }
- if(state=="pressup"){
- digitalWrite(L_PWM_PIN,LOW);
- digitalWrite(R_DIR_PIN,LOW);
- // digitalWrite(LED1_PIN,LOW);
- }
- }
- void button3_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if(state=="tap"){
- digitalWrite(L_DIR_PIN,HIGH);
- digitalWrite(R_PWM_PIN,HIGH);
- delay(TIME_TAP);
- digitalWrite(L_DIR_PIN,LOW);
- digitalWrite(R_PWM_PIN,LOW);
- }
- if(state=="press"){
- digitalWrite(L_DIR_PIN,HIGH);
- digitalWrite(R_PWM_PIN,HIGH);
- }
- if(state=="pressup"){
- digitalWrite(L_DIR_PIN,LOW);
- digitalWrite(R_PWM_PIN,LOW);
- }
- }
- void button4_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if (state=="on") {
- digitalWrite(LED0_PIN, HIGH);
- // 反馈开关状态
- Button4.print("on");
- } else if(state=="off"){
- digitalWrite(LED0_PIN, LOW);
- // 反馈开关状态
- Button4.print("off");
- }
- }
- void button5_callback(const String & state) {
- BLINKER_LOG("get button state: ", state);
- if (state=="on") {
- digitalWrite(LED1_PIN, HIGH);
- // 反馈开关状态
- Button5.print("on");
- } else if(state=="off"){
- digitalWrite(LED1_PIN, LOW);
- // 反馈开关状态
- Button5.print("off");
- }
- }
- // 如果未绑定的组件被触发,则会执行其中内容
- 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
- blinker_car_init();
- pinMode(LED_BUILTIN, OUTPUT);
- // 初始化blinker
- Blinker.begin(auth, ssid, pswd);
- Blinker.attachData(dataRead);
- Button1.attach(button1_callback);
- Button2.attach(button2_callback);
- Button3.attach(button3_callback);
- Button4.attach(button4_callback);
- Button5.attach(button5_callback);
- Button6.attach(button6_callback);
- Button7.attach(button7_callback);
- Button8.attach(button8_callback);
- }
- void loop() {
- Blinker.run();
- }
复制代码
|
|