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?