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.)