help with accelstepper

So I have the following code with which I am controlling a stepper (with a dvr8825 driver) and an led (with a pwm operated led driver):

const int M0 = 22; 
const int M1 = 24;
const int M2 = 26;

const int stepPin = 28; 
const int dirPin = 30; 

int ledcontrol = 9;

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);

void setup() {
   pinMode(M0,OUTPUT);
   pinMode(M1,OUTPUT);
   pinMode(M2,OUTPUT);    

   digitalWrite(M0,HIGH); 
   digitalWrite(M1,HIGH); 
   digitalWrite(M2,HIGH);   
   
   stepper.setMaxSpeed(10000);
   stepper.setSpeed(100);	
}

void loop() {  
    analogWrite(ledcontrol, 200);
    stepper.runSpeed();
}

my idea is to have the stepper running at speed = 100 for 2000 miliseconds
then accelerate it to speed = 1000 for 2000 miliseconds
then deaccelerate it back to speed 100

how can I achieve that?

I have another example where I use acceleration, but I dont really know how to apply all of this to achieve my goal…

const int M10 = 22;
const int M11 = 24;
const int M12 = 26;

const int stepPin1 = 28;
const int dirPin1 = 30;

int velocidad = 20;

#include <AccelStepper.h>

AccelStepper stepper1(AccelStepper::DRIVER, stepPin1, dirPin1);

void setup() {
    pinMode(M10,OUTPUT);
    pinMode(M11,OUTPUT);
    pinMode(M12,OUTPUT);

    digitalWrite(M10,LOW);
    digitalWrite(M11,LOW);  
    digitalWrite(M12,LOW);

    stepper1.setMaxSpeed(velocidad);
    stepper1.setAcceleration(5);

    stepper1.moveTo(__LONG_MAX__ / 2);
}

void loop() {  
   stepper1.run();
}

thanks in advance!

loop() { set speed 100; run stepper(); delay(2000);

set speed 1000; run stepper(); delay(2000);

}

It doesnt seem to be so easy. It doesnt work like this. Are you familiar with the library?

this works:

stepper.setSpeed(500);
stepper.runSpeed();

this doesnt

stepper.setSpeed(500);
stepper.runSpeed();
delay(2000);

the second code kind of triggers one pulse every 2 seconds

I sent You an idea You could use, making the neccessary changes, not some complete code. Pseudo code...

Sorry, I was too quick. Study how to use millis() instead of delay. Like

setup() { change_speed = millis() + 2000; speed = high;//make loop start at low speed }

loop() {

if ( millis() > change_speed )//time to change { if(speed == high )// switch to low speed { change_speed = millis() + 2000;//next change at …. set and run at low speed; speed =low; } else//switch from high to low { change_speed = millis() + 2000;//next change at …. set and run at high speed; speed =high; } //end of changing } }// end of loop

The AccelStepper is not designed for using acceleration between one speed and another. To use the acceleration you need to specify a number of steps and it will move from standstill to standstill accelerating and decelerating smoothly.

It is not difficult to write you own code to do acceleration without using the library. Have a look at the code in this link

...R Stepper Motor Basics Simple Stepper Code

This illustrates a way to make a stepper change speed and acceleration without stopping using the AccelStepper library. I set the motor to run a (very) long distance. The motor will stop when it reaches the distance so adjust distance for the total duration you want. Then, periodically, change the speed and accel using arrarys to store the sequence. I made adjustments for my setup in the pin assignments. Fix as required.

// stepper sequencing example C Goulding
// turn on a stepper and let it run at while changing speed and accel at designated times.
#include <AccelStepper.h>

const byte stepperEnablePin = 8;  // required for my CNC shield
const byte unsigned long numBusySteps = 100000; // get the motor to run reduce for shoter run
// put the sequence of times, speeds ann accels here  
const long times[] = {2000, 5000, 3000, 2000};
const long speeds[] = {0, 100, 1000, 2000};
const long accels[] = {50, 500, 500, 500};
unsigned int timesArraySize = 0; // this is taken care of later

AccelStepper stepper;

void setup()
{
  Serial.begin(115200);
   timesArraySize = sizeof(times) / sizeof(times[0]);
   pinMode(stepperEnablePin, OUTPUT);
   digitalWrite(stepperEnablePin, LOW);
   stepper.setMaxSpeed(100);
   stepper.setAcceleration(200);
}

void loop()
{
   static unsigned long changeTimer = 0;
   static unsigned long changeInterval = 10;
   static unsigned int moveIndex = 0;
   stepper.moveTo(numBusySteps); // tell it to go a looong way.
   if (millis() - changeTimer >= changeInterval)
   {
      changeTimer = millis();
      changeInterval = times[moveIndex];
      stepper.setMaxSpeed(speeds[moveIndex]);
      stepper.setAcceleration(accels[moveIndex]);
      moveIndex++;
      if (moveIndex > timesArraySize)
      {
         moveIndex = 0;
      }
   }
  stepper.run();
}

thanks for the input!

this is heavy coding for me. I will have to take some hours to check this information out.

Did you try the code? Is that action what you were after?

groundFungus:
Did you try the code? Is that action what you were after?

I have just tried it. em… not really. I identify 3 different movements; slow spinning, acceleration and something else. But at the moment of something else, I can only hear the stepper working but it doesnt spin. so no, definitely not what I am after.

As I said before also, this coding is much more than what I can handle. Which is also a very good thing to start improving my coding.

In the meantime, I got a nice code for this matter in the accelstepper google group. I think this is easier for me to understand and also manipulate…

const int M0 = 22;
const int M1 = 24;
const int M2 = 26;

const int stepPin = 28;
const int dirPin = 30;

int ledcontrol = 9;

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);

enum state_t : uint8_t {
  STATE_IDLE = 0,
  STATE_CONST = 1,
  STATE_ACCEL = 2,
  STATE_DECEL = 3,
  STATE_DONE = 4
};

uint32_t time_start = 0L;
uint32_t time_speed_last = 0L;      //time of last speed update in accelerated movements
uint8_t state = STATE_IDLE;

void setup() {
  pinMode(M0, OUTPUT);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);

  digitalWrite(M0, HIGH);
  digitalWrite(M1, HIGH);
  digitalWrite(M2, HIGH);

  stepper.setMaxSpeed(10000);
  stepper.setSpeed(100);
  stepper.setAcceleration(450.0f);

  time_start = millis();
  state = STATE_CONST;
}

void loop() {
  analogWrite(ledcontrol, 200);

  //if 2000ms have passed since the stepper started its constant speed movement
  if ((state == STATE_CONST) && (millis() - time_start > 5000))
  {
    //start accelerated movement
    state = STATE_ACCEL;
    time_speed_last = millis();
  }

  if (state == STATE_ACCEL)
  {
    //update stepper motor speed every 10 ms
    if (millis() - time_speed_last >= 10)
    {
      time_speed_last = millis();
      stepper.setSpeed(stepper.speed() + 4.5f);
    }

    if (stepper.speed() >= 2000.0f)
      state = STATE_DECEL;
  }

  if (state == STATE_DECEL)
  {
    //update stepper motor speed every 10 ms
    if (millis() - time_speed_last >= 10)
    {
      time_speed_last = millis();
      stepper.setSpeed(stepper.speed() - 4.5f);
    }

    if (stepper.speed() <= 100.0f)
    {
      time_start = millis();
      state = STATE_CONST;
    }
  }

  stepper.runSpeed();
}