hi i am on a project that controls 2 step motors simultaneously. it simply goes up and down periodically. it is working fine except powering up Arduino. Whenever i power on system my motors stars shaking abruptly. İ solve this problem with placing an on-off button to motor driver's adapter. and i will power step motors after powering the Arduino. but it is very unconfortable.
is this a problem? why it happens? is there any convenient way to solve this?
maybe another solution is setting enable pin high when powering Arduino. but i cant figure out how to set eneble pin low while it connected directly 5v.
lastly i am open for any comment on my code. i try to improve myself and there is so much thing to hear from you.
i am using
arduino mega
a4998 motor driver
17HS3401 step motor
i took a video shows the problem.
problem video.zip (4.9 MB)
// Define pin connections & motor's steps per revolution
const int dirPin1 = 3;
const int stepPin1 = 2;
const int dirPin2 = 4;//
const int stepPin2 = 5;
//screen
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//j1
#define j1sw A0
#define j1y A2
#define j1x A1
int j1swVal=0;
int j1yVal=0;
int j1xVal=0;
#define testButon 8
bool testVal;
#define initButon 9
//parameters
const int stepsPerRevolution = 200;
int stepdelay=4000;
int hiz=0;
int steps=200;
int mesafe=1;
int beklemeSuresi=3000;
float bekleme=3.0;
unsigned long counter=0;
void setup(){
// Declare pins as Outputs
Serial.begin(9600);
// Declare pins as Outputs
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
//j1
pinMode(j1sw,INPUT);
pinMode(j1x,INPUT);
pinMode(j1y,INPUT);
//screen
lcd.init(); // initialize the lcd
lcd.backlight();
//welcome screen
lcd.setCursor(2,0);
lcd.print("Hucre Uyarim");
lcd.setCursor(2,1);
lcd.print("v2");
delay(2000);
//parameters
initialize();
}
void loop(){
// Set motor direction clockwise
lcd.setCursor(0,1);lcd.print("dongu: ");lcd.print(counter);
counter++;
digitalWrite(dirPin1, HIGH);
digitalWrite(dirPin2, LOW);
for(int x = 0; x < steps; x++)
{
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay);
}
delay(beklemeSuresi); // Wait a second
// Set motor direction counterclockwise
digitalWrite(dirPin1, LOW);
digitalWrite(dirPin2, HIGH);
for(int x = 0; x < steps; x++)
{
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay);
}
delay(beklemeSuresi); // Wait a second
bool initButonVal=0;
initButonVal=digitalRead(initButon);
if (initButonVal==1)initialize();
Serial.println(initButon);
}
void test(){
// Set motor direction clockwise
digitalWrite(dirPin1, HIGH);
digitalWrite(dirPin2, LOW);
for(int x = 0; x < steps; x++)
{
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay);
}
delay(beklemeSuresi); // Wait a second
// Set motor direction counterclockwise
digitalWrite(dirPin1, LOW);
digitalWrite(dirPin2, HIGH);
for(int x = 0; x < steps; x++)
{
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
delayMicroseconds(stepdelay);
}
delay(10); // Wait a second
}
void stepup(){
Serial.println("stepup");
digitalWrite(dirPin1, HIGH);
digitalWrite(dirPin2, LOW);
for(int x = 0; x < 10; x++)
{
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
delayMicroseconds(2000);
}
delay(100);
}
void stepdown(){
Serial.println("stepup");
digitalWrite(dirPin1, LOW);
digitalWrite(dirPin2, HIGH);
for(int x = 0; x < 10; x++)
{
digitalWrite(stepPin1, HIGH);
digitalWrite(stepPin2, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin1, LOW);
digitalWrite(stepPin2, LOW);
delayMicroseconds(2000);
}
delay(100);
}
void initialize(){
//start position
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Baslangic Konumu");
lcd.setCursor(0,1);
lcd.print("ayarlayin");
while(1){
j1swVal=digitalRead(j1sw);
j1yVal=analogRead(j1y);
j1xVal=analogRead(j1x);
testVal=digitalRead(testButon);//test
if (testVal==1) test();
if (j1yVal<200) stepup();
if (j1yVal>800) stepdown();
if (j1swVal==0) {
while(j1swVal==0){
j1swVal=digitalRead(j1sw);
delay(100);
}
break;
}
}
//speed define
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Hizi ayarlayin");
lcd.setCursor(0,1);
lcd.print("hiz:");
while(1){
j1swVal=digitalRead(j1sw);
j1yVal=analogRead(j1y);
j1xVal=analogRead(j1x);
testVal=digitalRead(testButon);//test
if (testVal==1) test();
lcd.setCursor(6,1);
lcd.print(hiz);
if (j1yVal<200) hiz++;
if (j1yVal>800) hiz--;
if (j1swVal==0) {
while(j1swVal==0){
j1swVal=digitalRead(j1sw);
delay(100);
}
break;
}
if (hiz<0) hiz=0;
if (hiz>9) hiz=9;
stepdelay=(4000-(hiz*300))/4;
delay(150);
}
//updown distance
while(1){
j1swVal=digitalRead(j1sw);
j1yVal=analogRead(j1y);
j1xVal=analogRead(j1x);
testVal=digitalRead(testButon);//test
if (testVal==1) test();
if (j1yVal<200){
mesafe++;
lcd.clear();
}
if (j1yVal>800){
mesafe--;
lcd.clear();
}
if (j1swVal==0) {
while(j1swVal==0){
j1swVal=digitalRead(j1sw);
delay(150);
}
break;
}
if (mesafe<1) mesafe=1;
if (mesafe>40) mesafe=40;
steps=(200/8)*mesafe*2;
lcd.setCursor(0,0);
lcd.print("Hareket mesafesi");
lcd.setCursor(0,1);
lcd.print("mesafe:");
lcd.setCursor(7,1);
lcd.print(mesafe);
lcd.setCursor(10,1);
lcd.print("mm");
delay(100);
}
//wait between up and down
lcd.clear();
lcd.setCursor(0,0);
lcd.print("bekleme suresi");
lcd.setCursor(0,1);
lcd.print("sure: ");
while(1){
j1swVal=digitalRead(j1sw);
j1yVal=analogRead(j1y);
j1xVal=analogRead(j1x);
testVal=digitalRead(testButon);//test
if (testVal==1) test();
lcd.setCursor(6,1);
lcd.print(bekleme);
lcd.setCursor(10,1);
lcd.print(" sn");
if (j1yVal<200) bekleme+=0.1;
if (j1yVal>800) bekleme-=0.1;
if (j1swVal==0) {
while(j1swVal==0){
j1swVal=digitalRead(j1sw);
delay(100);
}
break;
}
if (bekleme<0.1) bekleme=0.1;
if (bekleme>15) bekleme=15;
beklemeSuresi=bekleme*1000;
delay(150);
}
lcd.clear();
lcd.setCursor(0,0);lcd.print("v:");lcd.print(hiz);
lcd.setCursor(4,0);lcd.print(mesafe);lcd.print("mm");
lcd.setCursor(9,0);lcd.print(bekleme);lcd.print("sn");
counter=0;
}

