Motors cant go in the other direction with this code

Hey guys,

so I’m really new to this aduino stuff. Like one month or so I’m playing around with motors and sensors.
Right now I’m working on a project where i put 2 Stepper Motors in both upper corners of a whiteboard. Attached to both (with strings) is some kind of pencil holder.
So with that i can control the position of the pencil on the board with the length of the wires by rotating the steppers.

I had one version before wich worked good, the problem was the speed of the motors, it was just way to slow, so i wrote a new code wich is aswell much shorter.

My idea is the following :
i have a simple analog-Stick , I set the position of the pecil to a certain position on the board and if i move the analogstick down, the pencil will go down aswell. If i move it back up, the pencil will do that aswell.

My problem is : the Steppers just turn in one direction with this version of the code. dor example : i push the stick down, and both motors turn clockwise because both have to loosen the wire. But if i go back up, nothing happens. Same if i go to left or right. for a left movement the left motor needs to spin in the direction to tighten the wire and the riight one needs to lose it in the same moment.
But both of the motors are just able to spin in one of the directions.

So the first thing that comes in mind is, that the direction pin is just not set correctly or the code wont even get to the point . ( I use an a4988 driver to run the steppers, there are 3 pins to control it: en for anabling, dir for direction control , and step, wich u turn on and off to do a full step.)
so the code is definetly getting to the point where it should change the direction (Serial monitor shows the print that should be printed when in the line below)

I calculate the amount of steps each motos has to do when moving the stick, so i thought that might be the next failure, but that seems to be correct aswell.

I really cant help myself with that. maybe you have an idea what coul be wrong with it . the motors are fine, in an extra sketch i can control them how i want so it needs to be something in that code.

It is looking kind of messy but i hope you understand.

the interesting part is the void positionieren(); in this one, there is the old x and y coordinate and the new ones. with the differences i calculate the distance between motor and pencil . if the new lengh is bigger then the old lengh then the motor needs to loose the wire, so turning in one direction , if it shorter in the other direction .
then i calculate the number of steps to take and make the motors do as many steps as i calculated.
thats it from the theory. here is my code and i hope you can help me fix that issue!

int xneu;
int xalt;
int yneu;
int yalt;
int del = 1;
int xfein;
int yfein;
int button;

int deltaxy = 50;
float stepdist = 0.769; // in mm
int fensterhohe = 700; // Abmessungen in mm
int fensterbreite = 500;
int Motorhohe = 900;

int pos0x = fensterbreite / 2;
int pos0y = fensterhohe;
int Hereinziehen = HIGH;
int Herausziehen = LOW;



int M1EnPin = 2;
int M2EnPin = 5;
int M2DirPin = 6;
int M1DirPin = 3;
int M1StepPin = 4;
int M2StepPin = 7;




void setup() {
  Serial.begin(9600);
//lcd.begin();
//lcd.noBacklight();
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

 xalt = pos0x;
 yalt = pos0y;

xneu = pos0x;
yneu = pos0y;


 

}






