Stepper Motor: Different Speeds with Acceleretion Deceleretion

Hi!

I'm trying to give a Stepper instructions to run at certain speeds and Accelerate or Decelerate.

I've tried different things but all I can get is to accelerate to a certain distance and then it stops.

I'm using a NEMA 17 with a DRV8825 Driver and connecting to wifi though a
ESP8266.

My code so far got the webserver that gives the instructions correctly to the board so I get the right speeds.

I just have to implement a way to accelerate or decelerate between them.

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <AccelStepper.h>

const char* ssid = "Stepper1";       
const char* password = "123456789"; 

ESP8266WebServer server(80);

#define dirPin 12
#define stepPin 14
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

int STPstatus = 1;

void setup() {
  Serial.begin(9600);
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(20);
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("Access Point IP:");
  Serial.println(myIP);
  
  server.on("/", handle_OnConnect);
  server.on("/1", handle_1);
  server.on("/2", handle_2);
  server.on("/3", handle_3);
  server.on("/4", handle_4);
  server.on("/5", handle_5);
  server.on("/6", handle_6);
  server.on("/7", handle_7);
  server.on("/8", handle_8);
  server.on("/0", handle_0);  
  server.onNotFound(handle_NotFound);
  
  server.begin();
  //Serial.println("HTTP Server Started");
}

void loop() {
  server.handleClient();
  
  if(STPstatus==1)
    {stepper.setSpeed(25);
    }
    else if(STPstatus==2)
      {stepper.setSpeed(50);}
      else if (STPstatus==3)
        {stepper.setSpeed(100);}
        else if (STPstatus==4)
          {stepper.setSpeed(150);}
          else if (STPstatus==5)
            {stepper.setSpeed(200);}
            else if (STPstatus==6)
              {stepper.setSpeed(300);}
              else if (STPstatus==7)
                {stepper.setSpeed(400);}
                else if (STPstatus==8)
                  {stepper.setSpeed(500);}
                  else if (STPstatus==0)
                    {stepper.setSpeed(0);}
  stepper.run();
}

// from here it's just the Web server

void handle_OnConnect() {
  STPstatus = 0;
  Serial.println("Motor: 0");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_1() {
  STPstatus = 1;
  Serial.println("Motor: 25");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_2() {
  STPstatus = 2;
  Serial.println("Motor: 50");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_3() {
  STPstatus = 3;
  Serial.println("Motor: 100");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_4() {
  STPstatus = 4;
  Serial.println("Motor: 150");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_5() {
  STPstatus = 5;
  Serial.println("Motor: 200");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_6() {
  STPstatus = 6;
  Serial.println("Motor: 300");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_7() {
  STPstatus = 7;
  Serial.println("Motor: 400");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_8() {
  STPstatus = 8;
  Serial.println("Motor: 500");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_0() {
  STPstatus = 0;
  Serial.println("Motor: 0");
  server.send(200, "text/html", updateWebpage(STPstatus)); 
}

void handle_NotFound(){
  server.send(404, "text/plain", "Not found");
}

String updateWebpage(uint8_t STPstatus){
  String ptr = "<!DOCTYPE html> <html>\n";
  ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  ptr +="<title>Motor Giratorio</title>\n";
  ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
  ptr +=".button1 {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button2 {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button3 {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button4 {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button5 {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button6 {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button7 {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button8 {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button0 {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
  ptr +=".button-on {background-color: #3498db;}\n";
  ptr +=".button-on:active {background-color: #3498db;}\n";
  ptr +=".button-off {background-color: #34495e;}\n";
  ptr +=".button-off:active {background-color: #2c3e50;}\n";
  ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
  ptr +="</style>\n";
  ptr +="</head>\n";
  ptr +="<body>\n";
  ptr +="<h1>ESP8266 Web Server</h1>\n";
  ptr +="<h3>Using Access Point(AP) Mode</h3>\n";
  
   if(STPstatus==1)
    {ptr +="<p>Motor: 25</p><a class=\"button1 button-off\" href=\"/1\">1</a>\n";}
      else {ptr +="<p>Motor: 25</p><a class=\"button1 button-on\" href=\"/1\">1</a>\n";}

   if(STPstatus==2)
    {ptr +="<p>Motor: 50</p><a class=\"button2 button-off\" href=\"/2\">2</a>\n";}
      else {ptr +="<p>Motor: 50</p><a class=\"button2 button-on\" href=\"/2\">2</a>\n";}
      
   if (STPstatus==3)
    {ptr +="<p>Motor: 100</p><a class=\"button3 button-off\" href=\"/3\">3</a>\n";}
      else {ptr +="<p>Motor: 100</p><a class=\"button3 button-on\" href=\"/3\">3</a>\n";}
        
   if (STPstatus==4)
    {ptr +="<p>Motor: 150</p><a class=\"button4 button-off\" href=\"/4\">4</a>\n";;}
      else {ptr +="<p>Motor: 150</p><a class=\"button4 button-on\" href=\"/4\">4</a>\n";}
    
   if (STPstatus==5)
    {ptr +="<p>Motor: 200</p><a class=\"button5 button-off\" href=\"/5\">5</a>\n";}
      else {ptr +="<p>Motor: 200</p><a class=\"button5 button-on\" href=\"/5\">5</a>\n";}
      
   if (STPstatus==6)
    {ptr +="<p>Motor: 300</p><a class=\"button6 button-off\" href=\"/6\">6</a>\n";}
      else {ptr +="<p>Motor: 300</p><a class=\"button6 button-on\" href=\"/6\">6</a>\n";}
    
   if (STPstatus==7)
    {ptr +="<p>Motor: 400</p><a class=\"button7 button-off\" href=\"/7\">7</a>\n";}
      else {ptr +="<p>Motor: 400</p><a class=\"button7 button-on\" href=\"/7\">7</a>\n";}
      
   if (STPstatus==8)
    {ptr +="<p>Motor: 500</p><a class=\"button8 button-off\" href=\"/8\">8</a>\n";}
      else {ptr +="<p>Motor: 500</p><a class=\"button8 button-on\" href=\"/8\">8</a>\n";}
      
   if (STPstatus==0)
    {ptr +="<p>Motor: 0</p><a class=\"button0 button-off\" href=\"/0\">0</a>\n";}
      else {ptr +="<p>Motor: 0</p><a class=\"button0 button-on\" href=\"/0\">0</a>\n";}


  ptr +="</body>\n";
  ptr +="</html>\n";
  return ptr;

Thank you so much, I hope someone can help me.

I don't see in your code, that you ever tell Accelstepper where to go ( .move() or .moveTo() ). .run() will not create any steps if there is no targetposition away from the actual position.
And if you use the .run() method to have acceleration and deceleration you must not use .setSpeed() to change the speed, but .setMaxSpeed().

2 Likes

Yeah, it's not there because it broke it and was just looking for this kind of guidance, thank you so much, I'll try this today.

Hi, I've been trying to do this to no avail, I can make it work with .run() and .setMaxSpeed() only if i put it in the initial pin, and if I push a button to take me to another speed it stops working.

When you make any changes to your code, please post the new version so that we can keep up.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.