Stepper motor moves too slow with other code running

I've got a arduino nano running a LCD screen a 3d printer hot end and a stepper motor the other two work fine but the stepper motor runs supper slow and not smooth.
As I delete different parts of the code the motor speeds up and runs better until there is only the code for the motor left and then it works perfectly. No delays are being used in the code

#include <LiquidCrystal.h>
#include <AccelStepper.h>
#include <PID_v1.h>

#define STEP A4 //A4
#define DIR A5//A5
// Define the stepper motor and the pins that is connected to
AccelStepper stepper(1, 7, 6); // (Type of driver: with 2 pins, STEP, DIR)

double PID_setpoint, PID_input, PID_output;
PID heater_pid(&PID_input, &PID_output, &PID_setpoint, 10, 0.3, 0, DIRECT);//PID(&Input, &Output, &Setpoint, Kp, Ki, Kd, Direction)
int MOSFET_gate = 11;

#define THERMISTORPIN A0  //Thermistor settings       
  // resistance at 25 degrees C
  #define THERMISTORNOMINAL 100000      
  // temp. for nominal resistance (almost always 25 C)
  #define TEMPERATURENOMINAL 25   
  // how many samples to take and average, more takes longer
  // but is more 'smooth'
  #define NUMSAMPLES 5
  // The beta coefficient of the thermistor (usually 3000-4000)
  #define BCOEFFICIENT 3950
  // the value of the 'other' resistor
  #define SERIESRESISTOR 100000  
  int samples[NUMSAMPLES];  
//

float Cur_Temp();


String   button = "";
String   TempType = "Extruding";

int ERate = 90000;

int ExtrusionTemp = 200;
int JoinTemp =255;

int CurrentTemp = 0;
int DesiredTemp = ExtrusionTemp;

float Cur_Temp();
const unsigned long animation_delay = 300;
unsigned long previous_millis_animation = 0;  

byte ClearChar[] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
  };
byte full_left[] = {
  B00000,
  B11100,
  B11100,
  B00100,
  B00100,
  B01110,
  B01110,
  B01110
  };
byte half_left[] = {
  B00000,
  B01100,
  B01100,
  B00100,
  B00100,
  B01110,
  B01110,
  B01110
  };
byte middle[] = {
  B00000,
  B00100,
  B00100,
  B00100,
  B00100,
  B01110,
  B01110,
  B01110
  };
byte half_right[] = {
  B00000,
  B00110,
  B00110,
  B00100,
  B00100,
  B01110,
  B01110,
  B01110
  };
byte full_right[] = {
  B00000,
  B00111,
  B00111,
  B00100,
  B00100,
  B01110,
  B01110,
  B01110
  };

byte ThemometerChar[] = {
  B00100,
  B01010,
  B01010,
  B01010,
  B01110,
  B11111,
  B11111,
  B01110
  };



int Value = 0;

const int rs = 2, en = 3, d4 = 6, d5 = 7, d6 = 8, d7 = 9;//LCD screen pinouts
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  lcd.begin(20, 4);
  lcd.createChar(7, full_left); //{creates all characters
  lcd.createChar(3, half_left);//
  lcd.createChar(4, middle);//
  lcd.createChar(5, half_right);//
  lcd.createChar(6, full_right);//
  lcd.createChar(10, ClearChar);//
  lcd.createChar(1, ThemometerChar);//}
  lcd.home();
  lcd.write(1); 

  Serial.begin(9600);

  analogReference(EXTERNAL);//used for the thermistor
  stepper.setMaxSpeed(100000);

  pinMode(MOSFET_gate, OUTPUT);//intitialises mosfet gate pin 
  heater_pid.SetMode(AUTOMATIC);//turns on PID controler
  PID_input = CurrentTemp;
  PID_setpoint = DesiredTemp;

}

