赞
踩
材料:
ESP8266 12F一块(10元出头)
L298N电机驱动模块一块(10元左右)
12V电源(15左右)
杜邦线若根
小车底盘+电机+螺丝+轮子(网上套件也就20多块钱)
接线:
8266的VIN针脚用杜邦线接L298N的5v孔
8266的G针脚用杜邦线接L298N的GND孔
8266的D0,D1,D2,D4针脚分别接到L298N的IN1,IN2,IN3,IN4针脚
左边两个电机分别接到OUT1,2口
右边两个电机分别接到OUT3,4口
不会接的有详细教程,下面放链接
代码部分:
- #define BLINKER_PRINT Serial
- #include <Servo.h>
- #include <ESP8266WiFi.h>
- #include <DNSServer.h>
- #include <ESP8266WebServer.h>
- #include <WiFiManager.h>
- #define BLINKER_WIFI
- //#define BLINKER_ESP_SMARTCONFIG//
- #include <Blinker.h>
-
-
-
-
- char auth[] = "2e5a0cbf4e19";
- char ssid[] = "";
- char pswd[] = "";
-
-
- #define BUTTON_2 "b2"
- #define BUTTON_4 "b4"
- #define BUTTON_5 "b5"
- #define BUTTON_6 "b6"
- #define BUTTON_8 "b8"
-
- #define slider_0 "r1"
- #define slider_1 "r2"
-
-
- BlinkerButton Button2(BUTTON_2);
- BlinkerButton Button4(BUTTON_4);
- BlinkerButton Button5(BUTTON_5);
- BlinkerButton Button6(BUTTON_6);
- BlinkerButton Button8(BUTTON_8);
-
- BlinkerSlider Slider0(slider_0);
- BlinkerSlider Slider1(slider_1);
-
-
- int pwm1=60;
- int pwm2=60;
-
- int flag;//判断当前状态
-
- void button2_callback(const String &state)//前进
- {
- BLINKER_LOG("get button state: ", state);
- if (state != "pressup")
- {
- flag=1;
- analogWrite(16, pwm1);
- digitalWrite(5, LOW);
- analogWrite(4, pwm2);
- digitalWrite(2, LOW);
-
- }
- }
-
- void button4_callback(const String &state)//左转
- {
- digitalWrite(16, LOW);
- analogWrite(5, pwm1);
-
- analogWrite(4, pwm2);
- digitalWrite(2, LOW);
-
- flag=3;
-
- }
-
- void button5_callback(const String &state)//停止
- {
- BLINKER_LOG("get button state: ", state);
- if (state != "pressup")
- {
- digitalWrite(16, HIGH);
- digitalWrite(5, HIGH);
- digitalWrite(4, HIGH);
- digitalWrite(2, HIGH);
- flag=5;
- }
- }
-
- void button6_callback(const String &state)//右转
- {
- BLINKER_LOG("get button state: ", state);
- if (state != "pressup")
- {
- analogWrite(16, pwm1);
- digitalWrite(5,LOW);
-
- digitalWrite(4, LOW);
- analogWrite(2,pwm2);
- flag=4;
- }
- }
-
- void button8_callback(const String &state)//后退
- {
- BLINKER_LOG("get button state: ", state);
- if (state != "pressup")
- {
- digitalWrite(16, LOW);
- analogWrite(5,pwm1);
- digitalWrite(4, LOW);
- analogWrite(2,pwm2);
- flag=2;
- }
- }
-
- void slider0_callback(int32_t value)
- {
- BLINKER_LOG("slider:", value);
- pwm1=value;
- switch(flag)
- {
- case 1: analogWrite(16, pwm1); break;
- case 2: analogWrite(5, pwm1); break;
- case 3: analogWrite(5, pwm1); break;
- case 4: analogWrite(16, pwm1); break;
- case 5: break;
- default:break;
-
- }
-
- }
-
- void slider1_callback(int32_t value)
- {
- BLINKER_LOG("slider:", value);
- pwm2=value;
- switch(flag)
- {
- case 1: analogWrite(4, pwm2); break;
- case 2: analogWrite(2, pwm2); break;
- case 3: analogWrite(2, pwm2); break;
- case 4: analogWrite(4, pwm2); break;
- case 5: break;
- default:break;
-
- }
-
- }
-
- //blinker初始化
- void blinker_car_init()
- {
- //1-4号引脚设置为输出模式
- pinMode(16, OUTPUT);//D0
- pinMode(5, OUTPUT);//D1
- pinMode(4, OUTPUT);//D2
- pinMode(2, OUTPUT);//D4
- }
-
- void setup()
- {
- // 初始化串口,并开启调试信息
-
- Serial.begin(115200);
-
- WiFiManager wifiManager;//自动配网
-
- wifiManager.autoConnect("北城以北");
- Serial.println("connected... :)");
-
- digitalWrite(D0, HIGH);
- digitalWrite(D1, HIGH);
- digitalWrite(D2, HIGH);
- digitalWrite(D4, HIGH);
- analogWriteRange(100);
- analogWriteFreq(50);
-
- //analogWrite(D2, 50);//0-100 50
- //初始化小车参数
- blinker_car_init();
-
-
-
- //按钮绑定
-
- Button2.attach(button2_callback);
- Button4.attach(button4_callback);
- Button5.attach(button5_callback);
- Button6.attach(button6_callback);
- Button8.attach(button8_callback);
- Slider0.attach(slider0_callback);
- Slider1.attach(slider1_callback);
- Blinker.begin(auth,ssid,pswd);
-
- // Blinker.begin(auth);
-
- }
-
-
- void loop()
- {
- Blinker.run();
-
- }
char auth[] = "2e5a0cbf4e19";//填写你的点灯密钥
char ssid[] = "";//可以不用填,配网的时候在填写
char pswd[] = "";//可以不用填,配网的时候在填写
把代码复制到arduino,需要的库自己下载,很基础的东西,我就不说了,不会的网上搜一下
这里强调一下就是代码上传的时候,工具里的flash size后面不能选择no SPIFFS,一定要注意,很多人就是因为没有注意这个地方,导致每次断电重启后,都需要重新配网,设备无法记住WIFI的账号密码!
有什么问题评论区留言,我有时间尽量会回复大家!平时比较忙,回复慢了还望见谅!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。