Stepper motor programming problem

Hi.
my program worked fine, but when I added routine for sensors reading (ntc 100k) stepper motor started to pause.

VIDEO

My code:

#include <LiquidCrystal.h>
#include <DFR_LCD_Keypad.h>
#include <AccelStepper.h>
#include <NTC_Thermistor.h>
void LCD();
void motor();
void updateLCD();
void senzor();
byte de[8] = {
  B01000,
  B00100,
  B00010,
  B11111,
  B11111,
  B00010,
  B00100,
  B01000
};
byte le[8] = {
  B00010,
  B00100,
  B01000,
  B11111,
  B11111,
  B01000,
  B00100,
  B00010
};
byte gon[8] = {
  B10010,
  B01001,
  B10010,
  B01001,
  B10010,
  B00000,
  B11111,
  B11111
};
byte goff[8] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DFR_LCD_Keypad keypad(A0, &lcd);
AccelStepper mot1(1, 2, 3); // pin 2 = step, pin 3 = direction
int last_key, key;
int nhitrost = 20;
float ntemp = 0;
float temp1 = 0;
int smer = 0;
//const int stepPin = 2;
const int dirPin = 11;
const int ssr = 12;
unsigned long lastLcdUpdate;
unsigned long lastSenzorUpdate;

#define SENSOR1_PIN             A1
#define SENSOR2_PIN             A2
#define SENSOR3_PIN             A3
#define REFERENCE_RESISTANCE   4660
#define NOMINAL_RESISTANCE     100000
#define NOMINAL_TEMPERATURE    25
#define B_VALUE                3950

NTC_Thermistor* thermistor1 = NULL;
NTC_Thermistor* thermistor2 = NULL;
NTC_Thermistor* thermistor3 = NULL;

void setup()
{
  mot1.setMaxSpeed(1500);
  mot1.setSpeed(0);
  lcd.begin(16, 2);
  lcd.createChar(1, de);
  lcd.createChar(2, le);
  lcd.createChar(3, gon);
  lcd.createChar(4, goff);
  pinMode(dirPin, OUTPUT);
  lcd.setCursor(0, 0);
  lcd.print("FilaBruh V2.4");
  lcd.setCursor(0, 1);
  lcd.print("BENO & R.P.3D");
  delay (6000);
  lcd.clear();

  thermistor1 = new NTC_Thermistor(
    SENSOR1_PIN,
    REFERENCE_RESISTANCE,
    NOMINAL_RESISTANCE,
    NOMINAL_TEMPERATURE,
    B_VALUE,
    1,
    0
  );
  thermistor2 = new NTC_Thermistor(
    SENSOR2_PIN,
    REFERENCE_RESISTANCE,
    NOMINAL_RESISTANCE,
    NOMINAL_TEMPERATURE,
    B_VALUE,
    1,
    0
  );
  thermistor3 = new NTC_Thermistor(
    SENSOR3_PIN,
    REFERENCE_RESISTANCE,
    NOMINAL_RESISTANCE,
    NOMINAL_TEMPERATURE,
    B_VALUE,
    1,
    0
  );

  lastLcdUpdate = millis();
}
void loop()
{
  last_key = keypad.get_last_key();
  key = keypad.read_key();
  if (key != last_key) {
    switch (key) {
      case KEY_RIGHT:
        nhitrost = (nhitrost + 10);
        if (nhitrost >= 1210) nhitrost = 20;
        mot1.setSpeed(nhitrost);
        break;
      case KEY_LEFT:
        nhitrost = (nhitrost - 10);
        if (nhitrost < 20) nhitrost = 20;
        mot1.setSpeed(nhitrost);
        break;
      case KEY_UP:
        ntemp = (ntemp + 1);
        if (ntemp >= 260) ntemp = 260;
        break;
      case KEY_DOWN:
        ntemp = (ntemp - 1);
        if (ntemp < 0) ntemp = 0;
        break;
      case KEY_SELECT:
        smer = smer == 1 ? 0 : 1;
        break;
      case KEY_NONE:
      default:
        break;
    }

    senzor();
    updateLCD();
  }
  if (millis() - lastSenzorUpdate > 1000)
  {
    senzor();
  }

  if (millis() - lastLcdUpdate > 1000)
  {
    updateLCD();
  }
  mot1.runSpeed();
}

