const int xDirPin = 2; const int xStepPin = 3;
const int yDirPin = 4; const int yStepPin = 5;
const int zDirPin = 6; const int zStepPin = 7;
const int enable = 8;
const int xLimitPin = 9;
const int yLimitPin = 10;
const int zLimitPin = 11;
long xPos = 0;
long yPos = 0;
long zPos = 0;
const long xMin = 0;
const long xMax = 10000;
const long yMin = 0;
const long yMax = 10000;
const long zMin = 0;
const long zMax = 10000;
const int fastDelay = 1000;
const int slowDelay = 3000;
bool isHomed = false;
void moveAxis(char axis, int steps);
void moveMotor(int dirPin, int stepPin, int steps, int delay);
void homeX();
void homeY();
void homeZ();
void setup() {
Serial.begin(9600);
Serial.println("Arduino is ready.");
pinMode(xDirPin, OUTPUT);
pinMode(xStepPin, OUTPUT);
pinMode(yDirPin, OUTPUT);
pinMode(yStepPin, OUTPUT);
pinMode(zDirPin, OUTPUT);
pinMode(zStepPin, OUTPUT);
pinMode(enable, OUTPUT);
pinMode(xLimitPin, INPUT_PULLUP);
pinMode(yLimitPin, INPUT_PULLUP);
pinMode(zLimitPin, INPUT_PULLUP);
digitalWrite(enable, LOW);
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
input.trim();
if (input == "home" || input == "HOME") {
homeZ();
homeX();
homeY();
isHomed = true;
Serial.print("Pos: ");
Serial.print(xPos);
Serial.print(", ");
Serial.print(yPos);
Serial.print(", ");
Serial.println(zPos);
return;
}
if (input.length() < 2) {
Serial.println("Invalid command.");
return;
}
char axis = input.charAt(0);
String value = input.substring(1);
int steps = value.toInt();
if (steps == 0 && value != "0") {
Serial.println("Invalid number.");
return;
}
if (!isHomed) {
Serial.println("ERROR : Please home first.");
return;
}
if (axis == 'x' || axis == 'X' ||
axis == 'y' || axis == 'Y' ||
axis == 'z' || axis == 'Z') {
moveAxis(axis, steps);
}
else {
Serial.println("Unknown axis.");
return;
}
Serial.print("Your command is ");
Serial.print(axis);
Serial.println(steps);
Serial.print("Pos: ");
Serial.print(xPos);
Serial.print(", ");
Serial.print(yPos);
Serial.print(", ");
Serial.println(zPos);
}
}
void moveAxis(char axis, int steps) {
if (axis == 'x' || axis == 'X') {
long targetX = xPos + steps;
if (targetX < xMin || targetX > xMax) {
Serial.println("ERROR : X axis limit reached.");
return;
}
moveMotor(xDirPin, xStepPin, steps, fastDelay);
xPos = targetX;
}
else if (axis == 'y' || axis == 'Y') {
long targetY = yPos + steps;
if (targetY < yMin ||targetY > yMax) {
Serial.println("ERROR : Y axis limit reached.");
return;
}
moveMotor(yDirPin, yStepPin, steps, fastDelay);
yPos = targetY;
}
else if (axis == 'z' || axis == 'Z') {
long targetZ = zPos + steps;
if (targetZ < zMin || targetZ > zMax) {
Serial.println("ERROR : Z axis limit reached.");
return;
}
moveMotor(zDirPin, zStepPin, steps, fastDelay);
zPos = targetZ;
}
}
void moveMotor(int dirPin, int stepPin, int steps, int delay) {
if (steps == 0) {
return;
}
if (steps > 0) {
digitalWrite(dirPin, HIGH);
}
else {
digitalWrite(dirPin, LOW);
}
int step = abs(steps);
for (int i = 0; i < step; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(delay);
digitalWrite(stepPin, LOW);
delayMicroseconds(delay);
}
}
void homeX() {
Serial.println("Homing X...");
while(digitalRead(xLimitPin) == HIGH) {
moveMotor(xDirPin, xStepPin, -1, slowDelay);
}
xPos = 0;
Serial.println("X is home.");
}
void homeY() {
Serial.println("Homing Y...");
while(digitalRead(yLimitPin) == HIGH) {
moveMotor(yDirPin, yStepPin, -1, slowDelay);
}
yPos = 0;
Serial.println("Y is home.");
}
void homeZ() {
Serial.println("Homing Z...");
while(digitalRead(zLimitPin) == HIGH) {
moveMotor(zDirPin, zStepPin, -1, slowDelay);
}
zPos = 0;
Serial.println("Z is home.");
}