void positionieren () {
digitalWrite(M1EnPin, LOW);
digitalWrite(M2EnPin, LOW);



float diffdistm1 = sqrt(pow(xneu, 2) + pow(Motorhohe - yneu, 2)) - sqrt(pow(xalt, 2) + pow(Motorhohe - yalt, 2));
float diffdistm2 = sqrt(pow(fensterbreite - xneu, 2) + pow(Motorhohe - yneu, 2)) - sqrt(pow(fensterbreite - xalt, 2) + pow(Motorhohe - yalt, 2));

float stepsm1 = diffdistm1 / stepdist;
float stepsm2 = diffdistm2 / stepdist;

int stepsm1int = round(stepsm1);
int stepsm2int = round(stepsm2);
stepsm1int = abs (stepsm1int);
stepsm2int = abs (stepsm2int);
Serial.print("stepsm1");
Serial.println(stepsm1int);
Serial.print("stepsm2");
Serial.println(stepsm2int);
Serial.print("diffdistm1:");
Serial.println(diffdistm1);
Serial.print("diffdistm2:");
Serial.println(diffdistm2);


if  (diffdistm1 < 0)
{digitalWrite(M1DirPin, Hereinziehen);
Serial.println("1 Dreht rein");}
if (diffdistm1 >= 0) { digitalWrite(M1DirPin, Herausziehen);
  Serial.println("1 dreht raus");}

if ( diffdistm2 < 0)
{ digitalWrite(M2DirPin, Hereinziehen);
Serial.println("2 Dreht rein");}
else { digitalWrite(M2DirPin, Herausziehen);
Serial.println("2 dreht raus");
}

int counter1 = 0;
int counter2 = 0;
Serial.println(stepsm1int);
Serial.println(stepsm2int);
delay(2000);
while ( counter1 < stepsm1int || counter2 < stepsm2int)
{Serial.println("for abfrage laeuft");
Serial.println ( counter1);
Serial.println(counter2);
  if( counter1 < stepsm1)
{digitalWrite(M1StepPin, HIGH);}
if ( counter2 < stepsm2)
{ digitalWrite(M2StepPin, HIGH);}
delay(del);
digitalWrite(M1StepPin, LOW);
digitalWrite(M2StepPin, LOW);
delay(del);
counter1 = counter1 + 1;
counter2 = counter2 + 1;
}
delay(2000);
xalt = xneu;
yalt = yneu;




}



void loop() {
 
int xval = analogRead(A0);
int yval = analogRead(A1);
xfein = xneu;
yfein = yneu;
 button = digitalRead(13);

if( xval >= 685)
{
  xneu = xalt + deltaxy;
  if ( xneu >= fensterbreite)
  {xneu = fensterbreite;}}
if( yval >= 685)
{yneu = yalt + deltaxy;
  if ( yneu >= fensterhohe)
  {yneu = fensterhohe;}}
if( xval <= 10)
{xneu = xalt - deltaxy;
  if ( xneu <= 0)
  {xneu = 0;}}
if( yval <= 10)
{yneu = yalt - deltaxy;
  if ( yneu <= 0)
  {yneu = 0;}}



Serial.println(xneu);
Serial.println(yneu);

Serial.println(button);


if (xneu != xalt || yneu != yalt)
{
int zeit1 = millis();
Serial.println("pos startet");
positionieren();
int zeit2 = millis();
int zeit3 = zeit2 - zeit1;
Serial.print("Zeit fuer positionieren: ");
Serial.println(zeit3);
} else { Serial.println("Warten auf Eingabe...");
}
/*
int button = digitalRead(13);
if (button == 0 )
{
  buttonstart:
  lcd.backlight();
xval = analogRead(A0);
yval = analogRead(A1);
Serial.println(yval);
Serial.println(xval);

if( xval >= 1023)
{ xfein = xfein + 10;}
  else if (xfein >= 800)
  {xfein = xfein + 5;}
  else if (xfein >= 600)
  {xfein = xfein + 1;}
  
  if ( xfein >= fensterbreite)
  {xfein = fensterbreite;}
  
if( yval >= 1023)
{ yfein = yfein + 10;}
  else if (yfein >= 800)
  {yfein = yfein + 5;}
  else if (yfein >= 600)
  {yfein = yfein + 1;}

  if ( yfein >= fensterhohe)
  {yfein = fensterhohe;}


if( xval <= 20)
{ xfein = xfein - 10;}
 if ( xfein <= 200)
{xfein = xfein - 5;}
 if ( xfein <= 300)
{xfein = xfein - 1;}

  if ( xfein <= 0)
  {xfein = 0;}

  
if( yval <= 20)
{ yfein = yfein - 10;}
else if ( yfein <= 200)
{yfein = yfein - 5;}
else if ( yfein <= 400)
{yfein = yfein - 1;}

  if ( yfein <= 0)
  {yfein = 0;}

  
lcd.setCursor(0 ,0);
lcd.print("X:");
lcd.print(xfein);
lcd.setCursor(1,0);


delay(50);
button = digitalRead(13);
if ( button == 0)
{xneu = xfein;
yneu = yfein;
lcd.noBacklight();
positionieren();

}
else { goto buttonstart;}

  
} */


}

