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;}
} */
}