stepper motor instead - GO TO, not speed

This commit is contained in:
Tyrel Souza 2022-10-28 10:39:18 -04:00
parent 57d0aaa6f4
commit fb216b16fb
2 changed files with 45 additions and 20 deletions

View File

@ -60,7 +60,7 @@ function action(angle, direction) {
clearTimeout(currentTimeout); clearTimeout(currentTimeout);
} }
$.post("192.168.1.232/angle", { $.post("/angle", {
angle: angle, angle: angle,
}); });
$("#currentAngle").text("Current Action: " + direction); $("#currentAngle").text("Current Action: " + direction);

View File

@ -1,10 +1,4 @@
/* /*
Project made and maintained by Kumar Aditya
ESP8266 Servo Controller
Create a ESP8266 Webserver for controlling the real-time position of the servo motor attached to ESP8266.
Change these lines as per yours: Change these lines as per yours:
const char *ssid = "REPLACE_WITH_YOUR_SSID"; // replace with your SSID const char *ssid = "REPLACE_WITH_YOUR_SSID"; // replace with your SSID
const char *password = "REPLACE_WITH_YOUR_PASSWORD"; // replace with your Password const char *password = "REPLACE_WITH_YOUR_PASSWORD"; // replace with your Password
@ -15,12 +9,6 @@
NodeMCU : NodeMCU :
https://amzn.to/397GzNe https://amzn.to/397GzNe
Servo Motor :
https://amzn.to/2SmyFtJ
The source code can be found at:
https://git.io/JfOQv
Download ESPAsyncWebServer library : Download ESPAsyncWebServer library :
https://github.com/me-no-dev/ESPAsyncWebServer/archive/master.zip https://github.com/me-no-dev/ESPAsyncWebServer/archive/master.zip
@ -36,22 +24,34 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <Servo.h> #include <AccelStepper.h>
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
// ULN2003 Motor Driver Pins
#define IN1 5
#define IN2 4
#define IN3 14
#define IN4 12
// initialize the stepper library
AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);
const char *ssid = "TARDIS"; // replace with your SSID const char *ssid = "TARDIS"; // replace with your SSID
const char *password = "EvangelionUnit-01"; // replace with your Password const char *password = "EvangelionUnit-01"; // replace with your Password
const uint8_t servoPin = D1; // replace with servo pin const uint8_t servoPin = D1; // replace with servo pin
/* Create Servo Object */
Servo servo;
// Create Server instance // Create Server instance
AsyncWebServer server(80); AsyncWebServer server(80);
void setup() void setup()
{ {
// Attach Servo, start SPIFFS and Connect to WiFi // Attach Servo, start SPIFFS and Connect to WiFi
Serial.begin(115200); Serial.begin(115200);
servo.attach(servoPin); stepper.setMaxSpeed(2000);
servo.write(160); stepper.setAcceleration(500);
// set target position
stepper.moveTo(0);
if (!SPIFFS.begin()) if (!SPIFFS.begin())
{ {
Serial.println("An Error has occurred while mounting SPIFFS"); Serial.println("An Error has occurred while mounting SPIFFS");
@ -73,13 +73,31 @@ void setup()
Serial.println("Send index.html."); Serial.println("Send index.html.");
request->send(SPIFFS, "/index.html", "text/html"); request->send(SPIFFS, "/index.html", "text/html");
}); });
// Receive Angle from client and process it
server.on("/angle", HTTP_POST, [](AsyncWebServerRequest *request) { /*server.on("/angle", HTTP_POST, [](AsyncWebServerRequest *request) {
String angle = request->arg("angle"); String angle = request->arg("angle");
Serial.println("Current Position: " + angle + "°"); Serial.println("Current Position: " + angle + "°");
servo.write(angle.toInt()); servo.write(angle.toInt());
request->send(200); request->send(200);
});*/
server.on("/stop", HTTP_POST, [](AsyncWebServerRequest *request) {
Serial.println("stopping");
stepper.stop();
request->send(200);
}); });
server.on("/cw", HTTP_POST, [](AsyncWebServerRequest *request) {
Serial.println("cw");
stepper.moveTo(2 * stepsPerRevolution);
request->send(200);
});
server.on("/ccw", HTTP_POST, [](AsyncWebServerRequest *request) {
Serial.println("cw");
stepper.moveTo(- 2 * stepsPerRevolution);
request->send(200);
});
// Send Favicon // Send Favicon
server.serveStatic("/favicon.ico", SPIFFS, "/favicon.ico"); server.serveStatic("/favicon.ico", SPIFFS, "/favicon.ico");
// Begin Server // Begin Server
@ -87,4 +105,11 @@ void setup()
} }
void loop() void loop()
{ {
stepper.run();
} }