優(yōu)化/新增 動(dòng)作

代碼分享:
詳細(xì)制作流程和原理都在那~(底層知識(shí)還得看太極創(chuàng)客的esp8266教程)
我只是改了/增加些動(dòng)作函數(shù),想要的可以直接拿走,記得微調(diào)一下(每個(gè)舵機(jī)天生的齒輪角度都不一樣)
最后,電壓不要超!我已經(jīng)燒壞過一個(gè)主板了_(-ω-`_)⌒)_
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
const char* ssid = "網(wǎng)絡(luò)名稱";
const char* password = "網(wǎng)絡(luò)密碼";
WiFiServer server(80); //http的服務(wù)端口
void setup() {
pinMode(16, OUTPUT); //用于連接Wi-Fi的LED指示燈
digitalWrite(16, LOW);
Serial.begin(115200);
pwm.begin();
pwm.setPWMFreq(60); //伺服電機(jī)以60hz刷新率工作
delay(1000);
//連接到無線網(wǎng)絡(luò)
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("-");
}
Serial.println("");
Serial.println("WiFi connected");
//啟動(dòng)服務(wù)器
server.begin();
Serial.println("Server started");
//打印IP地址
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
digitalWrite(16, HIGH);
}
void loop() {
//檢查客戶端是否已連接
WiFiClient client = server.available();
if (!client) {
return;
}
//等到客戶端發(fā)送一些數(shù)據(jù)
Serial.println("new client");
while (!client.available()) {
delay(1);
}
//閱讀請(qǐng)求的第一行
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
//匹配請(qǐng)求
if (request.indexOf("/S") != -1) {
sleep();
}
if (request.indexOf("/N") != -1) {
normal();
}
if (request.indexOf("/F") != -1) {
normal();
forward();
forward();
forward();
forward();
}
if (request.indexOf("/B") != -1) {
normal();
backward();
backward();
backward();
backward();
}
if (request.indexOf("/L") != -1) {
normal();
left();
left();
left();
}
if (request.indexOf("/R") != -1) {
normal();
right();
right();
right();
right();
}
if (request.indexOf("/H") != -1) {
normal();
hello();
}
if (request.indexOf("/C") != -1) {
normal();
come();
}
if (request.indexOf("/D") != -1) {
sleep();
dog();
dog();
dog();
dog();
}
if (request.indexOf("/M") != -1) {
middle();
}
if (request.indexOf("/U") != -1) {
sleep();
surrender();
}
if (request.indexOf("/W") != -1) {
normal();
swing();
normal();
}
//返回響應(yīng)
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<style>");
client.println(".button{");
/*----------網(wǎng)頁設(shè)計(jì)----------*/
client.println("background-color: #4CAF50; /* GREEN */");
client.println("width: 48%;");
client.println("border: none;");
client.println("color: white;");
client.println("padding: 50px 40px;");
client.println("text-align: center;");
client.println("text-decoration: none;");
client.println("display: inline-block;");
client.println("font-size: 50px;");
client.println("margin: 4px 2px;");
client.println("cursor: pointer;");
client.println("border-radius: 12px;");
client.println("}");
client.println("</style>");
client.println("</head>");
client.println("<body bgcolor=black>");
client.println("<button class=\"button\" onclick=\"window.location.href='/F';\">前進(jìn)</button>");
client.println("<button class=\"button\" onclick=\"window.location.href='/B';\">后退</button>");
client.println("<br></br>");
client.println("<button class=\"button\" onclick=\"window.location.href='/L';\">左轉(zhuǎn)</button>");
client.println("<button class=\"button\" onclick=\"window.location.href='/R';\">右轉(zhuǎn)</button>");
client.println("<br></br>");
client.println("<button class=\"button\" onclick=\"window.location.href='/S';\">睡覺</button>");
client.println("<button class=\"button\" onclick=\"window.location.href='/N';\">正常</button>");
client.println("<br></br>");
client.println("<button class=\"button\" onclick=\"window.location.href='/H';\">你好</button>");
client.println("<button class=\"button\" onclick=\"window.location.href='/C';\">來啊</button>");
client.println("<br></br>");
client.println("<button class=\"button\" onclick=\"window.location.href='/D';\">狗刨</button>");
client.println("<button class=\"button\" onclick=\"window.location.href='/M';\">中指</button>");
client.println("<br></br>");
client.println("<button class=\"button\" onclick=\"window.location.href='/U';\">投降</button>");
client.println("<button class=\"button\" onclick=\"window.location.href='/W';\">搖擺</button>");
client.println("</body>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}
/*----------動(dòng)作函數(shù)----------*/
// | 7 | | 6 |
// ----- ----- ----- -----
// | 3 | | 2 |
// ----- -----
// | 1 | | 0 |
// ----- ----- ----- -----
// | 5 | | 4 |
void sleep()
{
pwm.setPWM(4, 0, 340);
delay(100);
pwm.setPWM(5, 0, 370);
delay(100);
pwm.setPWM(6, 0, 360);
delay(100);
pwm.setPWM(7, 0, 410);
delay(100);
pwm.setPWM(0, 0, 370);
delay(100);
pwm.setPWM(1, 0, 190);
delay(100);
pwm.setPWM(2, 0, 180);
delay(100);
pwm.setPWM(3, 0, 390);
}
void normal()
{
pwm.setPWM(0, 0, 280);
delay(100);
pwm.setPWM(1, 0, 280);
delay(100);
pwm.setPWM(2, 0, 270);
delay(100);
pwm.setPWM(3, 0, 300);
delay(100);
pwm.setPWM(4, 0, 600);
delay(100);
pwm.setPWM(5, 0, 120);
delay(100);
pwm.setPWM(6, 0, 620);
delay(100);
pwm.setPWM(7, 0, 160);
}
void left()
{
pwm.setPWM(4, 0, 450);
pwm.setPWM(7, 0, 260);
delay(100);
pwm.setPWM(0, 0, 370);
pwm.setPWM(3, 0, 390);
delay(100);
pwm.setPWM(4, 0, 600);
pwm.setPWM(7, 0, 160);
delay(100);
pwm.setPWM(6, 0, 460);
pwm.setPWM(5, 0, 280);
delay(100);
pwm.setPWM(2, 0, 450);
pwm.setPWM(1, 0, 460);
delay(100);
pwm.setPWM(6, 0, 620);
pwm.setPWM(5, 0, 120);
delay(100);
pwm.setPWM(0, 0, 280);
pwm.setPWM(1, 0, 280);
pwm.setPWM(2, 0, 270);
pwm.setPWM(3, 0, 300);
delay(100);
}
void right()
{
pwm.setPWM(5, 0, 280);
pwm.setPWM(6, 0, 490);
delay(100);
pwm.setPWM(1, 0, 190);
pwm.setPWM(2, 0, 180);
delay(100);
pwm.setPWM(5, 0, 120);
pwm.setPWM(6, 0, 620);
delay(100);
pwm.setPWM(7, 0, 260);
pwm.setPWM(4, 0, 450);
delay(100);
pwm.setPWM(3, 0, 210);
pwm.setPWM(0, 0, 190);
delay(100);
pwm.setPWM(7, 0, 160);
pwm.setPWM(4, 0, 600);
delay(100);
pwm.setPWM(0, 0, 280);
pwm.setPWM(1, 0, 280);
pwm.setPWM(2, 0, 270);
pwm.setPWM(3, 0, 300);
delay(100);
}
void forward()
{
pwm.setPWM(7, 0, 260);
pwm.setPWM(4, 0, 450);
delay(50);
pwm.setPWM(3, 0, 390);
pwm.setPWM(0, 0, 120);
delay(100);
pwm.setPWM(7, 0, 160);
pwm.setPWM(4, 0, 600);
delay(100);
pwm.setPWM(0, 0, 280);
pwm.setPWM(3, 0, 300);
pwm.setPWM(6, 0, 490);
pwm.setPWM(5, 0, 280);
delay(50);
pwm.setPWM(2, 0, 180);
pwm.setPWM(1, 0, 460);
delay(100);
pwm.setPWM(6, 0, 620);
pwm.setPWM(5, 0, 120);
delay(100);
pwm.setPWM(1, 0, 280);
pwm.setPWM(2, 0, 270);
}
void backward()
{
pwm.setPWM(5, 0, 280);
pwm.setPWM(6, 0, 490);
delay(50);
pwm.setPWM(1, 0, 190);
pwm.setPWM(2, 0, 450);
delay(100);
pwm.setPWM(5, 0, 120);
pwm.setPWM(6, 0, 620);
delay(100);
pwm.setPWM(1, 0, 280);
pwm.setPWM(2, 0, 270);
pwm.setPWM(4, 0, 450);
pwm.setPWM(7, 0, 260);
delay(50);
pwm.setPWM(0, 0, 370);
pwm.setPWM(3, 0, 140);
delay(100);
pwm.setPWM(4, 0, 600);
pwm.setPWM(7, 0, 160);
delay(100);
pwm.setPWM(0, 0, 280);
pwm.setPWM(3, 0, 300);
}
void hello()
{
pwm.setPWM(7, 0, 300);
delay(100);
pwm.setPWM(0, 0, 370);
delay(200);
pwm.setPWM(4, 0, 110);
delay(300);
pwm.setPWM(4, 0, 340);
delay(300);
pwm.setPWM(4, 0, 110);
delay(300);
pwm.setPWM(4, 0, 340);
delay(300);
pwm.setPWM(4, 0, 110);
delay(300);
pwm.setPWM(4, 0, 340);
delay(500);
pwm.setPWM(0, 0, 280);
pwm.setPWM(4, 0, 600);
pwm.setPWM(7, 0, 160);
}
void come()
{
pwm.setPWM(7, 0, 300);
delay(100);
pwm.setPWM(0, 0, 120);
pwm.setPWM(4, 0, 280);
delay(500);
pwm.setPWM(4, 0, 110);
delay(200);
pwm.setPWM(4, 0, 340);
delay(400);
pwm.setPWM(4, 0, 110);
delay(200);
pwm.setPWM(4, 0, 340);
delay(400);
pwm.setPWM(0, 0, 280);
pwm.setPWM(4, 0, 600);
pwm.setPWM(7, 0, 160);
}
void dog()
{
delay(300);
pwm.setPWM(0, 0, 120);
pwm.setPWM(1, 0, 460);
pwm.setPWM(2, 0, 450);
pwm.setPWM(3, 0, 140);
delay(300);
pwm.setPWM(0, 0, 370);
pwm.setPWM(1, 0, 190);
pwm.setPWM(2, 0, 180);
pwm.setPWM(3, 0, 390);
}
void middle()
{
pwm.setPWM(7, 0, 300);
delay(100);
pwm.setPWM(0, 0, 120);
delay(100);
pwm.setPWM(4, 0, 110);
}
void surrender()
{
delay(100);
pwm.setPWM(4, 0, 110);
pwm.setPWM(5, 0, 590);
pwm.setPWM(6, 0, 110);
pwm.setPWM(7, 0, 640);
}
void swing()
{
delay(50);
pwm.setPWM(4, 0, 450);
pwm.setPWM(7, 0, 260);
delay(50);
pwm.setPWM(0, 0, 370);
pwm.setPWM(3, 0, 390);
delay(50);
pwm.setPWM(4, 0, 600);
pwm.setPWM(7, 0, 160);
delay(50);
pwm.setPWM(5, 0, 280);
pwm.setPWM(6, 0, 490);
delay(50);
pwm.setPWM(1, 0, 190);
pwm.setPWM(2, 0, 180);
delay(50);
pwm.setPWM(5, 0, 120);
pwm.setPWM(6, 0, 620);
delay(500);
pwm.setPWM(4, 0, 450);
pwm.setPWM(5, 0, 120);
pwm.setPWM(6, 0, 470);
pwm.setPWM(7, 0, 160);
delay(400);
pwm.setPWM(4, 0, 600);
pwm.setPWM(5, 0, 270);
pwm.setPWM(6, 0, 620);
pwm.setPWM(7, 0, 310);
delay(400);
pwm.setPWM(4, 0, 450);
pwm.setPWM(5, 0, 120);
pwm.setPWM(6, 0, 470);
pwm.setPWM(7, 0, 160);
delay(400);
pwm.setPWM(4, 0, 600);
pwm.setPWM(5, 0, 270);
pwm.setPWM(6, 0, 620);
pwm.setPWM(7, 0, 310);
delay(400);
pwm.setPWM(4, 0, 450);
pwm.setPWM(5, 0, 120);
pwm.setPWM(6, 0, 470);
pwm.setPWM(7, 0, 160);
delay(400);
pwm.setPWM(4, 0, 600);
pwm.setPWM(5, 0, 270);
pwm.setPWM(6, 0, 620);
pwm.setPWM(7, 0, 310);
delay(400);
pwm.setPWM(4, 0, 450);
pwm.setPWM(5, 0, 120);
pwm.setPWM(6, 0, 470);
pwm.setPWM(7, 0, 160);
delay(400);
pwm.setPWM(4, 0, 600);
pwm.setPWM(5, 0, 270);
pwm.setPWM(6, 0, 620);
pwm.setPWM(7, 0, 310);
delay(400);
pwm.setPWM(4, 0, 450);
pwm.setPWM(5, 0, 120);
pwm.setPWM(6, 0, 470);
pwm.setPWM(7, 0, 160);
delay(400);
pwm.setPWM(4, 0, 600);
pwm.setPWM(5, 0, 270);
pwm.setPWM(6, 0, 620);
pwm.setPWM(7, 0, 310);
delay(400);
pwm.setPWM(4, 0, 450);
pwm.setPWM(5, 0, 120);
pwm.setPWM(6, 0, 470);
pwm.setPWM(7, 0, 160);
delay(400);
pwm.setPWM(4, 0, 600);
pwm.setPWM(5, 0, 270);
pwm.setPWM(6, 0, 620);
pwm.setPWM(7, 0, 310);
delay(400);
}