// 马达运动方向控制通用副程式(for 11/28)
// axis_Dir: 轴选择,x~z_state: false(未激活),symbol: '+' or '-'(马达预设方向)
// 使用例: motion_DIR(axis_Dir, x_state, y_state, z_state, '+');
void motion_DIR(int axis_Dir, bool x_state, bool y_state, bool z_state, char symbol) {
int DIR_result = -1;
// X-axis
if (axis_Dir == 5) {
DIR_result = (x_state == false && symbol == '+') ? HIGH :
(x_state == false && symbol == '-') ? LOW :
(x_state == true && symbol == '+') ? LOW :
(x_state == true && symbol == '-') ? HIGH :
-1;
}
// Y-axis
else if (axis_Dir == 6) {
DIR_result = (y_state == false && symbol == '+') ? HIGH :
(y_state == false && symbol == '-') ? LOW :
(y_state == true && symbol == '+') ? LOW :
(y_state == true && symbol == '-') ? HIGH :
-1;
}
// Z-axis
else if (axis_Dir == 7) {
DIR_result = (z_state == false && symbol == '+') ? HIGH :
(z_state == false && symbol == '-') ? LOW :
(z_state == true && symbol == '+') ? LOW :
(z_state == true && symbol == '-') ? HIGH :
-1;
}
Serial.print("axis_Dir: ");
Serial.print(axis_Dir);
Serial.print(", DIR_result: ");
Serial.println((DIR_result == HIGH) ? "HIGH" : (DIR_result == LOW) ? "LOW" : "None");
if (DIR_result != -1) {
Serial.println((DIR_result == HIGH) ? "HIGH" : "LOW");
digitalWrite(axis_Dir, DIR_result);
} else {
Serial.println("None");
}
}
void setup() {
// 設置引腳模式和串行通訊
int axis_Dir_X = 5; // 假設引腳 5 控制 X 軸
int axis_Dir_Y = 6; // 假設引腳 6 控制 Y 軸
int axis_Dir_Z = 7; // 假設引腳 7 控制 Z 軸
bool x_state = false, y_state = true, z_state = false;
pinMode(axis_Dir_X, OUTPUT);
pinMode(axis_Dir_Y, OUTPUT);
pinMode(axis_Dir_Z, OUTPUT);
Serial.begin(9600);
// 調用函數來測試不同的組合
motion_DIR(axis_Dir_X, x_state, y_state, z_state, '+');
motion_DIR(axis_Dir_Y, x_state, y_state, z_state, '-');
motion_DIR(axis_Dir_Z, x_state, y_state, z_state, '+');
//motion_DIR(axis_Dir_X, false, true, true, '+');
//motion_DIR(axis_Dir_Y, true, false, true, '-');
//motion_DIR(axis_Dir_Z, true, true, false, '+');
}
void loop() {
// 主循環中不做任何事
}