SOLVED: Accelstepper constant motion problem

i have 2 steppers that drive a drum with some string that has a bucket attached. right now i would like the bucket to be wound quickly up and then when it gets to a certain position slow down to another speed but continue moving up. The way i have it set up now it slows to 0 first and then accererates to the slower speed.

I wont post my whole code as it is 700+ lines long and most of it has nothing to do with this issue
the relevenet parts are below

the loop

//-------------------------------Libraries----------------------------------------// 

#include<Servo.h>
#include<AccelStepper.h>
#include<SoftwareSerial.h> 
#include <OPCSerial.h>
#include <SPI.h>
#include <Ethernet.h>

//-----------------------------Global variables-----------------------------------// 

long currenttime;        // the time when the loop iterates
long    last_Hstop;
long    Hpos              =  0;         //position of the motors in steps
long    Spos              =  0;

int  MaxS           =  485;       //8.2467in / revolution 
int  CreepS         =  48;         
int  fixgudS        =  121;
int  accel          =  300;       //acceleration of motors
int  loadlevel      =  0;         //datum
int  dumplevel      =  1225;      //50.5 inches 
int  fixgudlvl      =  825;       //34 inches
int  creepSlvl      =  1116;      //46 inches
  

AccelStepper motorone (1, motor1steppin, motor1dirpin); 
AccelStepper motortwo (1, motor2steppin, motor2dirpin); 


  motorone.enableOutputs();
  motortwo.enableOutputs();

 


  motorone.setAcceleration(accel);          //acceleration is the same for each side
  motortwo.setAcceleration(accel);  

//--------------------------------------------------------------------------------//
void loop()
{
  
  state=1;
 

  currenttime = millis();

  if (state == 1)           //checks to see if the button is on
  { 
    MoveMotors();                              //if the motors can step. this will make them step
    HydraulicCycle();                   //determins the order to perform actions
    ScrollCycle();
    
  }
}

void PositionMotor(int a) // sets new target position for motor a
{
  if (a=1)
  {
    motorone.setMaxSpeed(Hspeed);
    motorone.moveTo(Hpos);
   
  }
  if(a=2)
  {
    motortwo.setMaxSpeed(Sspeed);
    motortwo.moveTo(Spos);
  
  }
}
//--------------------------------------------------------------------------------//

void MoveMotors()
{
  motorone.run();  //if the motor has steps to move. move motor 1 step
  motortwo.run();      
}

The functions hydraliccycle and scrollcycle are a bunch of if statements similar to this

if (Hstep == 1 && motorone.currentPosition() == fixgudlvl)
  {    //after the skip has finished moving
    Hstep=2;
    Hspeed = fixgudS;               //motor speed becomes fixguide
    Hpos = dumplevel;            //set target position to dump
    PositionMotor(1);

basiaclly it checks if the stepper is in the right position and if the program is on the correct phase. and if these conditions are met a new speed and the next target position are inputted, along with advancing the phase of the program.

I need some way so the stepper will slow down from highspeed to low speed without coming to a stop in the middle

Thanks

Matt

  state=1;


  currenttime = millis();

  if (state == 1)                     //checks to see if the button is on
  {

This makes no sense! Why are you bother to check that state is 1 nanoseconds are assigning it that value?

  if (a=1)

Bzzzt. Wrong!

Can't you just change the maximum speed while the motor is running?

I haven't tried this myself.

...R

PaulS: ```  state=1;

 currenttime = millis();

 if (state == 1)           //checks to see if the button is on  {



This makes no sense! Why are you bother to check that state is 1 nanoseconds are assigning it that value?



 if (a=1) ```

Bzzzt. Wrong!

the end game of the program is to interact with another program called In Touch (by a company called Wonderware) and the "state" variable is to set so the system can be turned on, off or reset from the computer GUI without having to power off anything. i had deleted the prilimary reset code from what i posted as it was irrelivant to the problem. and the state=1 line was put in so i dont need a computer to turn it on. usually it will boot up in the off state and you turn it on via computer GUI

Robin2: Can't you just change the maximum speed while the motor is running?

I haven't tried this myself.

...R

I tried that back whan i was using .distanceToGo in the if statements rather than .currentPosition and i got weird results. it only changed speeds on the way up. on the way down it ignored the speed up command. I will try agian today with the .currentPosition command and see what the results are.

omgnames: I will try agian today with the .currentPosition command and see what the results are.

If you have more questions be sure to post your latest code.

...R

OK. It is accelerating properly now :slight_smile: here is my full code if someone else runs into the same issue. I made a function that can update the speed position or both depending on the input.

Matt

code_v6.ino (11.5 KB)