Accelstepper speeds

I have tried to increase the stepper speed in my program but I can't seem to get it to run any faster no matter what I set too. With the one exemption when my program is in its
while (digitalRead(reset) == LOW)

loop the motor runs as fast as i program it.

 stepper.setMaxSpeed(5000); // Set the maximum steps per second:  
  stepper.setAcceleration(10000); // Set the maximum acceleration in steps per second^2:
#include <Adafruit_DS3502.h>                  
Adafruit_DS3502 ds3502 = Adafruit_DS3502();   


#include <AccelStepper.h>
#define MotorInterfaceType 1
byte directionPin = 2;      //New Stepper control
byte stepPin = 3;
byte stepstop  = 4;
AccelStepper stepper = AccelStepper(MotorInterfaceType, 3, 2); // Check for free pins

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenStepsup = 1; // milliseconds
unsigned long millisBetweenStepsdown = 2; // milliseconds
int stepdelay              =10; //testing
int clutchadvance          =10; //testing
int brakespeed             =5; //testing



#include "MultiMap.h"           //Multimap
int mappedValue;                //
int in[] = {0 , 31, 63, 95, 127};   //
int out[] = {0, 95, 115, 125, 127};  //



#define magnetDetected       HIGH
#define noMagnet             LOW

#define weAreTiming          true
#define weAreNotTiming       false

#define notfull              false
#define full                 true

#define resetting           true
#define tensioning          false

#define brakeon             true
#define brakeoff            false

#define clutchon            true
#define clutchoff           false

const byte BRAKE_IN_PIN    = A0;
const byte PRESSURE        = A1;  // Pot on Analog Pin 1
const byte POTH            = A2;  // Pot on Analog Pin 2
const byte CLUTCH_IN_PIN    =A3; 
const byte MOTOROFF       =7;

bool timerRunningFlag      = weAreNotTiming;
bool fullthrottle          = notfull;
bool resetflag             = tensioning;
bool brakeflag             = brakeoff;
bool clutchflag            = clutchoff;

const byte sensor          = 5;
const byte heartbeatLED    = 13;
int reset                  = 12;

byte lastWheelState        = noMagnet;

int digitalPot                ;
int brake_value           = 0;
int clutch_value          = 0;
int clutch_temp           = 0;
int lowspeed               = 0;   // Low speed set
int highspeed              = 0;   // High Speed set
int lowrpm                 = 0;   // Low RPM kick in
int highrpm                = 0;   // High RPM kick in
int highValue              = 0;
int stepsLeft              = 0;
int force                  = 0;
int stepmath               = 0;
int tension                = 0;
int stepstaken             = 0;
int fullsqueeze            = 0;

int distanceToGo           = 0;   //testing here <--------------------


int totaltension           =7000;

int lowValue         = 800; //low speed setting

const int highLow          = 600; //reading 0
const int highHigh         = 200; //reading 1023

//timing stuff
unsigned long startTime;
unsigned long endTime;
unsigned long duration;
unsigned long timerMillis;
unsigned long heartbeatMillis;
unsigned long wheelMillis;

//*********************************************************************
void setup()
{
  //analogReference(A0);
  ds3502.setWiper(0);
  pinMode (sensor, INPUT_PULLUP);
  pinMode(reset, INPUT_PULLUP);
  pinMode (heartbeatLED, OUTPUT);
  pinMode(MOTOROFF, OUTPUT);
  Serial.begin(115200);
  while (!Serial)
  {
    delay(1);
  }
  Serial.println("Adafruit DS3502 Test");
  if (!ds3502.begin())                             // <-----<<<<<
  { // <-----<<<<<
    Serial.println("Couldn't find DS3502 chip");   // <-----<<<<<
    while (1);                                     // <-----<<<<<
  }                                                // <-----<<<<<
  Serial.println("Found DS3502 chip");
  fullthrottle = notfull; 


  stepper.setMaxSpeed(5000); // Set the maximum steps per second:  
  stepper.setAcceleration(10000); // Set the maximum acceleration in steps per second^2:

} 


//*********************************************************************
void loop()

{
  digitalRead(reset);
  while (digitalRead(reset) == LOW)
    {
      stepper.moveTo(0); 
      stepper.runToNewPosition(0);          
     if (stepper.distanceToGo () == 0) //<-------------------------testing here
      {
        digitalWrite(MOTOROFF, HIGH);
       digitalWrite(heartbeatLED, LOW);
      };
      clutch_value = analogRead(CLUTCH_IN_PIN);
      int temp = map( clutch_value, 1027, 0, 127, 0); 
      temp = constrain(temp, 0, 127);   //stay within the 0-127 range
      digitalPot = temp;
      ds3502.setWiper(digitalPot);

      
      if (resetflag == tensioning) { 
        Serial.println("reset");
        resetflag = resetting;
        }
        digitalRead(reset);       
        }
    
      resetflag = tensioning;
      digitalWrite(MOTOROFF, LOW);
  


  timer();
  ds3502.setWiper(digitalPot);
  stepper.moveTo(tension);
  stepper.run();
  brake_value = analogRead(BRAKE_IN_PIN);
  clutch_value = analogRead(CLUTCH_IN_PIN);
  digitalRead(reset);
  
  curMillis = millis();
  

   while (brake_value < 500) {  // used to be 920
    ds3502.setWiper(127);
    if (brakeflag == brakeoff) {
    Serial.print("Brake on ");
    Serial.println(brake_value);
    brakeflag = brakeon;
    }
    curMillis = millis();
    brake_value = analogRead(BRAKE_IN_PIN);
    }

  brakeflag = brakeoff;
 
  while (clutch_value < 900 && brake_value > 920) {
    clutch_temp = map(clutch_value, 800, 80, digitalPot, 127);
    clutch_temp = constrain(clutch_temp, 0, 127);
    ds3502.setWiper(clutch_temp);
    if (clutchflag == clutchoff) {
    Serial.print("Clutch on ");
    Serial.print(clutch_value);
    Serial.print("  Digitalpot value:  ");
    Serial.println(clutch_temp);
    clutchflag = clutchon;
    }
    curMillis = millis();
    brake_value = analogRead(BRAKE_IN_PIN);
    clutch_value = analogRead(CLUTCH_IN_PIN);
    }

 clutchflag = clutchoff;
    

  
    

} //END of loop()

