LCD - stepper motor conflict

Hello, can someone help me whit my project? I have built a rotary welding table, and I'm using stepper motor NEMA 32, driver for it, arduino nano, lcd display to see the direction and the speed, also 3 push pullups buttons to control the motor and potentiometer for the speed. I’m good in building things, but new in coding and I’ve managed to run this thing, but I've got problem whit the code. However, after reading in the net I see that there is a problem using lsd and stepper motor in the same time! When I start the motor rotating every 1000 millis interrupts the speed of the motor, which is unacceptable. Otherwise, everything else is working. I’ll be appreciated if someone help!

#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define potentiometer A0
#define bt_F A1
#define bt_S A2
#define bt_B A3

#define dirPin 10
#define stepPin 11
#define enPin 9

int read_ADC;
int Speed_LCD, Speed_Delay;
int Mode = 1, flag = 0;

unsigned long lastLcdUpdateTime = 0;

void setup() {
  pinMode(potentiometer, INPUT);
  pinMode(bt_F, INPUT_PULLUP);
  pinMode(bt_S, INPUT_PULLUP);
  pinMode(bt_B, INPUT_PULLUP);
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(enPin, OUTPUT);

  lcd.begin(16, 2);
  lcd.backlight();
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print(" WELCOME To  My ");
  lcd.setCursor(0, 1);
  lcd.print("   WORKSHOP   ");
  delay(2000);
  lcd.clear();
}

void updateLCD() {
  lcd.setCursor(0, 0);
  lcd.print("   Speed: ");
  lcd.print(Speed_LCD);
  lcd.print("%  ");

  lcd.setCursor(0, 1);
  if (Mode == 0) lcd.print("      Free      ");
  else if (Mode == 1) lcd.print("      Stop      ");
  else if (Mode == 2) lcd.print("    Clockwise   ");
  else if (Mode == 3) lcd.print("  Anticlockwise ");
}

void loop() {
  read_ADC = analogRead(potentiometer);
  Speed_Delay = map(read_ADC, 0, 1023, 5000, 10);
  Speed_LCD = map(read_ADC, 0, 1023, 0, 100);

  // Update LCD display every 1000ms (1 second)
  if (millis() - lastLcdUpdateTime >= 1000) {
    lastLcdUpdateTime = millis();
    updateLCD();
  }

  if (digitalRead(bt_F) == 0) { Mode = 2; digitalWrite(enPin, LOW); } // Clockwise

  if (digitalRead(bt_S) == 0) {
    if (flag == 0) {
      flag = 1;
      if (Mode > 1) Mode = 1;
      else {
        Mode = !Mode;
        if (Mode == 0) digitalWrite(enPin, HIGH);
        else digitalWrite(enPin, LOW);
      }
      delay(100);
    }
  } else { flag = 0; }

  if (digitalRead(bt_B) == 0) { Mode = 3; digitalWrite(enPin, LOW); } // Anticlockwise

  if (Speed_LCD > 0 && Mode > 1) {
    if (Mode == 2) digitalWrite(dirPin, LOW);
    else digitalWrite(dirPin, HIGH);

    digitalWrite(stepPin, HIGH);
    delayMicroseconds(Speed_Delay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(Speed_Delay);
  }
}

This means "do nothing" for 100 ms, which also means "don't run the motor for 100 ms". Take a look at the Arduino Blink Without Delay example to learn how to do this differently.

Also, use a stepper library like AccelStepper to avoid these blocking statements:

    digitalWrite(stepPin, HIGH);
    delayMicroseconds(Speed_Delay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(Speed_Delay);

I recommend the MobaTools stepper library. In my opinion MobaTools is easier to learn and use than Accelstepper.

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