void loop() {
  lcd.setCursor(1,0);
  String   line_temp = String(CurrentTemp)  +  "/" + String(DesiredTemp) + (char)223 +" " +TempType;//creates first line of temperature and type of temp
  lcd.print(line_temp);

  lcd.setCursor(1,2);
  String   line_rate = "Rate:" + String(ERate);//creates speed of motor
  lcd.print(line_rate);

  Value = analogRead(A3);//button controll
    if (Value > 100 && Value < 170){ //joining temperature
      button = "BACK";
      DesiredTemp = JoinTemp;
      lcd.setCursor(14,0);//clears extruding off of screen
      lcd.print("    ");
      TempType = "Joining";
      }
    else if ((Value > 220) && (Value < 300)){//extruding temperature
      button = "DOWN";
      DesiredTemp = ExtrusionTemp;
      TempType = "Extruding";
      }  
    else if ((Value > 420) && (Value < 500)){//extrusion rate -
      button = "MENUE";
      ERate -= 1;
      }
   
    else if ((Value > 840) && (Value < 1000)){//extrusion rate +
      button = "UP";
      ERate = ERate + 1;

      }
    else{
      button = " ";
    }
    Serial.println(Value);
    Serial.println(button);

 
  //
  stepper.setSpeed(ERate);
  stepper.runSpeed();
  CurrentTemp = Cur_Temp();

  Serial.println(CurrentTemp);
  Serial.println(ERate);  

  PID_input = CurrentTemp;//set PID input to the temperature of the thermistor
  PID_setpoint = DesiredTemp;
  heater_pid.Compute();
  Serial.println(PID_output);
  analogWrite(MOSFET_gate, PID_output);

  unsigned long current_millis = millis();//stepper animation using millis

    if (current_millis - previous_millis_animation >= (animation_delay*1)){ 
      lcd.setCursor(-1,2);//stepper animation
      lcd.write(10);
    }
    if (current_millis - previous_millis_animation >= (animation_delay*2)){    
      lcd.write(7);//full left
      lcd.setCursor(-1,2);
      lcd.write(10);
    }
      if (current_millis - previous_millis_animation >= (animation_delay*3)){    
      lcd.write(3);//half left
      lcd.setCursor(-1,2);
      lcd.write(10);
    }
    if (current_millis - previous_millis_animation >= (animation_delay*4)){    
      lcd.write(4);//midle
      lcd.setCursor(-1,2);
      lcd.write(10);
    }  
    if (current_millis - previous_millis_animation >= (animation_delay*5)){    
      lcd.write(5);//half right
      lcd.setCursor(-1,2);
      lcd.write(10);
    }    
    if (current_millis - previous_millis_animation >= (animation_delay*6)){    
      lcd.write(6);//full right
      lcd.setCursor(-1,2);
      lcd.write(10);
    }   
    if (current_millis - previous_millis_animation >= (animation_delay*7)){    
      lcd.write(5);//half right
      lcd.setCursor(-1,2);
      lcd.write(10);
    }  
    if (current_millis - previous_millis_animation >= (animation_delay*8)){    
      lcd.write(4);//midle
      lcd.setCursor(-1,2);
      lcd.write(10);
    }
    if (current_millis - previous_millis_animation >= (animation_delay*9)){    
      lcd.write(3);//half left
      previous_millis_animation = current_millis;
    } 
}






anything in the code that might affect a stepper motor?

Yup.

  1. Analog read is slow. That's a combo LCD and button display, right, with all the buttons being read through manipulating the analog value read on a single pin.
  2. String manipulations are slow*, particularly as your building multiple Strings for display during every pass of loop(). This will also give your Nano fits eventually, as String is notorious for memory corruption when heavily utilized.
  3. Writing to the LCD on every pass of loop() slows you down. Particularly, clearing and rewriting. Write new data over old data when possible.
  4. Serial printing can slow you down a lot, because you're sending a lot of output at a very low baud rate. In this case, my suspicion is this is a problem for you. Start by pushing your baud rate up to 115200(don't forget to change the IDE setting too), and see what effect that has.
    That's a start. Can't fix the AnalogRead, unfortunately, but much of the rest can be mitigated.
1 Like

If you don't have a very very fast Board, this speed is far from beeing realistc. No chance with a classic Nano
You call runSpeed() only once in your loop(), and from what I see your loop time is fairly slow - there is a lot to do - as @camsysca already pointed out.

changed the baud rate it did speed up the motor but still not quick enough and vibrates alot
How do I mitigate the other problems?

What speed do you want to achive? And be aware for high speeds you need to ramp up and down the stepper. Steppers are not able to runs fast from standstill immediately.

And as already mentioned, you will never reach the speed that is set in your sketch with a classic Nano.

need the motor to spin at 31.8RPM preferably with micro stepping

31.8 RPM seems not too fast, so why these really big step/sec in your sketch?
Is it a 200 step/rev stepper ? E.g. with 1/16 Microstepping this is 3200 steps/rev. 31.8 rpm means 0.53 rev / sec. So you need only 1696 steps / sec.
You could try the MoToStepper class of my MobaTools library. This class creates the steps via timer IRQ in the background. So they are even created during your other tasks in loop(). The library can be installed via the library manager. There is a documentation and examples.
'Out of the box' in can create up to 2500 steps/sec on a classic Nano.

1 Like

+1 for the MobaTools stepper library. I found it much easier to learn and use than Accelstepper and I used AccelStepper for years.

1 Like

Moba tool works perfectly thank you for the help

1 Like

Glad I could help you. :slightly_smiling_face:

@setonic Is the topic solved? If so, please flag the topic as such, and give @MicroBahner the credit!

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