void timer()
{
  
  if(millis() - heartbeatMillis >= 500)
  {    heartbeatMillis = millis();
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //***********************
  //time to check the wheel sensor ?
  if (millis() - wheelMillis >= 10)
  {
    //restart the TIMER
    wheelMillis = millis();
    checkWheelSensor();
  }

  if (timerRunningFlag == weAreTiming && millis() - timerMillis >= lowValue)    //timeout
   {
    timerRunningFlag = weAreNotTiming;
    duration = lowValue;
    printDigitalPot();
    
    //timerRunningFlag = weAreTiming;       //testing

      //start this TIMER                      Causes an overreaction
    //  startTime = millis();                 Maybe base force math over an average

      //start this TIMER
    //  timerMillis = millis();             //too here
  }
}


//*********************************************************************
void checkWheelSensor()
{
  byte thisState = digitalRead(sensor);

  //***********************
  //was there a change in state ?
  if (lastWheelState != thisState)
  {
    //update to the new state
    lastWheelState = thisState;

    //***********************
    //if we are 'not' timing has the sensor detected the magnet ?
    if (timerRunningFlag == weAreNotTiming && thisState == magnetDetected)
    {
      //enable timing
      timerRunningFlag = weAreTiming;

      //start this TIMER
      startTime = millis();

      //start this TIMER
      timerMillis = millis();
    }

    //***********************
    if (timerRunningFlag == weAreTiming && thisState == noMagnet)
    {
      //disable the timing
      timerRunningFlag = weAreNotTiming;

      endTime = millis();

      duration = endTime - startTime;

      printDigitalPot();
    }

  } //END of  if(lastWheelState != thisState)

} //END of checkWheelSensor()


//*********************************************************************
void printDigitalPot()
{ 
  highspeed = analogRead(POTH); //0 to 1023
  highValue = map(highspeed, 0, 1000, highLow, highHigh);
  lowValue = (highValue + 400);
  
  int temp = map( duration, highValue, lowValue, 0, 127); 
  temp = constrain(temp, 0, 127);   //stay within the 0-127 range
  
  
  
  digitalPot=multiMap(temp, in, out, 5);
  

  force = analogRead(PRESSURE);
  force = map(force,0,1023,0,totaltension); 
  stepmath = map(temp,0,127,force,0);  //temp might become "digitalPot" again

  if (temp > 0 && clutchflag == clutchoff) //less than full throttle
    {
    fullthrottle = notfull;
    tension = stepmath;
    }

  if (fullthrottle == full && clutchflag == clutchoff)
    {
      fullsqueeze = 10; //stepmath/300; //300 may need to change depending on required full throttle pressure
      tension = tension+fullsqueeze;
      }

    if (fullthrottle == notfull && temp == 0 && clutchflag == clutchoff)
      {fullthrottle = full;
      tension = stepmath;

       }

 
   
   
// Serial.print("B: ");
// Serial.print(brake_value);
// Serial.print("C: ");
// Serial.print(clutch_value);
// Serial.print("  ");
// Serial.print(lowValue);
// Serial.print("  ");
// Serial.print("Timer spread ");
// Serial.print(highValue);
// Serial.print ("  Time in millis = ");
// Serial.print (duration);
// Serial.print("  ");
// Serial.print(temp);
// Serial.print("   Digital pot set to: ");
// Serial.print(digitalPot);
// Serial.print("\t");
// Serial.print("Savage level: ");
// Serial.print(force);
// Serial.print("  ");
// Serial.print(stepmath);
// Serial.print(" -Stepmath   Stepsdue- ");
// Serial.print(stepstaken);
// Serial.print("   Needed tension: ");
// Serial.println(tension);
// Serial.println("");
  
 

} //END of  printDigitalPot()

Assuming you’re using a 16MHz processor, you may like to check the AccelStepper documentation.

The ability to process all the required things, the total step rate is limited to around 4000 across all connected motors.

You can look for a faster processor, or a dedicated stepper controller - or if suitable, change to a DC motor with optional feedback.

If you can do without speed ramping, you could pump the steppers without a driver.
(i’ve run up to >30KHz by manually driving the step & dir pins on an 8-bit 16MHz AVR).
You can implement simple speed ramping in your own code

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