使用元件
- TT馬達+65mm 車輪 X 4
- L298N 直流馬達驅動板
- ESP8266 mini
接線圖:
使用MIT App inventor 2開發APP
完成影片
完整程式碼:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
int _speed = 512;
const char* ssid = "esp8266car";
const char* pwd = "your-pwd";
ESP8266WebServer server(8080);
const int PIN1A=D6;
const int PIN1B=D5;
const int PIN2A=D7;
const int PIN2B=D8;
void handleMove() {
String action = "";
for (uint8_t i = 0; i < server.args(); i++) {
action = server.arg(i);
if (action == "F") {
forward();
}
if (action == "B") {
backward();
}
if (action == "R") {
right();
}
if (action == "L") {
left();
}
if (action == "S") {
_stop();
}
}
server.send(200, "text/html", "");
}
void handleSpeed() {
for (uint8_t i = 0; i < server.args(); i++) {
int sp = server.arg(i).toInt();
_speed = 512+sp*64;
}
server.send(200, "text/html", "");
}
void forward() {
analogWrite(PIN1A, _speed);
analogWrite(PIN1B, 0);
analogWrite(PIN2A, _speed);
analogWrite(PIN2B, 0);
}
void backward() {
analogWrite(PIN1A, 0);
analogWrite(PIN1B, _speed);
analogWrite(PIN2A, 0);
analogWrite(PIN2B, _speed);
}
void right() {
analogWrite(PIN1A, _speed);
analogWrite(PIN1B, 0);
analogWrite(PIN2A, _speed/4);
analogWrite(PIN2B, 0);
}
void left() {
analogWrite(PIN1A, _speed/4);
analogWrite(PIN1B, 0);
analogWrite(PIN2A, _speed);
analogWrite(PIN2B, 0);
}
void _stop() {
analogWrite(PIN1A, 0);
analogWrite(PIN1B, 0);
analogWrite(PIN2A, 0);
analogWrite(PIN2B, 0);
}
void setup() {
delay(1000);
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(ssid, pwd);
server.on("/move", handleMove);
server.on("/speed", handleSpeed );
server.begin();
pinMode(PIN1A, OUTPUT);
pinMode(PIN1B, OUTPUT);
pinMode(PIN2A, OUTPUT);
pinMode(PIN2B, OUTPUT);
}
void loop() {
server.handleClient();
}




沒有留言:
張貼留言