Stepper motor pausing with LCD update

Hello all,

Humbly requesting some guidance on my 3d print filament recycler project. What I have is a geared stepper motor turning an auger inside a heated tube, thermistors to read the temperature in 3 locations, and 28BYJ stepper to turn the takeup reel. The tube is heated with some of those polyimide heat strips and a PWM controller.

This all basically works. The issue is that whenever the lcdUpdate function gets called, the steppers (and everything I guess) pauses for a split second, which translates to a slight jerk in the rotation.

I know lcd.print takes time and I suspect the analog2temp readings are costly as well. Im also unclear whether I should be using run or runSpeed for the steppers, or if it matters here.

I am not a good programmer so please forgive me if I am missing something obvious. Any and all input is appreciated.

Thank you!

#include <LiquidCrystal_I2C.h>
#include <AccelStepper.h>
#include <AVR_PWM.h>
#include <thermistor.h>

AccelStepper reelStepper(AccelStepper::FULL4WIRE, 9, 11, 10, 12);           //Reel stepper knob & setup
const int reelPin = A3;
int reelSpeed = 0;
int reelKnob = 0;
int reelDisp;

AccelStepper augerStepper(AccelStepper::DRIVER, 14, 15);                    //Auger stepper knob & setup
const int augerPin = A4;
int augerSpeed = 0;
int augerKnob = 0;
int augerDisp;

const int tempPin = A5;                                                     //Temp knob & PWM setup
const int heaterPin = 8;            
AVR_PWM* heater;
const int frequency = 20000;
int dutyCycle;
int tempKnob = 0;
int tempSet;


LiquidCrystal_I2C lcd(0x27,20,4);                                           //LCD consts


thermistor therm3(A0,0);                                                    //Thermistor consts
thermistor therm2(A1,0);
thermistor therm1(A2,0);

int temp3;
int temp2;
int temp1;


unsigned long previousMillis = 0;                                           // store last time LED was updated
const long interval = 1000;                                                 // interval at which to update LCD

void setup() {
  
  lcd.init();
  lcd.backlight();                                                          //start LCD
  lcd.print("Set Temp:");
  lcd.setCursor(0,1);
  lcd.print("Temp1:");
  lcd.setCursor(0,2);
  lcd.print("Temp2:");
  lcd.setCursor(0,3);
  lcd.print("Temp3:");
  lcd.setCursor(11,2);
  lcd.print("Auger:");
  lcd.setCursor(11,3);
  lcd.print("Reel:");

  reelStepper.setMaxSpeed(1000);
  reelStepper.setAcceleration(200);
  reelStepper.setSpeed(0);

  augerStepper.setMaxSpeed(1000);
  augerStepper.setAcceleration(1000);
  augerStepper.setSpeed(0);

  heater = new AVR_PWM(heaterPin, 20000, 0);

  Serial.begin(115200);
}


void loop() {

unsigned long currentMillis = millis();

tempControl();
augerControl();
reelControl();
//updateLCD();

if (currentMillis - previousMillis >= interval) {
    updateLCD();
    previousMillis = currentMillis;
    }
}


void updateLCD(){                                                           

  if (augerDisp > 0) {                                                        //Auger display
    lcd.setCursor(17, 2);
    lcd.print(augerDisp);
    lcd.print(" ");
    }
    else {
      lcd.setCursor(17, 2);
      lcd.print("off");
      lcd.print("");
    }
                                                            
  if (reelDisp > 0) {                                                         //Reel display
    lcd.setCursor(17, 3);
    lcd.print(reelDisp);
    lcd.print(" ");
    }
    else {
      lcd.setCursor(17, 3);
      lcd.print("off");
      lcd.print("");
    }


  if (tempSet > 150) {                                                        //TempSet dsiplay
    lcd.setCursor(9, 0);
    lcd.print(tempSet);
    lcd.print(" ");
  }
    else {
      lcd.setCursor(9, 0);
      lcd.print("off");
    }

  temp3 = therm3.analog2temp();                                               //read temp3
  temp2 = therm2.analog2temp();                                               //read temp2
  temp1 = therm1.analog2temp();                                               //read temp1

  //lcd.setCursor(6, 1);                                                      //Temp1 lcd update                                   
  //lcd.print(temp1);
  //lcd.print(" ");
                                              
  lcd.setCursor(6, 2);                                                        //Temp2 lcd update                              
  lcd.print(temp2);
  lcd.print(" ");
                                              
  lcd.setCursor(6, 3);                                                        //Temp3 lcd update                            
  lcd.print(temp3);
  lcd.print(" ");
}


void tempControl(){

  tempKnob = analogRead(tempPin);
  tempSet = map(tempKnob, 0, 1024, 149, 251);
  dutyCycle = map(tempKnob, 0 ,1024, 0, 100);

  if (tempSet > 150) {
    heater->setPWM(heaterPin, frequency, dutyCycle);
  }
    else {
      heater->setPWM(heaterPin, frequency, 0);
  }
}


void reelControl(){

  reelKnob = analogRead(reelPin);
  reelSpeed = map(reelKnob, 0, 1024, 0, 240);
  reelDisp = map(reelKnob, 0, 1024, 0, 99);

  if (reelKnob > 50) {
    reelStepper.setSpeed(reelSpeed);
    reelStepper.run();
  }
    else {
      reelStepper.stop();
  }
}


void augerControl(){

  augerKnob = analogRead(augerPin);
  augerSpeed = map(augerKnob, 0, 1024, 0, 1000);
  augerDisp = map(augerKnob, 0, 1024, 0, 99);

  if (augerKnob > 50) {
    augerStepper.setSpeed(augerSpeed);
    augerStepper.run();
  }
    else {
      augerStepper.stop();
      }
}

Which "termistor" library is that ?

The I2C bus is slow and the the function AccelStepper::run() decides if a step is taken or not. Any delay or a slow display update will cause a hiccup in the signal for the stepper motor.

You can replace the display with a display with a normal interface (with the many wires), not a I2C bus. Or use a stepper library that runs in a interrupt. Or control the stepper motors with a module.
Perhaps your can try to call the .run() more often, change the I2C bus from 100kHz to 400kHz clock speed, move the .analog2temp() to another location.

Did you know that the functions Wire.requestFrom() and Wire.endTransmission() are blocking ? They wait until the complete I2C session on the bus has finished.
I measured your sketch, and the I2C part takes about 40ms. During that 40ms, no steps are taken for the stepper motors.

ThermistorLibrary

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