void senzor()
{
  lastSenzorUpdate = millis();
  // Serial.print ("senzor ");
  //Serial.println (lastSenzorUpdate);
  const double celsius1 = thermistor1->readCelsius();
  const double celsius2 = thermistor2->readCelsius();
  const double celsius3 = thermistor3->readCelsius();;
  temp1 = ((celsius1 + celsius2 + celsius3) / 3);
}


void updateLCD()
{
  lastLcdUpdate = millis();
  lcd.clear();
  lcd.setCursor (0, 0);
  lcd.print ("nT=");
  lcd.setCursor (3, 0);
  lcd.print (ntemp, 0);
  lcd.setCursor(0, 1);
  lcd.print ("nH=");
  lcd.print (nhitrost);
  lcd.setCursor(14, 1);
  lcd.print ("S");
  lcd.setCursor(7, 0);
  lcd.print ("Tg=");
  lcd.setCursor(10, 0);
  lcd.print (temp1, 0);
  lcd.setCursor(14, 0);
  lcd.print ("G");
  if (smer == 0) {
    lcd.setCursor(15, 1);
    lcd.write (1);
    digitalWrite(dirPin, HIGH);
  }
  if (smer == 1) {
    lcd.setCursor(15, 1);
    lcd.write (2);
    digitalWrite(dirPin, LOW);
  }
  if (ntemp <= (temp1 + 2 )) {
    lcd.setCursor(15, 0);
    lcd.write (4);
    digitalWrite(ssr, LOW);
  }
  if ((ntemp - 2) >= temp1) {
    lcd.setCursor(15, 0);
    lcd.write (3);
    digitalWrite(ssr, HIGH);
  }
}

Any suggestions, please?

The stepper is controlled by sending pulses. The shorter the interval between pulses, the faster the stepper turns. This means that if time to execute the other code in your loop is shorter than the interval between stepper pulses, it will not interfere with the stepper.

The sensor read and the LCD update both take a significant amount of time.

You can significantly decrease the amount of time that the senzor function blocks your loop by only doing one read per call to the function, and then averaging the read once you have accumulated 3 of them.

You can significantly decrease the amount of time that the updateLCD function blocks your loop by only updating the data on the LCD that has actually changed. There is some fixed text that you should only be written to the LCD once, in setup(). There is some data that is only changed when the keypad is used, and so should only be updated then. The only data on the LCD that needs to regularly be updated is temp1.

I would also use an if...else if to ensure that senzor() and updateLCD() will never both be called in the same loop.

pert:
The stepper is controlled by sending pulses. The shorter the interval between pulses, the faster the stepper turns. This means that if time to execute the other code in your loop is shorter than the interval between stepper pulses, it will not interfere with the stepper.

The sensor read and the LCD update both take a significant amount of time.

You can significantly decrease the amount of time that the senzor function blocks your loop by only doing one read per call to the function, and then averaging the read once you have accumulated 3 of them.

You can significantly decrease the amount of time that the updateLCD function blocks your loop by only updating the data on the LCD that has actually changed. There is some fixed text that you should only be written to the LCD once, in setup(). There is some data that is only changed when the keypad is used, and so should only be updated then. The only data on the LCD that needs to regularly be updated is temp1.

I would also use an if...else if to ensure that senzor() and updateLCD() will never both be called in the same loop.

I made some changes and is better, but still far from perfect.

I forget to mention that i'm more beginner in arduino programing, and it's a big thing that I mange to put all this together so far :).

Here is my new code:

