Trouble with stepmotor for earthquake simulator

hi everyone. i’ve been working on this project lately, it’s a machine that simulates eathquakes. i’m using an Arduino UNO, a ys-div268n driver, a nema17 step motor, 10k ohm potentiometer and a lcd screen with i2c interface.
the motor works but when i run the code of the whole system it stops for just a fraction of a second when reading the value of the potentiometer, by doing this the motor get stuck sometimes and it’s not good. i tried fixing this with a switch (with it i can enable the reading of the value) but it’s even worse at high speeds (6-10 rps).
leaving the code below.

#include <LiquidCrystal_I2C.h>  //libreria per l'utilizzo del display con iterfaccia i2c
#define Pot_Vel A2
#define Sw_vel 2

LiquidCrystal_I2C lcd(0x27, 20, 4);  //dichiarazione vcariabile del display. indirizzo i2c 0x27

int i1 = 0;
int Sw_val;

int stepPin = 6;  //pin uscita motore
int dirPin = 9;
int enPin = 8;

void setup() {
  Serial.begin(9600);

  pinMode(Sw_vel, INPUT_PULLUP);
    
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);
 
  lcd.init();   //inizializzazione lcd
  lcd.backlight();  //attivazione retro illuminazione lcd
  
  lcd.setCursor(0, 0);
  lcd.print("Benvenuti");  //mostra la scritta di benvenuto sul lcd
  
  delay(1500);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Impostare la");
  lcd.setCursor(0, 1);
  lcd.print("Velocita'");
  
}

void loop(){

  Sw_val = digitalRead(Sw_vel);  //interruttore per leggere il valore del potenziometro

  if(Sw_val == HIGH){
  
    int analogValue_vel = analogRead(Pot_Vel);

    double i1map = map(analogValue_vel, 0, 1023, 100000, 2000000);

    i1 = i1map/6400;

    double freq = 1/(i1map/1000000);

    lcd.clear();

    lcd.setCursor(0, 0);
    lcd.print("Frequenza");

    lcd.setCursor(0, 2);
    lcd.print(freq);

    lcd.setCursor(7, 2);
    lcd.print("Hz");

    Serial.println(i1);
    Serial.println(freq);
    Serial.println(i1map);

  }

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction

  // Makes 200 pulses for making one full cycle rotation

  for(int x = 0; x < 3200; x++) {

    digitalWrite(stepPin,HIGH); 

    delayMicroseconds(i1); 

    digitalWrite(stepPin,LOW); 

    delayMicroseconds(i1); 

  }
}

i don’t know where the problem could be.
(Code tags added by moderator. Next time, use the </> button on the menu bar please.)

Hello
prevent the use of the delay() function. This function will block the continuous execution of the sketch.
A timer function based on the IDE example BLINKOUTWITHDELAY will be a good kick off for your project.

thank you. i’ll try asap and see if it works for me

did you mean something like this?

#include <LiquidCrystal_I2C.h>  //libreria per l'utilizzo del display con iterfaccia i2c
#define Pot_Vel A2

LiquidCrystal_I2C lcd(0x27, 20, 4);  //dichiarazione vcariabile del display. indirizzo i2c 0x27

unsigned long i1 = 0;

int stepPin = 6;  //pin uscita motore
int dirPin = 9;
int enPin = 8;

unsigned long prevMillis = 0, currMillis = millis();



void setup() {
  Serial.begin(9600);
    
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);

  digitalWrite(enPin, LOW);
 
  lcd.init();   //inizializzazione lcd
  lcd.backlight();  //attivazione retro illuminazione lcd
  
  lcd.setCursor(0, 0);
  lcd.print("Benvenuti");  //mostra la scritta di benvenuto sul lcd
  
  delay(1500);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Impostare la");
  lcd.setCursor(0, 1);
  lcd.print("Velocita'");
  
}

void loop(){

   double i1map = map(analogRead(Pot_Vel), 0, 1023, 100000, 2000000);

   i1 = i1map/6400;

   double freq = 1/(i1map/1000000);

   lcd.clear();

   lcd.setCursor(0, 0);
   lcd.print("Frequenza");

   lcd.setCursor(0, 2);
   lcd.print(freq);

   lcd.setCursor(7, 2);
   lcd.print("Hz");

   Serial.println(i1);
   Serial.println(freq);
   Serial.println(i1map);

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction

  // Makes 200 pulses for making one full cycle rotation

  if(currMillis - prevMillis >= 2*i1){
    
    prevMillis = currMillis;
    
    for(int x = 0; x < 3200; x++) {

      digitalWrite(stepPin,HIGH); 

      millis(); 

      digitalWrite(stepPin,LOW); 

      millis(); 

    }
  }
}

i’m pretty new to this function and i don’t know exactly how to use it

(Code tags added by moderator. Next time, use the </> button on the menu bar please.)

update.
i tried to modify the previous skatch, i found by my self it war wrong.
here’s the new one.

#include <LiquidCrystal_I2C.h> //libreria per l’utilizzo del display con iterfaccia i2c
#define Pot_Vel A2

LiquidCrystal_I2C lcd(0x27, 20, 4); //dichiarazione vcariabile del display. indirizzo i2c 0x27

unsigned long i1 = 0;

int stepPin = 6; //pin uscita motore
int dirPin = 9;
int enPin = 8;

int stepState = LOW;

unsigned long prevMillis = 0, currMillis = millis();

void setup() {
Serial.begin(9600);

pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);

digitalWrite(enPin, LOW);

lcd.init(); //inizializzazione lcd
lcd.backlight(); //attivazione retro illuminazione lcd

lcd.setCursor(0, 0);
lcd.print(“Benvenuti”); //mostra la scritta di benvenuto sul lcd

delay(1500);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Impostare la”);
lcd.setCursor(0, 1);
lcd.print(“Velocita’”);

}

void loop(){

double i1map = map(analogRead(Pot_Vel), 0, 1023, 100000, 2000000);

i1 = i1map/6400;

double freq = 1/(i1map/1000000);

lcd.clear();

lcd.setCursor(0, 0);
lcd.print(“Frequenza”);

lcd.setCursor(0, 2);
lcd.print(freq);

lcd.setCursor(7, 2);
lcd.print(“Hz”);

Serial.println(i1);
Serial.println(freq);
Serial.println(i1map);

delay(100);

digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction

// Makes 200 pulses for making one full cycle rotation

if(currMillis - prevMillis >= i1){

prevMillis = currMillis;

if(stepState == LOW){
  stepState = HIGH;
}

else {
  stepState = LOW;
}

digitalWrite(stepPin,stepState); 

}
}

i tried it but it’s not working at all. the driver doesn’t give power thr motor, when i turn it on and spin the motor with my hand it moves just like if ther isn’t current. but as soon as i try the old sketch it works just fine, oblviously with the same issues it had before.
i tried with a diy gdf using a square wave and connecting all the gnd together but nothing worked.

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