I can't make heads or tails of your program. NOTHING belongs on a line after a {. Every } belongs on a line BY ITSELF.

Tools + Auto Format, after you manually insert the missing carriage returns will properly indent your code.

Why are you not using the Stepper library? Or the MultiStepper library if you want multiple steppers to move at the same time.

What kind of stepper driver are you using?

PaulS:
I can't make heads or tails of your program. NOTHING belongs on a line after a {. Every } belongs on a line BY ITSELF.

Tools + Auto Format, after you manually insert the missing carriage returns will properly indent your code.

Why are you not using the Stepper library? Or the MultiStepper library if you want multiple steppers to move at the same time.

What kind of stepper driver are you using?

yeah my nomenclature isnt the best, I use { and } in same lines with other stuff if the whole statement belongs togethere, that seemed logical to me .

I dont get what you mean with the sentence beginning with Tools + Auto Format.

I am using the a4988 stepper motor driver.

I dont use stepper library because first I didnt know it existed and then i found out, that the controlling itself is pretty simple with the enable, diretion and step pins.

Nic0lodeon:
yeah my nomenclature isnt the best, I use { and } in same lines with other stuff if the whole statement belongs togethere, that seemed logical to me .

Try wrapping it around in your head. Every statement on a new line. Everything that belongs to each other on the same level on indent :slight_smile:

Nic0lodeon:
I dont get what you mean with the sentence beginning with Tools + Auto Format.

Hint: The IDE has a menu called Tools :wink:

I think you have the Direction and Enable signals swapped. That would mean the Direction signal (on the Enable pin) would always be LOW (Herausziehen) and the Enable signal (on the Direction pin) would only be LOW (enabled) when the Direction pin was set LOW (Herausziehen). For inward motion the Direction pin would be set to HIGH (Hereinziehen) which would set the Enable signal HIGH (disabled). Check your pin numbers and signal wires.

In the IDE, format your code:
PC CTRL T
Apple CMD T

johnwasser:
I think you have the Direction and Enable signals swapped. That would mean the Direction signal (on the Enable pin) would always be LOW (Herausziehen) and the Enable signal (on the Direction pin) would only be LOW (enabled) when the Direction pin was set LOW (Herausziehen). For inward motion the Direction pin would be set to HIGH (Hereinziehen) which would set the Enable signal HIGH (disabled). Check your pin numbers and signal wires.

Did you see something in the code that made you think that ? I cant find where. The motors (drivers) are connected to the pins 2 , 3, 4 for enable , direction and step . same goes for the second motor with 5, 6, 7. so that should be correct.
But yeah, that could cause the problem .

larryd:
In the IDE, format your code:
PC CTRL T
Apple CMD T

thanks mate! does this just change the layout or could that have influence on the code itself?

‘Only’ makes your code much easier to read.

Nic0lodeon:
Did you see something in the code that made you think that?

No, but it's the only mistake I can think of that would cause the exact symptom you see. I can't see anything in the code that would allow motion in the the outgoing direction but not in the incoming direction. The Enable pins are never changed and the stepping code shouldn't care which way the Direction pin is set.

Nic0lodeon:
The motors (drivers) are connected to pins 2, 3, and 4 for enable, direction and step. The same goes for the second motor with pins 5, 6, and 7, so that should be correct.

Assuming that the driver boards are labeled correctly and each wire is connected correctly at both ends. And no shorts between the Direction and Enable wires.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks... Tom.. :slight_smile: