@agmue erst einmal Danke für die Hilfe bis Heute.
Nachdem ich die Version mit den Servos verworfen habe, baue ich mein Projekt mit Servomotoren.
Zur Vorgeschichte. Ich möchte gleichzeitig und synchron laufend 2 Stepper ansteuern. Nach links von 0° bis 135° zum Endschalter und nach rechts von 0° bis -135° zum Endschalter. 0 Grad ist die Mittelstellung auch mit Schalter. Für die Schalter verwende ich Hallsensoren. Die Sensoren befinden sich nur an einem Stepper, weil der andere ja eigentlich synchron mitläuft. Für den Fall der Fälle kann ich aber auch manuell mit zwei Tastern nachjustieren.
Funktionieren tut soweit alles, außer wenn ich die Mittelstellung anfahre, bleibt der Stepper je nachdem ob ich von links oder rechts komme in 2 unterschiedlichen Positionen stehen. Das liegt wohl an der Funktion des Hall Sensors.
Mein Frage könnte man nicht sagen, das der Stepper noch ein gewisse Anzahl Schritte weiter fährt, damit er immer in der selben Mittelposition ankommt. Oder lässt man den Mittelsensor weg und zählt die Schritte bis zur Mitte.
Dafür müßte doch aber vor der void Schleife eine Initialisierung stattfinden, die erst einen Endschalter anfährt und dann bis zur Mitte zählt. Das ist mir aber bissl zu hoch ohne Hilfe.
Hier mal der aktuelle Sketch
// Test Stepper links und rechts
// Mit Multischalter und Endlagen links und rechts
// Stopp bei erreichen der Mittelstellung
// mit 2 Steppern parallel und 2x EasyDriver 4.4
// Geschwindigkeit einstellbar
// Stepper Farben Orange an Motor A1, Pink an Motor A2, Gelb an Motor B1, Blau an Motor B2
int sleep = A0; // Stopp Stepper 1 Anschluss an SLEEP 1
int sleep1 = A1; // Stopp Stepper 2 Anschluss an SLEEP 2
int dirpin = 10; // Easy Driver Stepper 1 + 2 parallel
int steppin = 11; // Easy Driver Stepper 1 + 2 parallel
int dirpin1 = 12; // Easy Driver Stepper 3 + 4 parallel
int steppin1 = 13; // Easy Driver Stepper 3 + 4 parallel
int cal_links = A2; // Taster zum links kalibrieren
int cal_rechts = A3; // Taster zum rechts kalibrieren
const int buttonPin1 = 2; // Anschluss Multiswitch Kanal 1
const int buttonPin2 = 3; // Anschluss Multiswitch Kanal 2
const int buttonPin3 = 4; // Anschluss Endschalter 1 Stepper 1 + 2
const int buttonPin4 = 5; // Anschluss Endschalter 2 Stepper 1 + 2
const int buttonPin5 = 6; // Anschluss Schalter Mitte Stepper 1 + 2
const int buttonPin6 = 7; // Anschluss Endschalter 1 Stepper 3 + 4
const int buttonPin7 = 8; // Anschluss Endschalter 2 Stepper 3 + 4
const int buttonPin8 = 9; // Anschluss Schalter Mitte Stepper 3 + 4
boolean altPin1;
boolean altPin2;
boolean altPin5;
boolean mitte = false;
boolean altPin8;
boolean mitte2 = false;
void setup()
{
pinMode(sleep, OUTPUT);
pinMode(sleep1, OUTPUT);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(dirpin1, OUTPUT);
pinMode(steppin1, OUTPUT);
Serial.begin(9600);
pinMode(cal_links, INPUT_PULLUP);
pinMode(cal_rechts, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP); // Anschluss Multiswitch Kanal 1
pinMode(buttonPin2, INPUT_PULLUP); // Anschluss Multiswitch Kanal 2
pinMode(buttonPin3, INPUT_PULLUP); // Anschluss Endschalter 1 Servo 1
pinMode(buttonPin4, INPUT_PULLUP); // Anschluss Endschalter 2 Servo 1
pinMode(buttonPin5, INPUT_PULLUP); // Anschluss Schalter Mitte Servo 1
pinMode(buttonPin6, INPUT_PULLUP); // Anschluss Endschalter 1 Servo 2
pinMode(buttonPin7, INPUT_PULLUP); // Anschluss Endschalter 2 Servo 2
pinMode(buttonPin8, INPUT_PULLUP); // Anschluss Schalter Mitte Servo 2
altPin1 = digitalRead(buttonPin1);
altPin2 = digitalRead(buttonPin2);
altPin5 = digitalRead(buttonPin5);
altPin8 = digitalRead(buttonPin8);
}
void loop()
{
// Stepper 1 und 2 parallel
if (digitalRead(buttonPin5) == LOW && altPin5 == HIGH)
{
mitte = false;
}
else if ((digitalRead(buttonPin1) == LOW && altPin1 == HIGH) || (digitalRead(buttonPin2) == LOW && altPin2 == HIGH))
{
mitte = true;
}
// Stepper 3 und 4 parallel
if (digitalRead(buttonPin8) == LOW && altPin8 == HIGH)
{
mitte2 = false;
}
else if ((digitalRead(buttonPin1) == LOW && altPin1 == HIGH) || (digitalRead(buttonPin2) == LOW && altPin2 == HIGH))
{
mitte2 = true;
}
altPin1 = digitalRead(buttonPin1);
altPin2 = digitalRead(buttonPin2);
altPin5 = digitalRead(buttonPin5);
altPin8 = digitalRead(buttonPin8);
// Stepper 1 + 2
if (digitalRead(buttonPin1) == LOW && digitalRead(buttonPin3) == HIGH && mitte)
{
Serial.println("Links drehen Stepper 1 + 2");
digitalWrite(sleep, HIGH);
digitalWrite(sleep1, HIGH);
rotate(50, 0.05); //The motor rotates 100 steps clockwise with a speed of 0.1 (slow)
}
else if (digitalRead(buttonPin2) == LOW && digitalRead(buttonPin4) == HIGH && mitte)
{
Serial.println("Rechts drehen Stepper 1 + 2");
digitalWrite(sleep, HIGH);
digitalWrite(sleep1, HIGH);
rotate(-50, 0.05); //The motor rotates 100 steps clockwise with a speed of 0.1 (slow)
}
else
{
Serial.println("Stop Stepper 1 + 2");
digitalWrite(sleep, LOW);
digitalWrite(sleep1, LOW);
}
// Kalibrierung links
if (digitalRead(cal_links) == LOW)
{
digitalWrite(sleep, HIGH);
digitalWrite(sleep1, LOW);
rotate(20, 0.02); //The motor rotates 100 steps clockwise with a speed of 0.1 (slow)
}
// Kalibrierung rechts
if (digitalRead(cal_rechts) == LOW)
{
digitalWrite(sleep, HIGH);
digitalWrite(sleep1, LOW);
rotate(-20, 0.02); //The motor rotates 100 steps clockwise with a speed of 0.1 (slow)
}
// Stepper 3 + 4
if (digitalRead(buttonPin1) == LOW && digitalRead(buttonPin6) == HIGH && mitte2)
{
Serial.println(" Links drehen Stepper 3 + 4");
//myservo1.writeMicroseconds(SV2_links);
}
else if (digitalRead(buttonPin2) == LOW && digitalRead(buttonPin7) == HIGH && mitte2)
{
Serial.println(" Rechts drehen Stepper 3 + 4");
//myservo1.writeMicroseconds(SV2_rechts);
}
else
{
Serial.println(" Stop Stepper 3 + 4");
}
}
/* Funktion 1
The rotate function turns the stepper motor. Tt accepts two arguments: 'steps' and 'speed'*/
void rotate(int steps, float speed){
/*This section looks at the 'steps' argument and stores 'HIGH' in the 'direction' variable if */
/*'steps' contains a positive number and 'LOW' if it contains a negative.*/
int direction;
if (steps > 0){
direction = HIGH;
}else{
direction = LOW;
}
speed = 1/speed * 70; //Calculating speed
steps = abs(steps); //Stores the absolute value of the content in 'steps' back into the 'steps' variable
digitalWrite(dirpin, direction); //Writes the direction (from our if statement above), to the EasyDriver DIR pin
/*Steppin'*/
for (int i = 0; i < steps; i++){
digitalWrite(steppin, HIGH);
delayMicroseconds(speed);
digitalWrite(steppin, LOW);
delayMicroseconds(speed);
}
}
/* Funktion 2
The rotate function turns the stepper motor. Tt accepts two arguments: 'steps' and 'speed'*/
void rotate1(int steps, float speed){
/*This section looks at the 'steps' argument and stores 'HIGH' in the 'direction' variable if */
/*'steps' contains a positive number and 'LOW' if it contains a negative.*/
int direction;
if (steps > 0){
direction = HIGH;
}else{
direction = LOW;
}
speed = 1/speed * 70; //Calculating speed
steps = abs(steps); //Stores the absolute value of the content in 'steps' back into the 'steps' variable
digitalWrite(dirpin1, direction); //Writes the direction (from our if statement above), to the EasyDriver DIR pin
/*Steppin'*/
for (int i = 0; i < steps; i++){
digitalWrite(steppin1, HIGH);
delayMicroseconds(speed);
digitalWrite(steppin1, LOW);
delayMicroseconds(speed);
}
}
