Arduino/StepperTest/StepperTest.ino

132 lines
3.3 KiB
Arduino
Raw Normal View History

2022-10-20 16:14:30 +02:00
#include <AccelStepper.h>
2022-10-20 18:15:39 +02:00
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver
#define motorInterfaceType 1
2022-10-20 16:14:30 +02:00
#define dirPin 2
#define stepPin 3
2022-10-20 18:15:39 +02:00
// Stepper motor speed configuration
2022-10-20 16:14:30 +02:00
#define microStepsFactor 4
2022-10-20 19:59:11 +02:00
#define maxStepperSpeed 4000
#define inversionPause 50
2022-10-20 16:14:30 +02:00
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
2022-10-20 18:15:39 +02:00
// Runtime data
int currentPosition[] = {-1, -1, -1};
int nextPosition[] = {0, 0, 0};
2022-10-20 19:59:11 +02:00
int stepperPosition = 0;
2022-10-20 16:14:30 +02:00
void setup() {
2022-10-20 18:15:39 +02:00
Serial.begin(115200);
// Wait for zeroing
Serial.println("Please rotate to zero position and press Enter");
while (Serial.available() == 0) {}
Serial.flush();
2022-10-20 16:14:30 +02:00
2022-10-20 18:15:39 +02:00
// Set the current position to 0:
stepper.setCurrentPosition(0);
// Set the maximum speed in steps per second:
stepper.setMaxSpeed(maxStepperSpeed);
2022-10-20 19:59:11 +02:00
stepper.setAcceleration(40000);
2022-10-20 18:15:39 +02:00
// Rotate to 0,0,0
setPosition();
2022-10-20 16:14:30 +02:00
}
void loop() {
2022-10-20 18:15:39 +02:00
incrementCombination();
setPosition();
delay(inversionPause);
}
void incrementCombination() {
if (currentPosition[0] < 99) {
nextPosition[0] = currentPosition[0] + 1;
nextPosition[1] = currentPosition[1];
nextPosition[2] = currentPosition[2];
} else {
nextPosition[0] = 0;
if (currentPosition[1] < 99) {
nextPosition[1] = currentPosition[1] + 1;
nextPosition[2] = currentPosition[2];
} else {
nextPosition[1] = 0;
if (currentPosition[2] < 99) {
nextPosition[2] = currentPosition[2] + 1;
} else {
nextPosition[2] = 0;
}
}
2022-10-20 16:14:30 +02:00
}
2022-10-20 18:15:39 +02:00
}
void setPosition() {
Serial.print("Testing ");
Serial.print(currentPosition[0]);
Serial.print(",");
Serial.print(currentPosition[1]);
Serial.print(",");
Serial.println(currentPosition[2]);
2022-10-20 19:59:11 +02:00
if (currentPosition[2] != nextPosition[2]) {
2022-10-20 20:28:57 +02:00
rotateCcwTo(nextPosition[2], false);
rotateCcwTo(nextPosition[2], true);
rotateCcwTo(nextPosition[2], true);
rotateCcwTo(nextPosition[2], true);
currentPosition[1] = -1;
2022-10-20 18:15:39 +02:00
}
2022-10-20 19:59:11 +02:00
if (currentPosition[1] != nextPosition[1]) {
2022-10-20 20:28:57 +02:00
rotateCwTo(nextPosition[1], false);
rotateCwTo(nextPosition[1], true);
rotateCwTo(nextPosition[1], true);
currentPosition[0] = -1;
2022-10-20 18:15:39 +02:00
}
2022-10-20 19:59:11 +02:00
if (currentPosition[0] != nextPosition[0]) {
2022-10-20 20:28:57 +02:00
rotateCcwTo(nextPosition[0], false);
rotateCcwTo(nextPosition[0], true);
2022-10-20 18:15:39 +02:00
}
2022-10-20 20:28:57 +02:00
if (stepperPosition <= 10) {
rotateCwTo(0, true);
}
rotateCwTo(10, false);
2022-10-20 18:15:39 +02:00
currentPosition[0] = nextPosition[0];
currentPosition[1] = nextPosition[1];
currentPosition[2] = nextPosition[2];
}
2022-10-20 20:28:57 +02:00
void rotateCwTo(int pos, boolean forceFullRotation) {
2022-10-20 19:59:11 +02:00
Serial.print("CW to ");
Serial.println(pos);
2022-10-20 20:28:57 +02:00
if (stepperPosition == pos && forceFullRotation) {
2022-10-20 19:59:11 +02:00
// make sure we actually move one whole turn
stepper.move(-200 * microStepsFactor);
stepper.runToPosition();
} else {
stepper.runToNewPosition(-2 * pos * microStepsFactor);
stepperPosition = pos;
}
2022-10-20 18:15:39 +02:00
}
2022-10-20 16:14:30 +02:00
2022-10-20 20:28:57 +02:00
void rotateCcwTo(int pos, boolean forceFullRotation) {
2022-10-20 19:59:11 +02:00
Serial.print("CCW to ");
Serial.println(pos);
2022-10-20 20:28:57 +02:00
if (stepperPosition == pos && forceFullRotation) {
2022-10-20 19:59:11 +02:00
// make sure we actually move one whole turn
stepper.move(200 * microStepsFactor);
stepper.runToPosition();
} else {
stepper.runToNewPosition(2 * pos * microStepsFactor);
stepperPosition = pos;
}
2022-10-20 16:14:30 +02:00
}