#include <LiquidCrystal.h>
#include <DFR_LCD_Keypad.h>
#include <AccelStepper.h>
#include <NTC_Thermistor.h>
void LCD();
void motor();
void updateLCD();
byte de[8] = {
  B01000,
  B00100,
  B00010,
  B11111,
  B11111,
  B00010,
  B00100,
  B01000
};
byte le[8] = {
  B00010,
  B00100,
  B01000,
  B11111,
  B11111,
  B01000,
  B00100,
  B00010
};
byte gon[8] = {
  B10010,
  B01001,
  B10010,
  B01001,
  B10010,
  B00000,
  B11111,
  B11111
};
byte goff[8] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DFR_LCD_Keypad keypad(A0, &lcd);
AccelStepper mot1(1, 2, 3); // pin 2 = step, pin 3 = direction
int last_key, key;
int nhitrost = 20;
float ntemp = 0;
float temp1 = 0;
int smer = 0;
int x = 0;
//const int stepPin = 2;
const int dirPin = 11;
const int ssr = 12;
unsigned long lastLcdUpdate;
#define SENSOR1_PIN             A1
#define SENSOR2_PIN             A2
#define SENSOR3_PIN             A3
#define REFERENCE_RESISTANCE   4660
#define NOMINAL_RESISTANCE     100000
#define NOMINAL_TEMPERATURE    25
#define B_VALUE                3950
NTC_Thermistor* thermistor1 = NULL;
NTC_Thermistor* thermistor2 = NULL;
NTC_Thermistor* thermistor3 = NULL;
void setup()
{
  //Serial.begin(9600);
  mot1.setMaxSpeed(10000);
  mot1.setSpeed(0);
  lcd.begin(16, 2);
  lcd.createChar(1, de);
  lcd.createChar(2, le);
  lcd.createChar(3, gon);
  lcd.createChar(4, goff);;
  pinMode(dirPin, OUTPUT);
  lcd.setCursor(0, 0);
  lcd.print("FilaBruh V2.7");
  lcd.setCursor(0, 1);
  lcd.print("BENO & R.P.3D");
  delay (6000);
  lcd.clear();
  lcd.setCursor (0, 0);
  lcd.print ("nT=");
  lcd.setCursor(0, 1);
  lcd.print ("nH=");
  lcd.setCursor(14, 1);
  lcd.print ("S");
  lcd.setCursor(7, 0);
  lcd.print ("Tg=");
  lcd.setCursor(14, 0);
  lcd.print ("G");
  thermistor1 = new NTC_Thermistor(
    SENSOR1_PIN,
    REFERENCE_RESISTANCE,
    NOMINAL_RESISTANCE,
    NOMINAL_TEMPERATURE,
    B_VALUE,
    1,
    0
  );
  thermistor2 = new NTC_Thermistor(
    SENSOR2_PIN,
    REFERENCE_RESISTANCE,
    NOMINAL_RESISTANCE,
    NOMINAL_TEMPERATURE,
    B_VALUE,
    1,
    0
  );
  thermistor3 = new NTC_Thermistor(
    SENSOR3_PIN,
    REFERENCE_RESISTANCE,
    NOMINAL_RESISTANCE,
    NOMINAL_TEMPERATURE,
    B_VALUE,
    1,
    0
  );

  lastLcdUpdate = millis();
}
void loop()
{
  //Serial.println (x);
  last_key = keypad.get_last_key();
  key = keypad.read_key();
  if (key != last_key) {
    switch (key) {
      case KEY_RIGHT:
        nhitrost = (nhitrost + 10);
        if (nhitrost >= 1210) nhitrost = 20;
        mot1.setSpeed(nhitrost);
        break;
      case KEY_LEFT:
        nhitrost = (nhitrost - 10);
        if (nhitrost < 20) nhitrost = 20;
        mot1.setSpeed(nhitrost);
        break;
      case KEY_UP:
        ntemp = (ntemp + 1);
        if (ntemp >= 260) ntemp = 260;
        break;
      case KEY_DOWN:
        ntemp = (ntemp - 1);
        if (ntemp < 0) ntemp = 0;
        break;
      case KEY_SELECT:
        smer = smer == 1 ? 0 : 1;
        break;
      case KEY_NONE:
      default:
        break;
    }
    //updateLCD();
  }
  if (millis() - lastLcdUpdate > 1000)
  {
    updateLCD();
  }
  mot1.runSpeed();

}
void updateLCD()
{
  lastLcdUpdate = millis();
  if (x == 0) {
    lcd.setCursor (3, 0);
    lcd.print ("    ");
    lcd.setCursor (3, 1);
    lcd.print ("    ");
    lcd.setCursor(10, 0);
    lcd.print ("    ");
    lcd.setCursor(15, 1);
    lcd.print (" ");
    lcd.setCursor(15, 0);
    lcd.print (" ");
    lcd.setCursor (3, 0);
    lcd.print (ntemp, 0);
    lcd.setCursor (3, 1);
    lcd.print (nhitrost);
    lcd.setCursor(10, 0);
    lcd.print (temp1, 0);
    x = (x + 1);
    loop();
  }
  if (x == 1) {
    if (smer == 0) {
      lcd.setCursor(15, 1);
      lcd.write (1);
      digitalWrite(dirPin, HIGH);
    }
    if (smer == 1) {
      lcd.setCursor(15, 1);
      lcd.write (2);
      digitalWrite(dirPin, LOW);
    }
    if (ntemp <= (temp1 + 2 )) {
      lcd.setCursor(15, 0);
      lcd.write (4);
      digitalWrite(ssr, LOW);
    }
    if ((ntemp - 2) >= temp1) {
      lcd.setCursor(15, 0);
      lcd.write (3);
      digitalWrite(ssr, HIGH);
    }
    x = (x + 1);
    loop();
  }

  if (x == 2) {
    int celsius1 = thermistor1->readCelsius();
    int celsius2 = thermistor2->readCelsius();
    int celsius3 = thermistor3->readCelsius();;
    temp1 = ((celsius1 + celsius2 + celsius3) / 3);
    x = 0;
    loop();
  }

}

Don't call loop from updateLCD. You'll consume all your stack and crash in interesting but non-obvious ways.

You are still updating parts of the LCD unnecessarily.

You are still reading the thermistor three times in a single loop.

This code is still a lite buggy, but stepper working great. It contains three .ino files.

Main code:

#include <LiquidCrystal.h>
#include <DFR_LCD_Keypad.h>
#include <NTC_Thermistor.h>

//LCD 
byte de[8] = { B01000, B00100, B00010, B11111, B11111, B00010, B00100, B01000 };
byte le[8] = { B00010, B00100, B01000, B11111, B11111, B01000, B00100, B00010 };
byte gon[8] = { B10010, B01001, B10010, B01001, B10010, B00000, B11111, B11111 };
byte goff[8] = { B00000, B00000, B00000, B00000, B00000, B00000, B11111, B11111 };

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
DFR_LCD_Keypad keypad(A0, &lcd);

const int ssr = 12;

int last_key, key;
int nhitrost = 20;
float ntemp = 0;
float temp1 = 0;
int smer = 0;
int x = 0;
unsigned long lastLcdUpdate;

//NTC Sensors
#define SENSOR1_PIN             A1
#define SENSOR2_PIN             A2
#define SENSOR3_PIN             A3
#define REFERENCE_RESISTANCE   4660
#define NOMINAL_RESISTANCE     100000
#define NOMINAL_TEMPERATURE    25
#define B_VALUE                3950

NTC_Thermistor* thermistor1 = NULL;
NTC_Thermistor* thermistor2 = NULL;
NTC_Thermistor* thermistor3 = NULL;

// Stepper Vars
int temp_stepper_int = 6000; 
int temp_stepper_int_roll = 0; 
int temp_lcd_int = 500;
int temp_lcd_int_roll = 0;


const int stepPin = 2;
const int dirPin = 11;


// Debug echo
bool Debug_ON = false;



void setup()
{
  Serial.begin(9600);  // Setial debug port
  
  lcd.begin(16, 2);
  lcd.createChar(1, de);
  lcd.createChar(2, le);
  lcd.createChar(3, gon);
  lcd.createChar(4, goff);;
  pinMode(dirPin, OUTPUT);
  lcd.setCursor(0, 0);
  lcd.print("FilaBruh V2.7");
  lcd.setCursor(0, 1);
  lcd.print("BENO & R.P.3D");
  delay (6000);
  lcd.clear();
  lcd.setCursor (0, 0);
  lcd.print ("nT=");
  lcd.setCursor(0, 1);
  lcd.print ("nH=");
  lcd.setCursor(14, 1);
  lcd.print ("S");
  lcd.setCursor(7, 0);
  lcd.print ("Tg=");
  lcd.setCursor(14, 0);
  lcd.print ("G");
  thermistor1 = new NTC_Thermistor( SENSOR1_PIN, REFERENCE_RESISTANCE, NOMINAL_RESISTANCE, NOMINAL_TEMPERATURE, B_VALUE, 1, 0 );
  thermistor2 = new NTC_Thermistor( SENSOR2_PIN, REFERENCE_RESISTANCE, NOMINAL_RESISTANCE, NOMINAL_TEMPERATURE, B_VALUE, 1, 0 );
  thermistor3 = new NTC_Thermistor( SENSOR3_PIN, REFERENCE_RESISTANCE, NOMINAL_RESISTANCE, NOMINAL_TEMPERATURE, B_VALUE, 1, 0 );

  lastLcdUpdate = millis();
  
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(ssr, OUTPUT);
  interrupt_setup();
  
}
void loop()
{
  //Serial.println (x);
  last_key = keypad.get_last_key();
  key = keypad.read_key();
  if (key != last_key) {
    switch (key) {
      case KEY_RIGHT:
        nhitrost = (nhitrost + 10);
        if (nhitrost >= 1210) nhitrost = 20;
        //mot1.setSpeed(nhitrost);
        motor((nhitrost*10));
        break;
      case KEY_LEFT:
        nhitrost = (nhitrost - 10);
        if (nhitrost < 20) nhitrost = 20;
        //mot1.setSpeed(nhitrost);
        motor((nhitrost*10));
        break;
      case KEY_UP:
        ntemp = (ntemp + 1);
        if (ntemp >= 260) ntemp = 260;
        break;
      case KEY_DOWN:
        ntemp = (ntemp - 1);
        if (ntemp < 0) ntemp = 0;
        break;
      case KEY_SELECT:
        smer = smer == 1 ? 0 : 1;
        break;
      case KEY_NONE:
      default:
        break;
    }
  }
  //if (millis() - lastLcdUpdate > 1000)
  //{
  //  updateLCD();
  //}

}

void updateLCD()
{
  lastLcdUpdate = millis();
  if (x == 0) {
    lcd.setCursor (3, 0);
    lcd.print ("    ");
    lcd.setCursor (3, 1);
    lcd.print ("    ");
    lcd.setCursor(10, 0);
    lcd.print ("    ");
    lcd.setCursor(15, 1);
    lcd.print (" ");
    lcd.setCursor(15, 0);
    lcd.print (" ");
    lcd.setCursor (3, 0);
    lcd.print (ntemp, 0);
    lcd.setCursor (3, 1);
    lcd.print (nhitrost);
    lcd.setCursor(10, 0);
    lcd.print (temp1, 0);
    x = (x + 1);
  }
  if (x == 1) {
    if (smer == 0) {
      lcd.setCursor(15, 1);
      lcd.write (1);
      digitalWrite(dirPin, HIGH);
    }
    if (smer == 1) {
      lcd.setCursor(15, 1);
      lcd.write (2);
      digitalWrite(dirPin, LOW);
    }
    if (ntemp <= (temp1 + 2 )) {
      lcd.setCursor(15, 0);
      lcd.write (4);
      digitalWrite(ssr, LOW);
    }
    if ((ntemp - 2) >= temp1) {
      lcd.setCursor(15, 0);
      lcd.write (3);
      digitalWrite(ssr, HIGH);
    }
    x = (x + 1);
  }

  if (x == 2) {
  int celsius1 = thermistor1->readCelsius();
  int celsius2 = thermistor2->readCelsius();
  int celsius3 = thermistor3->readCelsius();;
    temp1 = ((celsius1 + celsius2 + celsius3) / 3);
    x = 0;
  }

}

Debug:

// Debuggiranje preko RS porta
void Debug_Echo(String msg){ 
  if(Debug_ON){ 
    Serial.println(msg); 
  }
}

Stepper routine:

void interrupt_setup(){ 
 cli();//stop interrupts
 //set timer0 interrupt at 2kHz
 TCCR0A = 0;// set entire TCCR2A register to 0
 TCCR0B = 0;// same for TCCR2B
 TCNT0  = 0;//initialize counter value to 0
 // set compare match register for 2khz increments
 OCR0A = 124;// = (16*10^6) / (2000*64) - 1 (must be <256)
 // turn on CTC mode
 TCCR0A |= (1 << WGM01);
 // Set CS01 and CS00 bits for 64 prescaler
 TCCR0B |= (1 << CS01) | (1 << CS00);   
 // enable timer compare interrupt
 TIMSK0 |= (1 << OCIE0A);
 sei();//allow interrupts

}

ISR(TIMER0_COMPA_vect){//timer0 interrupt - 1ms 
   //Debug_Echo("interrupt"); 
   if(temp_stepper_int_roll == 0){
     digitalWrite(stepPin, !digitalRead(stepPin)); 
     temp_stepper_int_roll = temp_stepper_int;  
     Debug_Echo("toggle");
   }
   else{
     temp_stepper_int_roll = temp_stepper_int_roll - 1; 
   }
   if(temp_lcd_int_roll == 0){
     updateLCD();
     temp_lcd_int_roll = temp_lcd_int;  
   }
   else{
     temp_lcd_int_roll = temp_lcd_int_roll - 1;  
   }


   
}

void motor(int Speed){
 temp_stepper_int = 6000 / Speed; 
 Serial.print("Speed: ");  Serial.println(Speed);
 Serial.print("temp_stepper_int: "); Serial.println(temp_stepper_int);

}