stepper bak
parent
b44bc5bd08
commit
462a69b68d
@ -0,0 +1,171 @@
|
||||
// Include the AccelStepper library:
|
||||
#include <AccelStepper.h>
|
||||
|
||||
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
|
||||
|
||||
// Set stepper 1 pins
|
||||
#define m1LimitNegPin 2
|
||||
#define m1LimitPosPin 3
|
||||
#define m1DirPin 4
|
||||
#define m1StepPin 5
|
||||
#define m1PowerPin 6
|
||||
|
||||
// Set stepper 2 pins
|
||||
#define m2LimitNegPin 9
|
||||
#define m2LimitPosPin 10
|
||||
#define m2DirPin 11
|
||||
#define m2StepPin 12
|
||||
#define m2PowerPin 13
|
||||
|
||||
#define motorInterfaceType 1
|
||||
|
||||
// Create a new instance of the AccelStepper class:
|
||||
AccelStepper m1Stepper = AccelStepper(motorInterfaceType, m1StepPin, m1DirPin);
|
||||
AccelStepper m2Stepper = AccelStepper(motorInterfaceType, m2StepPin, m2DirPin);
|
||||
|
||||
unsigned long previousMillis = 0;
|
||||
unsigned long currentMillis = 0;
|
||||
|
||||
void setup() {
|
||||
|
||||
pinMode(m1PowerPin, OUTPUT);
|
||||
pinMode(m1LimitNegPin, INPUT);
|
||||
pinMode(m1LimitPosPin, INPUT);
|
||||
|
||||
pinMode(m2PowerPin, OUTPUT);
|
||||
pinMode(m2LimitNegPin, INPUT);
|
||||
pinMode(m2LimitPosPin, INPUT);
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
// Set the maximum speed in steps per second:
|
||||
m1Stepper.setMaxSpeed(200);
|
||||
m1Stepper.setAcceleration(100);
|
||||
m1Stepper.setCurrentPosition(0);
|
||||
|
||||
m2Stepper.setMaxSpeed(200);
|
||||
m2Stepper.setAcceleration(100);
|
||||
m2Stepper.setCurrentPosition(0);
|
||||
}
|
||||
|
||||
|
||||
int integerValue=0;
|
||||
bool negativeNumber = false; // track if number is negative
|
||||
char incomingByte;
|
||||
|
||||
void loop() {
|
||||
|
||||
currentMillis = millis();
|
||||
|
||||
int m1EorNeg = digitalRead(m1LimitNegPin);
|
||||
int m1EorPos = digitalRead(m1LimitPosPin);
|
||||
|
||||
int m2EorNeg = digitalRead(m2LimitNegPin);
|
||||
int m2EorPos = digitalRead(m2LimitPosPin);
|
||||
|
||||
if (currentMillis - previousMillis >= 1000 == true ) {
|
||||
Serial.println("------Stepper 1------");
|
||||
Serial.print("m1EorPos:");
|
||||
Serial.println(m1EorNeg);
|
||||
Serial.print("m1EorNeg: ");
|
||||
Serial.println(m1EorPos);
|
||||
Serial.print("m1CurPos: ");
|
||||
Serial.println(m1Stepper.currentPosition() * -1);
|
||||
Serial.print("m1TarPos: ");
|
||||
Serial.println(m1Stepper.targetPosition() * -1);
|
||||
Serial.println("");
|
||||
|
||||
Serial.println("------Stepper 2------");
|
||||
Serial.print("m2EorPos: ");
|
||||
Serial.println(m2EorNeg);
|
||||
Serial.print("m2EorNeg: ");
|
||||
Serial.println(m2EorPos);
|
||||
Serial.print("m2CurPos: ");
|
||||
Serial.println(m2Stepper.currentPosition() * -1);
|
||||
Serial.print("m2TarPos: ");
|
||||
Serial.println(m2Stepper.targetPosition() * -1);
|
||||
Serial.println("");
|
||||
|
||||
previousMillis = currentMillis;
|
||||
}
|
||||
|
||||
// limit switch logic for stepper 1
|
||||
if ((m1EorNeg < m1EorPos) && (m1Stepper.targetPosition() > m1Stepper.currentPosition())) {
|
||||
m1Stepper.setSpeed(0);
|
||||
m1Stepper.moveTo(m1Stepper.currentPosition());
|
||||
digitalWrite(m1PowerPin, HIGH);
|
||||
} else if ((m1EorNeg > m1EorPos) && (m1Stepper.targetPosition() < m1Stepper.currentPosition())) {
|
||||
m1Stepper.setSpeed(0);
|
||||
m1Stepper.moveTo(m1Stepper.currentPosition());
|
||||
digitalWrite(m1PowerPin, HIGH);
|
||||
} else if (m1Stepper.targetPosition() == m1Stepper.currentPosition()) {
|
||||
digitalWrite(m1PowerPin, HIGH);
|
||||
} else {
|
||||
digitalWrite(m1PowerPin, LOW);
|
||||
m1Stepper.run();
|
||||
}
|
||||
|
||||
// limit switch logic for stepper 2
|
||||
if ((m2EorNeg < m2EorPos) && (m2Stepper.targetPosition() > m2Stepper.currentPosition())) {
|
||||
m2Stepper.setSpeed(0);
|
||||
m2Stepper.moveTo(m2Stepper.currentPosition());
|
||||
digitalWrite(m2PowerPin, HIGH);
|
||||
} else if ((m2EorNeg > m2EorPos) && (m2Stepper.targetPosition() < m2Stepper.currentPosition())) {
|
||||
m2Stepper.setSpeed(0);
|
||||
m2Stepper.moveTo(m1Stepper.currentPosition());
|
||||
digitalWrite(m2PowerPin, HIGH);
|
||||
} else if (m2Stepper.targetPosition() == m2Stepper.currentPosition()) {
|
||||
digitalWrite(m2PowerPin, HIGH);
|
||||
} else {
|
||||
digitalWrite(m2PowerPin, LOW);
|
||||
m2Stepper.run();
|
||||
}
|
||||
|
||||
if (Serial.available() > 0) { // something came across serial
|
||||
integerValue = 0; // throw away previous integerValue
|
||||
negativeNumber = false; // reset for negative
|
||||
|
||||
while(1) { // force into a loop until 'n' is received
|
||||
incomingByte = Serial.read();
|
||||
if (incomingByte == ' ') break; // exit the while(1), we're done receiving
|
||||
if (incomingByte == -1) continue; // if no characters are in the buffer read() returns -1
|
||||
if (incomingByte == '-') {
|
||||
negativeNumber = true;
|
||||
continue;
|
||||
}
|
||||
integerValue *= 10; // shift left 1 decimal place
|
||||
integerValue = ((incomingByte - 48) + integerValue); // convert ASCII to integer, add, and shift left 1 decimal place
|
||||
}
|
||||
|
||||
if (negativeNumber)
|
||||
integerValue = -integerValue;
|
||||
|
||||
integerValue = -integerValue; // this makes up for the fact that things are backwards
|
||||
m1Stepper.moveTo(integerValue);
|
||||
|
||||
|
||||
integerValue = 0; // throw away previous integerValue
|
||||
negativeNumber = false; // reset for negative
|
||||
|
||||
while(1) { // force into a loop until 'n' is received
|
||||
incomingByte = Serial.read();
|
||||
if (incomingByte == '\n') break; // exit the while(1), we're done receiving
|
||||
if (incomingByte == -1) continue; // if no characters are in the buffer read() returns -1
|
||||
if (incomingByte == '-') {
|
||||
negativeNumber = true;
|
||||
continue;
|
||||
}
|
||||
integerValue *= 10; // shift left 1 decimal place
|
||||
integerValue = ((incomingByte - 48) + integerValue); // convert ASCII to integer, add, and shift left 1 decimal place
|
||||
}
|
||||
|
||||
if (negativeNumber)
|
||||
integerValue = -integerValue;
|
||||
|
||||
integerValue = -integerValue; // this makes up for the fact that things are backwards
|
||||
m2Stepper.moveTo(integerValue);
|
||||
|
||||
}
|
||||
|
||||
//delay(100);
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue