/* 更新日志: for 2024/12/01.
- 程式架构重构(for 12/1 ~ ?)
- 程式注解清晰化(for 12/1 ~ ?)
- 模式T更名,开发者模式 -> 设置模式(for 12/1 ~ ?)
- 设置模式(mode_T)进入位置更动(for 12/1 ~ 12/2)
- 模式B返回键灵敏化(for 12/1 ~ ?)
- 设置模式更动(for 12/1 ~ ?)
*/
/* Library - 函式库 */
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Joystick.h>
/* Changelog - 版本日志 */
const String updated_Version = "New_mode-V2.5";
const String updated_Date = "2024/12/15";
/* Arduino Pin_setup - 脚位设置 */
// Button Pin - 触发按键 (joy: 搖桿, limit: 限位開關
const int joyX = A6, joyY = A7, joyButton = 50;
const int xlimitPin = 52, ylimitPin = 12, zlimitPin = 13;
// RGB LED - 灯号设置 (R: 红, G: 绿, B: 蓝
const int R_LED = 14, G_LED = 15, B_LED = 16;
// Stepper motor Pin - 步进马达脚位设置 (en:致能, step:步, dir:轉向
const int enablePin = 8;
const int xstepPin = 2, ystepPin = 3, zstepPin = 4;
const int xdirPin = 5, ydirPin = 6, zdirPin = 7;
/* Array setup - 阵列设置 */
// Keypad set - 键盘设置 (Rows(列) -, Cols(行) |
const byte ROWS = 6;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'s', ' ', ' ', ' '}, // 's': 確認/鎖定
{'<', ' ', ' ', ' '}, // '<': 左移/減
{'>', ' ', ' ', ' '}, // '>': 右移/增
{' ', 'o', ' ', ' '}, // 'o': 馬達啟動/繼續
{' ', ' ', 'm', ' '}, // 'm': 模式選擇/返回
{' ', ' ', ' ', 'p'} // 'p': 设置模式
};
byte rowPins[ROWS] = {A12, A13, A14, A8, A10, 23};
byte colPins[COLS] = {A15, A9, A11, 25};
// list set - 列表设置
const int B_listSize = 24; // 陣列大小(mode_B, E)
const int D_listSize = 24; // 陣列大小(mode_D)
long B_x_axis[B_listSize]; // X軸點位存儲(mode_B)
long B_y_axis[B_listSize]; // Y軸點位存儲(mode_B)
long D_x_axis[D_listSize]; // X軸點位存儲(mode_D)
long D_y_axis[D_listSize]; // Y軸點位存儲(mode_D)
long D_x_axis_01[B_listSize]; // X軸點位存儲(mode_D)
long D_y_axis_01[B_listSize]; // Y軸點位存儲(mode_D)
long E_x_axis[B_listSize]; // X軸點位存儲(mode_E)
long E_y_axis[B_listSize]; // Y軸點位存儲(mode_E)
/* Global variable - 全域变数宣告 */
// Judgment with variables - 全域判别變數
int modeChange = 0; // 模式切换
int numlocation = 0; // 變更數字位置(mode_A, C, D)
int button = 0; // 搖桿按鈕(mode_B)
bool modeReturn = false; // 模式返回
bool modeStart = false; // 馬達啟用
bool D_modeStart = false; // 馬達啟用(mode_D)
bool online = false; // 線上/線下
bool xlimit = false; // x軸限位确认
bool ylimit = false; // Y軸限位确认
bool zlimit = false; // Z軸限位确认
bool xlimitJudge = false; // Y軸限位狀態
bool ylimitJudge = false; // Y軸限位狀態
bool zlimitJudge = false; // Z軸限位狀態
// mode variables - 模式變數
bool A_Correction = false;
bool B_Manual = false;
bool C_Semi_automatic = false;
bool D_Automatic = false;
bool E_Point_custom = false;
int T_mode = 0; // 模式T内部切换
bool T_return = false; // 模式T返回函数
bool Again = false; // 重复执行
/* Numerical storage - 數值存儲 */
// General - 全域变数
char words; // 线上用读取
long x_coordinate = 0; // X軸座標
long y_coordinate = 0; // Y軸座標
long xmoveSteps = 0; // X軸步數
long ymoveSteps = 0; // Y軸步數
int motorDelay = 400; // 步數延遲
int delaytime = 0; // 延遲時間(mode_E)
int fast_setmode = 0; // 快速设置模式
bool potential = false; // 电位设置
char xsymbol = '.', ysymbol = '.';
int xnum_01 = 0, xnum_02 = 0, xnum_03 = 0, xnum_04 = 0;
int ynum_01 = 0, ynum_02 = 0, ynum_03 = 0, ynum_04 = 0;
bool x_state = false, y_state = false, z_state = false;
// mode_A - 模式A专用
int Anum_01 = 1;
int Anum_02 = 0;
int Anum_03 = 0;
// mode_B - 模式B专用
int B_i = 1;
bool B_display = false; // 状态面板显示
// mode_D - 模式D专用
int D_i = 0; // list使用
int Dnum_01 = 0;
int Dnum_02 = 0;
int Dnum_03 = 0;
// mode_E - 模式E专用
int Enum_01 = 1; // 累計執行次數
int Enum_02 = 0; // 存儲秒數(十位數)
int Enum_03 = 0; // 存儲秒數(個位數)
int Enum_04 = 0; // 重複執行存儲次數
int Enum_05 = 0; // 延遲時間存儲
// mode_T - 模式T专用
int T_Developer = 0; // 设置模式限位开关
int limitenable = 0; // 限位模式參數
int delayenable = 0; // 延遲參數
int state_enable = 0; // 马达正反转参数
/* set Library module - 函式库启用设置 */
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Joystick myJoystick(joyX, joyY, joyButton);
LiquidCrystal_I2C lcd(0x27, 20, 4);
/* Initial setup - 初始設定 */
void setup() {
// set motor Pin
pinMode(enablePin, OUTPUT);
pinMode(xdirPin, OUTPUT);
pinMode(ydirPin, OUTPUT);
pinMode(zdirPin, OUTPUT);
pinMode(xstepPin, OUTPUT);
pinMode(ystepPin, OUTPUT);
pinMode(zstepPin, OUTPUT);
digitalWrite(enablePin, LOW);
// set RGB LED
pinMode(B_LED, OUTPUT);
pinMode(G_LED, OUTPUT);
pinMode(R_LED, OUTPUT);
digitalWrite(B_LED, LOW);
digitalWrite(G_LED, LOW);
digitalWrite(R_LED, HIGH);
// set Joystick
pinMode(joyX, INPUT);
pinMode(joyX, INPUT);
pinMode(joyButton, INPUT);
// set limit
pinMode(xlimitPin, INPUT);
pinMode(ylimitPin, INPUT);
pinMode(zlimitPin, INPUT);
// set LCD
lcd.init();
lcd.backlight();
// begin
Serial.begin(9600);
Serial1.begin(9600);
lcd.setCursor(0, 0);
lcd.print("Welcome using,");
lcd.setCursor(17, 0);
lcd.print("O:");
lcd.setCursor(19, 0);
lcd.print("L");
lcd.setCursor(0, 1);
lcd.print("> mode_A <");
lcd.setCursor(2, 2);
lcd.print("mode_B");
lcd.setCursor(12, 1);
lcd.print("mode_C");
lcd.setCursor(12, 2);
lcd.print("mode_D");
lcd.setCursor(6, 3);
lcd.print("|");
lcd.setCursor(8, 3);
lcd.print("mode");
lcd.setCursor(13, 3);
lcd.print("|");
lcd.setCursor(14, 3);
lcd.print(" setup");
// 初始状态(串口)显示
Serial.print("# 歡迎使用步進控制器-線上串口通信模式,當前系統版本為: ");
Serial.println(updated_Version);
Serial1.print("# 歡迎使用步進控制器-線上串口通信模式,當前系統版本為: New_mode-V2.5");
Serial1.println(updated_Version);
/*Serial.println("# 此為線上模式專用串口通信,請輸入字母以進入模式(A~V):");
Serial1.println("# 此為線上模式專用串口通信,請輸入字母以進入模式(A~V):");*/
}
void loop() {
// Arduino窗口讀取
if (Serial.available() > 0) {
// 模式讀取
char modeChoose = Serial.read();
Serial.print("- 當前使用的串口為\'Serial port-0\',當前模式選擇為:");
Serial.println(modeChoose);
mode_online(modeChoose);
} else if (Serial1.available() > 0) {
// 模式讀取
char modeChoose = Serial1.read();
Serial1.print("- 當前使用的串口為\'Serial port-1\',當前模式選擇為:");
Serial1.println(modeChoose);
mode_online(modeChoose);
}
// 實體按鍵讀取
char keys = keypad.getKey();
if (keys != NO_KEY) {
mode_Offline(keys);
}
}
/* LCD显示通用副程式 - LCD_Display (for 11/01) */
// mode_display(显示模式), Rows(列) -, Cols(行) |, str(字串), num(整数)
// 使用例 # LCD_Display('A', 0, 0, "str", NULL);
void LCD_Display(char mode_display, int Rows, int Cols, String str, long num) {
// LCD文字显示,通用格式# LCD_Display('A', 0, 0, "str", NULL);
if (mode_display == 'A') {
lcd.setCursor(Rows, Cols);
lcd.print(str);
}
// LCD数字显示,通用格式# LCD_Display('B', 0, 0, "", num);
else if (mode_display == 'B') {
lcd.setCursor(Rows, Cols);
lcd.print(num);
}
// LCD进阶显示,通用格式# LCD_Display('C', 0, 0, "str", num);
else if (mode_display == 'C') {
lcd.setCursor(Rows, Cols);
lcd.print(str);
lcd.print(num);
}
// LCD自定义单行清除,通用格式# LCD_Display('D', 0, 0, "", NULL);
else if (mode_display == 'D') {
lcd.setCursor(Rows, Cols);
if (0 < num && num <= 20) {
for (int i = Rows; i < num; i++)
lcd.print(" ");
} else {
for (int i = Rows; i < 20; i++)
lcd.print(" ");
}
}
// LCD多行清除,通用格式# LCD_Display('E', 0, 1, "", NULL);
else if (mode_display == 'E') {
for (int i = Cols; i < 3; i++) {
lcd.setCursor(Rows, i);
for (int j = Rows; j < 20; j++)
lcd.print(" ");
}
}
// LCD步數显示(单位至多十万),通用格式# LCD_Display('F', 0, 2, "X: ", num);
else if (mode_display == 'F') {
lcd.setCursor(Rows, Cols);
int length = str.length();
lcd.print(str);
int digits = (num == 0) ? 6 : log10(num) + 1;
int position = Rows + length + (6 - digits);
lcd.setCursor(position, Cols);
if (num == 0) {
for (int i = 0; i < 6; i++)
lcd.print("0");
} else
lcd.print(num);
}
}
// 模式切換
void selectedMode(String mode, int ROWS_location, int COLS_location) {
if (mode == "mode_A" || mode == "mode_E") {
lcd.setCursor(ROWS_location - 2, COLS_location);
lcd.print(">");
lcd.setCursor(ROWS_location + 7, COLS_location);
lcd.print("<");
} else if (mode == "mode_B" || mode == "mode_D" || mode == "mode_T") {
lcd.setCursor(ROWS_location - 2, COLS_location - 1);
lcd.print(" ");
lcd.setCursor(ROWS_location + 7, COLS_location - 1);
lcd.print(" ");
lcd.setCursor(ROWS_location - 2, COLS_location);
lcd.print(">");
lcd.setCursor(ROWS_location + 7, COLS_location);
lcd.print("<");
} else if (mode == "mode_C" || mode == "mode_V") {
lcd.setCursor(ROWS_location - 12, COLS_location + 1);
lcd.print(" ");
lcd.setCursor(ROWS_location - 3, COLS_location + 1);
lcd.print(" ");
lcd.setCursor(ROWS_location - 2, COLS_location);
lcd.print(">");
lcd.setCursor(ROWS_location + 7, COLS_location);
lcd.print("<");
}
lcd.setCursor(ROWS_location, COLS_location);
lcd.print(" ");
delay(150);
lcd.setCursor(ROWS_location, COLS_location);
lcd.print(mode);
delay(300);
}
// 模式顯示
void modeLCD_display(String mode, int ROWS_location, int COLS_location, bool online, String display_01, String display_02, String display_03) {
// state display
LCD_Display('A', 0, 3, display_01, NULL);
LCD_Display('A', 6, 3, "|", NULL);
LCD_Display('A', 7, 3, display_02, NULL);
LCD_Display('A', 13, 3, "|", NULL);
LCD_Display('A', 14, 3, display_03, NULL);
// online display
if (online == true) {
LCD_Display('A', 17, 0, "O:", NULL);
LCD_Display('A', 19, 0, "H", NULL);
} else {
LCD_Display('A', 17, 0, "O:", NULL);
LCD_Display('A', 19, 0, "L", NULL);
}
// mode display
if (mode == "mode_A") {
LCD_Display('A', 0, 0, "Welcome using,", NULL);
LCD_Display('A', 2, 2, "mode_B", NULL);
LCD_Display('A', 12, 1, "mode_C", NULL);
LCD_Display('A', 12, 2, "mode_D", NULL);
selectedMode("mode_A", 2, 1);
} else if (mode == "mode_B") {
LCD_Display('A', 0, 0, "Welcome using,", NULL);
LCD_Display('A', 2, 1, "mode_A", NULL);
LCD_Display('A', 12, 1, "mode_C", NULL);
LCD_Display('A', 12, 2, "mode_D", NULL);
selectedMode("mode_B", 2, 2);
} else if (mode == "mode_C") {
LCD_Display('A', 0, 0, "Welcome using,", NULL);
LCD_Display('A', 2, 1, "mode_A", NULL);
LCD_Display('A', 2, 2, "mode_B", NULL);
LCD_Display('A', 12, 2, "mode_D", NULL);
selectedMode("mode_C", 12, 1);
} else if (mode == "mode_D") {
LCD_Display('A', 0, 0, "Welcome using,", NULL);
LCD_Display('A', 2, 1, "mode_A", NULL);
LCD_Display('A', 2, 2, "mode_B", NULL);
LCD_Display('A', 12, 1, "mode_C", NULL);
selectedMode("mode_D", 12, 2);
} else if (mode == "mode_E") {
LCD_Display('A', 0, 0, "Welcome using,", NULL);
LCD_Display('A', 2, 2, "mode_T", NULL);
LCD_Display('A', 12, 1, "mode_V", NULL);
selectedMode("mode_E", 2, 1);
} else if (mode == "mode_T") {
LCD_Display('A', 0, 0, "Welcome using,", NULL);
LCD_Display('A', 2, 1, "mode_E", NULL);
LCD_Display('A', 12, 1, "mode_V", NULL);
selectedMode("mode_T", 2, 2);
} else if (mode == "mode_V") {
LCD_Display('A', 0, 0, "Welcome using,", NULL);
LCD_Display('A', 2, 1, "mode_E", NULL);
LCD_Display('A', 2, 2, "mode_T", NULL);
selectedMode("mode_V", 12, 1);
}
}
/* 马达运动方向控制通用副程式 - motion_DIR (for 11/28) */
// axis_Dir: 预设轴选择,x~z_state: false(未激活),symbol: '+' or '-'(预设方向)
// 使用例 # motion_DIR(xdirPin, 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");
}
}
/* 马达校正次数/模式通用副程式 - Correction_procedure (for 12/1) */
// frequency: 校正次數, potential: 电位, T_Developer: 校正模式
// 使用例 # Correction_procedure(1);
void Correction_procedure(int frequency) {
// 电位判别,如果 potential == true 则反向判别
int limit_potential = (potential == true) ? 1 : 0;
// 常规校正,T_Developer == None, A_Correction == true 给予通行
if (T_Developer == 0 || A_Correction == true) {
digitalWrite(B_LED, HIGH);
digitalWrite(R_LED, LOW);
while (frequency > 0) {
// X-axis
if (!xlimitJudge && digitalRead(xlimitPin) == limit_potential) {
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(xlimitPin) == !limit_potential && xlimit == false) {
xlimitJudge = true;
xlimit = true;
Serial.println("~ Xstop已觸發");
Serial1.println("~ Xstop已觸發");
}
// Y-axis
if (!ylimitJudge && digitalRead(ylimitPin) == limit_potential) {
digitalWrite(ystepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(ylimitPin) == !limit_potential && ylimit == false) {
ylimitJudge = true;
ylimit = true;
Serial.println("~ Ystop已觸發");
Serial1.println("~ Ystop已觸發");
}
// Z-axis
if (!zlimitJudge && digitalRead(zlimitPin) == limit_potential) {
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(zlimitPin) == !limit_potential && zlimit == false) {
zlimitJudge = true;
zlimit = true;
Serial.println("~ Zstop已觸發");
Serial1.println("~ Zstop已觸發");
}
// 倘若都触发,则反向
if (xlimitJudge && ylimitJudge && zlimitJudge) {
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
for (int i = 0; i < 2500; i++) {
digitalWrite(xstepPin, HIGH);
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(zstepPin, LOW);
digitalWrite(ystepPin, LOW);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
// 每步間隔
delayMicroseconds(motorDelay);
}
xlimitJudge = false; ylimitJudge = false; zlimitJudge = false;
xlimit = false; ylimit = false; zlimit = false;
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
frequency--;
if (frequency == 0)
break;
}
delayMicroseconds(motorDelay);
}
digitalWrite(B_LED, LOW);
digitalWrite(R_LED, HIGH);
}
// 不进行校正,仅 T_Developer == 1 下通行
// X单轴校正,仅 T_Developer == 2 下通行
else if (T_Developer == 2) {
digitalWrite(B_LED, HIGH);
digitalWrite(R_LED, LOW);
while (frequency > 0) {
// X-axis
if (!xlimitJudge && digitalRead(xlimitPin) == limit_potential) {
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(xlimitPin) == !limit_potential && xlimit == false) {
xlimitJudge = true;
xlimit = true;
Serial.println("~ Xstop已觸發");
Serial1.println("~ Xstop已觸發");
}
// 倘若触发,则反向
if (xlimitJudge) {
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
for (int i = 0; i < 2500; i++) {
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
// 每步間隔
delayMicroseconds(motorDelay);
}
xlimitJudge = false;
xlimit = false;
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
frequency--;
if (frequency == 0)
break;
}
delayMicroseconds(motorDelay);
}
digitalWrite(B_LED, LOW);
digitalWrite(R_LED, HIGH);
}
// Y单轴校正,仅 T_Developer == 3 下通行
else if (T_Developer == 3) {
digitalWrite(B_LED, HIGH);
digitalWrite(R_LED, LOW);
while (frequency > 0) {
// Y-axis
if (!ylimitJudge && digitalRead(ylimitPin) == limit_potential) {
digitalWrite(ystepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(ylimitPin) == !limit_potential && ylimit == false) {
ylimitJudge = true;
ylimit = true;
Serial.println("~ Ystop已觸發");
Serial1.println("~ Ystop已觸發");
}
// 倘若触发,则反向
if (ylimitJudge) {
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
for (int i = 0; i < 2500; i++) {
digitalWrite(ystepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
delayMicroseconds(10);
// 每步間隔
delayMicroseconds(motorDelay);
}
ylimitJudge = false;
ylimit = false;
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
frequency--;
if (frequency == 0)
break;
}
delayMicroseconds(motorDelay);
}
digitalWrite(B_LED, LOW);
digitalWrite(R_LED, HIGH);
}
// Z单轴校正,仅 T_Developer == 4 下通行
else if (T_Developer == 4) {
digitalWrite(B_LED, HIGH);
digitalWrite(R_LED, LOW);
while (frequency > 0) {
// Z-axis
if (!zlimitJudge && digitalRead(zlimitPin) == limit_potential) {
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(zlimitPin) == !limit_potential && zlimit == false) {
zlimitJudge = true;
zlimit = true;
Serial.println("~ Zstop已觸發");
Serial1.println("~ Zstop已觸發");
}
// 倘若触发,则反向
if (zlimitJudge) {
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
for (int i = 0; i < 2500; i++) {
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
// 每步間隔
delayMicroseconds(motorDelay);
}
zlimitJudge = false;
zlimit = false;
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
frequency--;
if (frequency == 0)
break;
}
delayMicroseconds(motorDelay);
}
digitalWrite(B_LED, LOW);
digitalWrite(R_LED, HIGH);
}
// XY双轴校正,仅 T_Developer == 5 下通行
else if (T_Developer == 5) {
digitalWrite(B_LED, HIGH);
digitalWrite(R_LED, LOW);
while (frequency > 0) {
// X-axis
if (!xlimitJudge && digitalRead(xlimitPin) == limit_potential) {
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(xlimitPin) == !limit_potential && xlimit == false) {
xlimitJudge = true;
xlimit = true;
Serial.println("~ Xstop已觸發");
Serial1.println("~ Xstop已觸發");
}
// Y-axis
if (!ylimitJudge && digitalRead(ylimitPin) == limit_potential) {
digitalWrite(ystepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(ylimitPin) == !limit_potential && ylimit == false) {
ylimitJudge = true;
ylimit = true;
Serial.println("~ Ystop已觸發");
Serial1.println("~ Ystop已觸發");
}
// 倘若都触发,则反向
if (xlimitJudge && ylimitJudge) {
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
for (int i = 0; i < 2500; i++) {
digitalWrite(xstepPin, HIGH);
digitalWrite(ystepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
digitalWrite(ystepPin, LOW);
delayMicroseconds(10);
// 每步間隔
delayMicroseconds(motorDelay);
}
xlimitJudge = false; ylimitJudge = false;
xlimit = false; ylimit = false;
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
frequency--;
if (frequency == 0)
break;
}
delayMicroseconds(motorDelay);
}
digitalWrite(B_LED, LOW);
digitalWrite(R_LED, HIGH);
}
// YZ双轴校正,仅 T_Developer == 6 下通行
else if (T_Developer == 6) {
digitalWrite(B_LED, HIGH);
digitalWrite(R_LED, LOW);
while (frequency > 0) {
// Y-axis
if (!ylimitJudge && digitalRead(ylimitPin) == limit_potential) {
digitalWrite(ystepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(ylimitPin) == !limit_potential && ylimit == false) {
ylimitJudge = true;
ylimit = true;
Serial.println("~ Ystop已觸發");
Serial1.println("~ Ystop已觸發");
}
// Z-axis
if (!zlimitJudge && digitalRead(zlimitPin) == limit_potential) {
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(zlimitPin) == !limit_potential && zlimit == false) {
zlimitJudge = true;
zlimit = true;
Serial.println("~ Zstop已觸發");
Serial1.println("~ Zstop已觸發");
}
// 倘若都触发,则反向
if (ylimitJudge && zlimitJudge) {
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
for (int i = 0; i < 2500; i++) {
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
// 每步間隔
delayMicroseconds(motorDelay);
}
ylimitJudge = false; zlimitJudge = false;
ylimit = false; zlimit = false;
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
frequency--;
if (frequency == 0)
break;
}
delayMicroseconds(motorDelay);
}
digitalWrite(B_LED, LOW);
digitalWrite(R_LED, HIGH);
}
// XZ双轴校正,仅 T_Developer == 7 下通行
else if (T_Developer == 7) {
digitalWrite(B_LED, HIGH);
digitalWrite(R_LED, LOW);
while (frequency > 0) {
// X-axis
if (!xlimitJudge && digitalRead(xlimitPin) == limit_potential) {
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(xlimitPin) == !limit_potential && xlimit == false) {
xlimitJudge = true;
xlimit = true;
Serial.println("~ Xstop已觸發");
Serial1.println("~ Xstop已觸發");
}
// Z-axis
if (!zlimitJudge && digitalRead(zlimitPin) == limit_potential) {
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
} else if (digitalRead(zlimitPin) == !limit_potential && zlimit == false) {
zlimitJudge = true;
zlimit = true;
Serial.println("~ Zstop已觸發");
Serial1.println("~ Zstop已觸發");
}
// 倘若都触发,则反向
if (xlimitJudge && zlimitJudge) {
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
for (int i = 0; i < 2500; i++) {
digitalWrite(xstepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
// 每步間隔
delayMicroseconds(motorDelay);
}
xlimitJudge = false; zlimitJudge = false;
xlimit = false; zlimit = false;
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
frequency--;
if (frequency == 0)
break;
}
delayMicroseconds(motorDelay);
}
digitalWrite(B_LED, LOW);
digitalWrite(R_LED, HIGH);
}
// 完成状态显示
/*Serial.println("- 馬達已校正完成!");
Serial1.println("- 馬達已校正完成!");*/
/*if (frequency == 0) {
Serial.println("- 馬達已校正完成!");
Serial.println("- 請輸入大寫字母'R(返回)'以繼續!");
Serial1.println("- 請輸入大寫字母'R(返回)'以繼續!");
}*/
}
// 模式(線上)
void mode_online(char modeChoose) {
switch (modeChoose) {
case 'A':
online = true;
mode_A();
mode_Offline('<');
online = false;
break;
case 'B':
online = true;
mode_B();
online = false;
break;
case 'C':
online = true;
mode_C();
mode_Offline('<');
online = false;
break;
case 'D':
online = true;
mode_D();
mode_Offline('<');
online = false;
break;
case 'E':
online = true;
mode_E();
mode_Offline('<');
online = false;
break;
}
}
// 馬達啟動,通用副程式,以判斷模式為基礎
void motor_ON() {
// mode_A,校正模式...
while (A_Correction == true) {
char keys = keypad.getKey();
if (Serial.available() > 0)
words = Serial.read();
else if (Serial1.available() > 0)
words = Serial1.read();
// 校正程式(校正次數Anum_02,已校正次數累計顯示Anum_03,當前狀態(燈號、訊息))...
if (Anum_01 <= Anum_02) {
Serial.println("- 馬達正在校正中...");
Serial1.println("- 馬達正在校正中...");
}
while (Anum_01 <= Anum_02) {
// 當前狀態顯示
digitalWrite(B_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('C', 0, 2, "state: moving ", Anum_01);
// set motor_dir = LOW
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
// input motor while
Correction_procedure(1);
delay(500);
Anum_01++;
if (Anum_01 > Anum_02)
Serial.println("- 馬達已校正完成!");
Serial1.println("- 馬達已校正完成!");
}
// 儲存數值並顯示
digitalWrite(B_LED, LOW);
digitalWrite(R_LED, HIGH);
LCD_Display('C', 0, 1, "Current total: ", Anum_03);
LCD_Display('A', 0, 2, "state: finish ", NULL);
LCD_Display('A', 8, 0, "finish", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " ");
// 判斷是否按下'm'
if (keys == 'm' || words == 'R') {
LCD_Display('A', 0, 0, "mode_A: return", NULL);
modeLCD_display("", 2, 1, online, " ", " OK ", " ");
Serial.println("# 已退出模式A!");
Serial1.println("# 已退出模式A!");
Anum_01 = 1;
A_Correction = false;
D_Automatic = false;
modeStart = false;
break;
}
if (keys == 's') {
Anum_01 = 1;
//Serial1.println("press A~D choose mode");
A_Correction = false;
// 原因不明
D_Automatic = false;
modeStart = false;
// mode_A();
break;
}
}
// mode_B,手動(搖桿)模式
while (B_Manual == true) {
// 讀取搖桿數值
if (Serial.available() > 0)
words = Serial.read();
else if (Serial1.available() > 0)
words = Serial1.read();
int xValue = analogRead(joyX);
int yValue = analogRead(joyY);
// 判斷搖桿幅度。如果搖桿幅度過小,跳出副程式。
if ((400 < xValue && xValue < 800) && (300 < yValue && yValue < 800) && online == false || online == true && words == 'R') {
//Serial.println("YG_P");
Serial.println(x_coordinate);
Serial.println(y_coordinate);
Serial.println("===========");
digitalWrite(G_LED, LOW);
digitalWrite(R_LED, HIGH);
LCD_Display('A', 8, 0, "start", NULL);
xsymbol = '.';
ysymbol = '.';
B_Manual = false;
break;
}
// 馬達運作
if (xsymbol == '+' || words == '4') {
// X軸馬達正轉...
//Serial.println("X軸+");
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
x_coordinate++;
} else if (xsymbol == '-' || words == '6') {
// X軸馬達反轉...
//Serial.println("X軸-");
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
x_coordinate--;
} else if (ysymbol == '+' || words == '2') {
// Y軸馬達正轉...
//Serial.println("Y軸+");
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
y_coordinate++;
} else if (ysymbol == '-' || words == '8') {
// Y軸馬達反轉...
//Serial.println("Y軸-");
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
y_coordinate--;
} else if (xsymbol == '@' || words == '7') {
// X、Y軸馬達正、反轉...
//Serial.println("X、Y軸馬達正、反轉");
digitalWrite(xstepPin, HIGH);
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
x_coordinate++;
y_coordinate--;
} else if (ysymbol == '#' || words == '9') {
// X、Y軸馬達反轉...
//Serial.println("X、Y軸馬達反轉");
digitalWrite(xstepPin, HIGH);
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
x_coordinate--;
y_coordinate--;
} else if (xsymbol == '$' || words == '1') {
// X、Y軸馬達正轉...
//Serial.println("X、Y軸馬達正轉");
digitalWrite(xstepPin, HIGH);
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
x_coordinate++;
y_coordinate++;
} else if (ysymbol == '%' || words == '3') {
// X、Y軸馬達反、正轉...
//Serial.println("X、Y軸馬達反、正轉");
digitalWrite(xstepPin, HIGH);
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
x_coordinate--;
y_coordinate++;
}
}
// mode_C,半自動(數值)模式
while (C_Semi_automatic == true) {
char keys = keypad.getKey();
char words = Serial1.read();
// 狀態顯示(移動中(綠),print: move)
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "start ", NULL);
// 馬達根據數值運作(xmoveSteps, ymoveSteps)...
while (xmoveSteps != 0 || ymoveSteps != 0) {
if (xmoveSteps > 0) {
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
xmoveSteps--;
}
if (ymoveSteps > 0) {
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
ymoveSteps--;
}
if (xmoveSteps < 0) {
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
xmoveSteps++;
}
if (ymoveSteps < 0) {
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
ymoveSteps++;
}
//Serial.println(xmoveSteps);
//Serial.println(ymoveSteps);
delayMicroseconds(motorDelay);
}
break;
}
// mode_D,全自動模式
while (D_Automatic == true) {
char keys = keypad.getKey();
char words = Serial1.read();
digitalWrite(B_LED, LOW);
digitalWrite(R_LED, HIGH);
// 先移動到mode_B已鎖定點位
while (D_x_axis[0] != 0 || D_y_axis[0] != 0) {
if (D_x_axis[0] > 0) {
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
D_x_axis[0]--;
}
if (D_y_axis[0] > 0) {
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
D_y_axis[0]--;
}
if (D_x_axis[0] < 0) {
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
D_x_axis[0]++;
}
if (D_y_axis[0] < 0) {
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
D_y_axis[0]++;
}
//Serial.println(D_x_axis[0]);
//Serial.println(D_y_axis[0]);
delayMicroseconds(motorDelay);
}
LCD_Display('A', 8, 0, "press's'", NULL);
modeLCD_display("", 2, 1, online, " ", " ", " ");
if ((keys == 's' && D_i <= Dnum_02 + 1) || (words == '5' && D_i <= Dnum_02 + 1)) {
D_i++;
LCD_Display('A', 8, 0, "moveing", NULL);
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
// 馬達根據數值運作(xmoveSteps, ymoveSteps)...
while (D_x_axis[D_i] != 0 || D_y_axis[D_i] != 0) {
if (D_x_axis[D_i] > 0) {
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
D_x_axis[D_i]--;
}
if (D_y_axis[D_i] > 0) {
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
D_y_axis[D_i]--;
}
if (D_x_axis[D_i] < 0) {
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
D_x_axis[D_i]++;
}
if (D_y_axis[D_i] < 0) {
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
D_y_axis[D_i]++;
}
//Serial.println(xmoveSteps);
//Serial.println(ymoveSteps);
delayMicroseconds(motorDelay);
}
Serial1.print(D_i);
}
else if (D_i >= Dnum_02)
break;
}
// mode_E,點位自定義模式
while (E_Point_custom == true) {
char keys = keypad.getKey();
char words = Serial1.read();
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
while (E_x_axis[Enum_01] != 0 || E_y_axis[Enum_01] != 0) {
if (E_x_axis[Enum_01] > 0) {
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
E_x_axis[Enum_01]--;
}
if (E_y_axis[Enum_01] > 0) {
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
E_y_axis[Enum_01]--;
}
if (E_x_axis[Enum_01] < 0) {
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
digitalWrite(xstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(xstepPin, LOW);
delayMicroseconds(10);
E_x_axis[Enum_01]++;
}
if (E_y_axis[Enum_01] < 0) {
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
digitalWrite(ystepPin, HIGH);
digitalWrite(zstepPin, HIGH);
delayMicroseconds(10);
digitalWrite(ystepPin, LOW);
digitalWrite(zstepPin, LOW);
delayMicroseconds(10);
E_y_axis[Enum_01]++;
}
//Serial.println(B_x_axis[Enum_01]);
//Serial.println(B_y_axis[Enum_01]);
delayMicroseconds(motorDelay);
}
LCD_Display('D', 8, 0, "", NULL);
LCD_Display('C', 8, 0, "N: ", Enum_01);
Enum_01++;
if (Enum_01 == B_i || Enum_01 > B_i) {
digitalWrite(G_LED, LOW);
digitalWrite(R_LED, HIGH);
Serial1.println("Ending");
B_i = 1;
Enum_01 = 1;
delaytime = 0;
E_Point_custom = false;
break;
}
digitalWrite(R_LED, HIGH);
delay(delaytime);
digitalWrite(R_LED, LOW);
}
}
// 模式C、D步數換算
void mathSteps(long & moveSteps, char symbol, int num_01, int num_02, int num_03, int num_04, int rowlocation, int collocation, char axis, long xy_axis[], long xy_axis_01[], int &num) {
// 僅在模式C下運作
if (C_Semi_automatic == true) {
// axis steps
LCD_Display('A', 7, collocation, " ", NULL);
Serial1.print(symbol);
float numDistance = (10 * num_01) + (1 * num_02) + (0.1 * num_03) + (0.01 * num_04);
if (symbol == '-') {
numDistance = 0 - numDistance;
}
Serial1.println(numDistance);
moveSteps = static_cast<long>((static_cast<double>(numDistance) * 3200) / 0.8);
if (moveSteps < 200 && num_04 == 5 && symbol == '+')
moveSteps += 1;
else if (moveSteps < 200 && num_04 == 5 && symbol == '-')
moveSteps -= 1;
Serial1.println(moveSteps);
LCD_Display('D', rowlocation, collocation, "", NULL);
// 这段尚未修正
lcd.setCursor(rowlocation, collocation);
lcd.print(axis);
lcd.print("Step: ");
lcd.print(moveSteps);
}
// 僅在模式D下運作
if (D_Automatic == true) {
// 進行運算、儲存...
float numDistance = (10 * num_01) + (1 * num_02) + (0.1 * num_03) + (0.01 * num_04);
if (symbol == '-') {
numDistance = 0 - numDistance;
}
moveSteps = static_cast<long>((static_cast<double>(numDistance) * 3200) / 0.8);
if (moveSteps < 200 && num_04 == 5 && symbol == '+')
moveSteps += 1;
else if (moveSteps < 200 && num_04 == 5 && symbol == '-')
moveSteps -= 1;
//Serial.println(moveSteps);
xy_axis[num] = moveSteps;
xy_axis_01[num] = moveSteps;
}
}
// 數字位置識別與閃爍(num: 需要變更的數字, flashingLocation: 閃爍位置, location: 數字位置)
void numLocationRecognition(int &num, int COL_flashingLocation, int ROW_flashingLocation, int location) {
while (true) {
// 引用副程式,num: 該位置數值、flashingLocation: 閃爍位置
numChanges(num, COL_flashingLocation, ROW_flashingLocation);
// 按下"s"取消數值變更後跳出
if (numlocation != location)
break;
// 按下"m"後確認返回
if (modeReturn == true) {
if (D_Automatic == true)
modeLCD_display("", 2, 1, online, "Again ", " OK ", " ");
else
modeLCD_display("", 2, 1, online, " ", " OK ", " ");
modeReturn = false;
A_Correction = false;
C_Semi_automatic = false;
D_Automatic = false;
E_Point_custom = false;
Again = false;
LCD_Display('A', 8, 0, "return ", NULL);
break;
}
// 在模式A中按下'o'啟動馬達,校正模式
if (A_Correction == true && modeStart == true && D_Automatic == false) {
modeStart = false;
Anum_03 += Anum_02;
LCD_Display('A', 8, 0, "moving", NULL);
modeLCD_display("", 2, 1, online, " ", " ", " ");
motor_ON();
break;
}
// 在模式C中按下'o'啟動馬達,motor_ON副程式
if (C_Semi_automatic == true && modeStart == true) {
modeStart = false;
// 呼叫步數計算副程式
mathSteps(xmoveSteps, xsymbol, xnum_01, xnum_02, xnum_03, xnum_04, 0, 1, 'x', D_x_axis, D_x_axis_01, Dnum_02);
mathSteps(ymoveSteps, ysymbol, ynum_01, ynum_02, ynum_03, ynum_04, 0, 2, 'y', D_y_axis, D_y_axis_01, Dnum_02);
modeLCD_display("", 2, 1, online, " ", " ", " ");
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "moving", NULL);
motor_ON();
// 恢復模式C操作介面
digitalWrite(G_LED, LOW);
digitalWrite(R_LED, HIGH);
LCD_Display('A', 8, 0, "Input", NULL);
// 无法修正
lcd.setCursor(0, 2);
lcd.print("X:");
lcd.print(xsymbol);
LCD_Display('B', 3, 1, "", xnum_01);
LCD_Display('B', 4, 1, "", xnum_02);
LCD_Display('A', 5, 1, ".", NULL);
LCD_Display('B', 6, 1, "", xnum_03);
LCD_Display('B', 7, 1, "", xnum_04);
LCD_Display('A', 8, 1, " ", NULL);
LCD_Display('C', 9, 1, "Y:", ysymbol);
LCD_Display('B', 12, 1, "", ynum_01);
LCD_Display('B', 13, 1, "", ynum_02);
LCD_Display('A', 14, 1, ".", NULL);
LCD_Display('B', 15, 1, "", ynum_03);
LCD_Display('B', 16, 1, "", ynum_04);
LCD_Display('A', 17, 1, "(mm", NULL);
LCD_Display('D', 0, 2, "", NULL);
break;
}
// 在模式D中按下'o'進入數值選擇...
if (D_Automatic == true && modeStart == true) {
modeStart = false;
// 无法修正
lcd.setCursor(0, 2);
lcd.print("X:");
lcd.print(xsymbol);
LCD_Display('B', 3, 2, "", xnum_01);
LCD_Display('B', 4, 2, "", xnum_02);
LCD_Display('A', 5, 2, ".", NULL);
LCD_Display('B', 6, 2, "", xnum_03);
LCD_Display('B', 7, 2, "", xnum_04);
LCD_Display('A', 8, 2, " ", NULL);
LCD_Display('C', 9, 2, "Y:", ysymbol);
LCD_Display('B', 12, 2, "", ynum_01);
LCD_Display('B', 13, 2, "", ynum_02);
LCD_Display('A', 14, 2, ".", NULL);
LCD_Display('B', 15, 2, "", ynum_03);
LCD_Display('B', 16, 2, "", ynum_04);
LCD_Display('A', 17, 2, "(mm", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " ");
// 若Dnum_02小於Dnum_01則+1,並儲存參數
if (Dnum_02 <= Dnum_01) {
LCD_Display('C', 17, 1, "N:", Dnum_02);
// 引用換算副程式、儲存數值...
mathSteps(xmoveSteps, xsymbol, xnum_01, xnum_02, xnum_03, xnum_04, 0, 2, 'x', D_x_axis, D_x_axis_01, Dnum_02);
mathSteps(ymoveSteps, ysymbol, ynum_01, ynum_02, ynum_03, ynum_04, 0, 3, 'y', D_y_axis, D_y_axis_01, Dnum_02);
Dnum_02++;
} else { /* if (Dnum_02 == Dnum_01) */
for (int i = 1; i < Dnum_02; i++) {
Serial1.println("--------------------");
Serial1.print("Point ");
Serial1.print(i);
Serial1.print(": (");
Serial1.print(D_x_axis[i]);
Serial1.print(", ");
Serial1.print(D_y_axis[i]);
Serial1.println(")");
}
D_x_axis[0] = B_x_axis[1];
D_y_axis[0] = B_y_axis[1];
Dnum_03 = Dnum_02;
// 跳往馬達通用副程式...
A_Correction = false;
motor_ON();
LCD_Display('A', 0, 0, "mode_D: finish ", NULL);
modeLCD_display("", 2, 1, online, "Again ", "return", " ");
Dnum_01 = 0;
Dnum_02 = 0;
// 禁止進入'<' or '>'
D_Automatic = false;
Again = true;
break;
}
// 這邊跳往數值選擇
A_Correction = false;
D_numChoose();
break;
}
// 在模式E按下'o'啟動馬達
if (E_Point_custom == true && modeStart == true) {
modeStart = false;
delaytime = ((Enum_02 * 10) + Enum_03) * 1000;
// 記錄延遲時間
Enum_05 = delaytime;
// 記錄存儲次數
Enum_04 = B_i;
// 位置運算
for (int i = 1; i <= B_i; i++) {
E_x_axis[i] = B_x_axis[i] - B_x_axis[i - 1];
E_y_axis[i] = B_y_axis[i] - B_y_axis[i - 1];
// 存儲移動距離
Serial.println("--------------------");
Serial.print("Point ");
Serial.print(i);
Serial.print(": (");
Serial.print(B_x_axis[i]);
Serial.print(", ");
Serial.print(B_y_axis[i]);
Serial.println(")");
}
modeLCD_display("", 2, 1, online, " ", " ", " ");
motor_ON();
modeLCD_display("", 2, 1, online, "Again ", "return", " setup");
Again = true;
break;
}
// 預設數值閃爍
LCD_Display('A', COL_flashingLocation, ROW_flashingLocation, " ", NULL);
delay(150);
LCD_Display('B', COL_flashingLocation, ROW_flashingLocation, "", num);
delay(150);
}
}
// 手動模式(按鍵)數值選擇
void numChanges(int &num, int COL_location, int ROW_location) {
char keys = keypad.getKey();
if (Serial.available() > 0)
words = Serial.read();
else if (Serial1.available() > 0)
words = Serial1.read();
// 僅在線下模式座使用
if (keys == '>' && online == false) {
// 僅在模式A作用
if (A_Correction == true)
numlocation = 0;
else
numlocation += 1;
} else if (keys == '<' && online == false) {
// 僅在模式A作用
if (A_Correction == true)
numlocation = 0;
// 僅在模式E作用
else if (E_Point_custom == true) {
if (numlocation > 0)
numlocation -= 1;
else
numlocation = 1;
}
else if (numlocation > 0)
numlocation -= 1;
else
numlocation = 9;
} else if (keys == 's' && online == false) {
while (true) {
char keys = keypad.getKey();
if (keys == '>') {
lcd.setCursor(COL_location, ROW_location);
if (num > 0)
num -= 1;
else
num = 9;
lcd.print(num);
delay(300);
} else if (keys == '<') {
lcd.setCursor(COL_location, ROW_location);
if (num < 9)
num += 1;
else
num = 0;
lcd.print(num);
delay(300);
} else if (keys == 's') {
break;
}
}
} else if (keys == 'm' || words == 'R') {
Serial.println("# 已退出模式!");
Serial1.println("# 已退出模式!");
modeReturn = true;
} else if (keys == 'o' || words == 'O') {
/*Serial.println("- 馬達正在校正中...");
Serial1.println("- 馬達正在校正中...");*/
if (C_Semi_automatic == true)
Serial.println("- 馬達正在運作中...");
else // fix
Serial.println("S");
modeStart = true;
}
// 僅在模式A線上模式使用
else if (A_Correction == true && online == true) {
// 读取窗口资料
String inputString;
if (Serial.available() > 0)
inputString = Serial.readStringUntil('\n');
else if (Serial1.available() > 0)
inputString = Serial1.readStringUntil('\n');
int number001 = inputString.toInt();
Anum_02 = number001;
Dnum_01 = number001;
lcd.setCursor(15, 1);
lcd.print(number001);
if (number001 != 0) {
Serial.println(number001);
Serial1.println(number001);
online = !online;
Serial.println("- 請輸入大寫字母\'O(啟動)\' or \'R(返回)\'以繼續!");
Serial1.println("- 請輸入大寫字母\'O(啟動)\' or \'R(返回)\'以繼續!");
number001 = 0;
}
}
// 僅在模式C、D線上模式使用
else if ((C_Semi_automatic == true && online == true) || (D_Automatic == true && online == true)) {
/*Serial.println("Input num:");
Serial1.println("Input num:");*/
String inputString;
/*if (Serial.available() > 0)
inputString = Serial.readStringUntil('\n');
else if (Serial1.available() > 0)
inputString = Serial1.readStringUntil('\n');*/
inputString = Serial.readStringUntil('\n');
int commaIndex = inputString.indexOf(',');
int spaceIndex = inputString.indexOf(' ');
int separatorIndex = (commaIndex != -1 && (spaceIndex == -1 || commaIndex < spaceIndex)) ? commaIndex : spaceIndex;
String number1String = inputString.substring(0, separatorIndex);
String number2String = inputString.substring(separatorIndex + 1);
float number002 = number1String.toFloat();
float number003 = number2String.toFloat();
if (number002 != 0 || number003 != 0) {
Serial.println(number002);
Serial1.println(number002);
int sign1 = (number002 < 0) ? -1 : 1;
xsymbol = (sign1 == -1) ? '-' : '+';
number002 = abs(number002);
xnum_01 = int(number002) / 10;
xnum_02 = int(number002) % 10;
xnum_03 = int(number002 * 10) % 10;
xnum_04 = int(number002 * 100) % 10;
// 判別線上數值輸入為模式A、D
if (C_Semi_automatic == true) {
lcd.setCursor(2, 1);
lcd.print(xsymbol);
LCD_Display('B', 3, 1, "", xnum_01);
LCD_Display('B', 4, 1, "", xnum_02);
LCD_Display('B', 6, 1, "", xnum_03);
LCD_Display('B', 7, 1, "", xnum_04);
} else if (D_Automatic == true) {
lcd.setCursor(2, 2);
lcd.print(xsymbol);
LCD_Display('B', 3, 2, "", xnum_01);
LCD_Display('B', 4, 2, "", xnum_02);
LCD_Display('B', 6, 2, "", xnum_03);
LCD_Display('B', 7, 2, "", xnum_04);
}
Serial.println(number003);
Serial1.println(number003);
int sign2 = (number003 < 0) ? -1 : 1;
ysymbol = (sign2 == -1) ? '-' : '+';
number003 = abs(number003);
ynum_01 = int(number003) / 10;
ynum_02 = int(number003) % 10;
ynum_03 = int(number003 * 10) % 10;
ynum_04 = int(number003 * 100) % 10;
if (C_Semi_automatic == true) {
lcd.setCursor(11, 1);
lcd.print(ysymbol);
LCD_Display('B', 12, 1, "", ynum_01);
LCD_Display('B', 13, 1, "", ynum_02);
LCD_Display('B', 15, 1, "", ynum_03);
LCD_Display('B', 16, 1, "", ynum_04);
} else if (D_Automatic == true) {
lcd.setCursor(11, 2);
lcd.print(ysymbol);
LCD_Display('B', 12, 2, "", ynum_01);
LCD_Display('B', 13, 2, "", ynum_02);
LCD_Display('B', 15, 2, "", ynum_03);
LCD_Display('B', 16, 2, "", ynum_04);
}
// fix 迷之online
online = !online;
}
}
// 僅在模式E線上模式使用
else if (E_Point_custom == true && online == true) {
int num_e = 0;
if (Serial.available() > 0) {
String inputString = Serial.readStringUntil('\n'); // 读取一整行直到换行符
inputString.trim(); // 去除开头和结尾的空白字符
if (inputString.length() == 2) { // 检查是否为两位数
int twoDigitNumber = inputString.toInt(); // 将两位数字字符串转换为整数
Serial.println("识别到的两位数: " + String(twoDigitNumber));
Enum_02 = twoDigitNumber / 10;
Enum_03 = twoDigitNumber % 10;
LCD_Display('B', 16, 2, "", Enum_02);
LCD_Display('B', 17, 2, "", Enum_03);
online = !online;
}
} else if (Serial1.available() > 0) {
String inputString = Serial1.readStringUntil('\n'); // 读取一整行直到换行符
inputString.trim(); // 去除开头和结尾的空白字符
if (inputString.length() == 2) { // 检查是否为两位数
int twoDigitNumber = inputString.toInt(); // 将两位数字字符串转换为整数
//Serial1.println("识别到的两位数: " + String(twoDigitNumber));
Enum_02 = twoDigitNumber / 10;
Enum_03 = twoDigitNumber % 10;
LCD_Display('B', 16, 2, "", Enum_02);
LCD_Display('B', 17, 2, "", Enum_03);
online = !online;
}
}
}
return;
}
// 手動模式(按鍵)符號選擇
void symbolChanges(char &symbol, int COL_location, int ROW_location) {
char keys = keypad.getKey();
Serial1.print(keys);
if (keys == '>') {
numlocation += 1;
} else if (keys == '<') {
if (numlocation > 0)
numlocation -= 1;
else
numlocation = 9;
} else if (keys == 's') {
while (true) {
char keys = keypad.getKey();
if (keys == '>') {
lcd.setCursor(COL_location, ROW_location);
if (symbol == '+')
symbol = '-';
else if (symbol == '-')
symbol = '+';
lcd.print(symbol);
delay(300);
} else if (keys == '<') {
lcd.setCursor(COL_location, ROW_location);
if (symbol == '-')
symbol = '+';
else if (symbol == '+')
symbol = '-';
lcd.print(symbol);
delay(300);
} else if (keys == 's') {
break;
}
}
} else if (keys == 'm') {
modeReturn = true;
} else if (keys == 'o') {
modeStart = true;
}
return;
}
// 數值選擇,僅在模式D下使用
void D_numChoose() {
while (true) {
char keys = keypad.getKey();
char words = Serial1.read();
// 切換線上線下模式
/*if ((keys == 'p' && online == true) || words == 'O') {
Serial1.println("# 已進入線下模式!");
online = false;
} else if ((keys == 'p' && online == false) || words == 'P') {
Serial1.println("# 已進入線上模式!");
keys = '<';
online = true;
} else if (words == 'R') {
Serial1.println("# press A~D choose mode!");
break;
}*/
switch (keys) {
case '<':
while (D_Automatic == true) {
modeLCD_display("", 2, 1, online, " OK ", "return", " ");
if (numlocation == 9) {
while (true) {
symbolChanges(xsymbol, 2, 2);
if (numlocation != 9)
break;
if (modeReturn == true) {
modeReturn = false;
C_Semi_automatic = false;
lcd.setCursor(8, 0);
lcd.print("return ");
break;
}
// 這個須修改
if (modeStart == true) {
modeStart = false;
// 暫無用處
//mathSteps(xmoveSteps, xsymbol, xnum_01, xnum_02, xnum_03, xnum_04, 0, 2, 'x');
//mathSteps(ymoveSteps, ysymbol, ynum_01, ynum_02, ynum_03, ynum_04, 0, 3, 'y');
//motor_ON();
break;
}
lcd.setCursor(2, 2);
lcd.print(" ");
delay(150);
lcd.setCursor(0, 2);
lcd.print("X:");
lcd.print(xsymbol);
delay(150);
}
} else if (numlocation == 8) {
numLocationRecognition(xnum_01, 3, 2, 8);
} else if (numlocation == 7) {
numLocationRecognition(xnum_02, 4, 2, 7);
} else if (numlocation == 6) {
numLocationRecognition(xnum_03, 6, 2, 6);
} else if (numlocation == 5) {
numLocationRecognition(xnum_04, 7, 2, 5);
}// ysymbol
else if (numlocation == 4) {
while (true) {
symbolChanges(ysymbol, 11, 2);
if (numlocation != 4)
break;
if (modeReturn == true) {
modeReturn = false;
C_Semi_automatic = false;
lcd.setCursor(8, 0);
lcd.print("return ");
break;
}
if (modeStart == true) {
modeStart = false;
//mathSteps(xmoveSteps, xsymbol, xnum_01, xnum_02, xnum_03, xnum_04, 0, 2, 'x');
//mathSteps(ymoveSteps, ysymbol, ynum_01, ynum_02, ynum_03, ynum_04, 0, 3, 'y');
//motor_ON();
break;
}
lcd.setCursor(11, 2);
lcd.print(" ");
delay(150);
lcd.setCursor(9, 2);
lcd.print("Y:");
lcd.print(ysymbol);
delay(150);
}
} else if (numlocation == 3) {
numLocationRecognition(ynum_01, 12, 2, 3);
} else if (numlocation == 2) {
numLocationRecognition(ynum_02, 13, 2, 2);
} else if (numlocation == 1) {
numLocationRecognition(ynum_03, 15, 2, 1);
} else if (numlocation == 0) {
numLocationRecognition(ynum_04, 16, 2, 0);
}
// reset
else {
numlocation = 0;
}
}
break;
case '>':
while (D_Automatic == true) {
modeLCD_display("", 2, 1, online, " OK ", "return", " ");
if (numlocation == 9) {
while (true) {
symbolChanges(xsymbol, 2, 2);
if (numlocation != 9)
break;
if (modeReturn == true) {
modeReturn = false;
C_Semi_automatic = false;
lcd.setCursor(8, 0);
lcd.print("return ");
break;
}
if (modeStart == true) {
modeStart = false;
//mathSteps(xmoveSteps, xsymbol, xnum_01, xnum_02, xnum_03, xnum_04, 0, 2, 'x');
//mathSteps(ymoveSteps, ysymbol, ynum_01, ynum_02, ynum_03, ynum_04, 0, 3, 'y');
//motor_ON();
break;
}
lcd.setCursor(2, 2);
lcd.print(" ");
delay(150);
lcd.setCursor(0, 2);
lcd.print("X:");
lcd.print(xsymbol);
delay(150);
}
} else if (numlocation == 8) {
numLocationRecognition(xnum_01, 3, 2, 8);
} else if (numlocation == 7) {
numLocationRecognition(xnum_02, 4, 2, 7);
} else if (numlocation == 6) {
numLocationRecognition(xnum_03, 6, 2, 6);
} else if (numlocation == 5) {
numLocationRecognition(xnum_04, 7, 2, 5);
}// ysymbol
else if (numlocation == 4) {
while (true) {
symbolChanges(ysymbol, 11, 2);
if (numlocation != 4)
break;
if (modeReturn == true) {
modeReturn = false;
C_Semi_automatic = false;
lcd.setCursor(8, 0);
lcd.print("return ");
break;
}
if (modeStart == true) {
modeStart = false;
//mathSteps(xmoveSteps, xsymbol, xnum_01, xnum_02, xnum_03, xnum_04, 0, 2, 'x');
//mathSteps(ymoveSteps, ysymbol, ynum_01, ynum_02, ynum_03, ynum_04, 0, 3, 'y');
//motor_ON();
break;
}
lcd.setCursor(11, 2);
lcd.print(" ");
delay(150);
lcd.setCursor(9, 2);
lcd.print("Y:");
lcd.print(ysymbol);
delay(150);
}
} else if (numlocation == 3) {
numLocationRecognition(ynum_01, 12, 2, 3);
} else if (numlocation == 2) {
numLocationRecognition(ynum_02, 13, 2, 2);
} else if (numlocation == 1) {
numLocationRecognition(ynum_03, 15, 2, 1);
} else if (numlocation == 0) {
numLocationRecognition(ynum_04, 16, 2, 0);
}
// reset
else {
numlocation = 0;
}
}
break;
// 按下's'直接跳出
case 's':
modeReturn = true;
break;
// 模式切換、返回
case 'm':
if (modeChange < 6) {
modeChange += 1;
lcd.clear();
if (modeChange == 1) {
modeLCD_display("mode_B", 2, 2, online, " ", " mode ", " setup");
} else if (modeChange == 2) {
modeLCD_display("mode_C", 12, 1, online, " ", " mode ", " setup");
} else if (modeChange == 3) {
modeLCD_display("mode_D", 12, 2, online, " ", " mode ", " setup");
} else if (modeChange == 4) {
lcd.clear();
modeLCD_display("mode_E", 2, 1, online, " ", " mode ", " setup");
} else if (modeChange == 5) {
modeLCD_display("mode_T", 2, 2, online, " ", " mode ", " setup");
} else if (modeChange == 6) {
modeLCD_display("mode_V", 12, 1, online, " ", " mode ", " setup");
}
} else {
modeChange = 0;
lcd.clear();
modeLCD_display("mode_A", 2, 1, online, " ", " mode ", " setup");
}
break;
case 'o':
if (Again == true) {
Again = false;
for (int i = 1; i < Dnum_03; i++) {
Serial1.println("--------------------");
/* Serial.print("Point ");
Serial.print(i);
Serial.print(": (");
Serial.print(D_x_axis_01[i]);
Serial.print(", ");
Serial.print(D_y_axis_01[i]);
Serial.println(")");*/
D_x_axis[i] = D_x_axis_01[i];
D_y_axis[i] = D_y_axis_01[i];
Serial1.print("Point ");
Serial1.print(i);
Serial1.print(": (");
Serial1.print(D_x_axis[i]);
Serial1.print(", ");
Serial1.print(D_y_axis[i]);
Serial1.println(")");
}
D_x_axis[0] = B_x_axis[1];
D_y_axis[0] = B_y_axis[1];
Dnum_02 = Dnum_03;
D_i = 0;
D_Automatic = true;
motor_ON();
break;
}
}
if (modeReturn == true) {
modeReturn = false;
lcd.setCursor(0, 0);
lcd.print("mode: press 's' ");
break;
}
}
}
// 模式(按鍵)
void mode_Offline(int keys) {
switch (keys) {
// 手動模式(數值)下數值減少,左(選到數字時後閃爍)
case '<':
// 僅在模式A下使用
while (A_Correction == true) {
// Anum_02
modeLCD_display("", 2, 1, online, "enbale", "return", " setup");
numLocationRecognition(Anum_02, 15, 1, 0);
break;
}
// 僅在模式C下使用
while (C_Semi_automatic == true) {
modeLCD_display("", 2, 1, online, "enbale", "return", " setup");
// xsymbol
if (numlocation == 9) {
while (true) {
symbolChanges(xsymbol, 2, 1);
if (numlocation != 9)
break;
if (modeReturn == true) {
modeReturn = false;
C_Semi_automatic = false;
lcd.setCursor(8, 0);
modeLCD_display("", 2, 1, online, " ", " OK ", " ");
lcd.print("return");
break;
}
if (modeStart == true) {
modeStart = false;
mathSteps(xmoveSteps, xsymbol, xnum_01, xnum_02, xnum_03, xnum_04, 0, 1, 'x', D_x_axis, D_x_axis_01, Dnum_02);
mathSteps(ymoveSteps, ysymbol, ynum_01, ynum_02, ynum_03, ynum_04, 0, 2, 'y', D_y_axis, D_y_axis_01, Dnum_02);
motor_ON();
break;
}
lcd.setCursor(2, 1);
lcd.print(" ");
delay(150);
lcd.setCursor(0, 1);
lcd.print("X:");
lcd.print(xsymbol);
delay(150);
}
}
// xnum_01
else if (numlocation == 8) {
numLocationRecognition(xnum_01, 3, 1, 8);
}
// xnum_02
else if (numlocation == 7) {
numLocationRecognition(xnum_02, 4, 1, 7);
}
// xnum_03
else if (numlocation == 6) {
numLocationRecognition(xnum_03, 6, 1, 6);
}
// xnum_04
else if (numlocation == 5) {
numLocationRecognition(xnum_04, 7, 1, 5);
}
// ysymbol
else if (numlocation == 4) {
while (true) {
symbolChanges(ysymbol, 11, 1);
if (numlocation != 4)
break;
if (modeReturn == true) {
modeReturn = false;
C_Semi_automatic = false;
lcd.setCursor(8, 0);
lcd.print("return");
modeLCD_display("", 2, 1, online, " ", " OK ", " ");
break;
}
if (modeStart == true) {
modeStart = false;
mathSteps(xmoveSteps, xsymbol, xnum_01, xnum_02, xnum_03, xnum_04, 0, 1, 'x', D_x_axis, D_x_axis_01, Dnum_02);
mathSteps(ymoveSteps, ysymbol, ynum_01, ynum_02, ynum_03, ynum_04, 0, 2, 'y', D_y_axis, D_y_axis_01, Dnum_02);
motor_ON();
break;
}
lcd.setCursor(11, 1);
lcd.print(" ");
delay(150);
lcd.setCursor(9, 1);
lcd.print("Y:");
lcd.print(ysymbol);
delay(150);
}
}
// ynum_01
else if (numlocation == 3) {
numLocationRecognition(ynum_01, 12, 1, 3);
}
// ynum_02
else if (numlocation == 2) {
numLocationRecognition(ynum_02, 13, 1, 2);
}
// ynum_03
else if (numlocation == 1) {
numLocationRecognition(ynum_03, 15, 1, 1);
}
// ynum_04
else if (numlocation == 0) {
numLocationRecognition(ynum_04, 16, 1, 0);
}
// reset
else {
numlocation = 0;
}
}
// 僅在模式D下使用
while (D_Automatic == true) {
modeLCD_display("", 2, 1, online, " next ", "return", " setup");
A_Correction = true;
numLocationRecognition(Dnum_01, 15, 1, 0);
//A_Correction = false;
break;
}
// 僅在模式E下使用
while (E_Point_custom == true) {
modeLCD_display("", 2, 1, online, "enbale", "return", " ");
if (numlocation == 1)
numLocationRecognition(Enum_02, 16, 2, 1);
else if (numlocation == 0)
numLocationRecognition(Enum_03, 17, 2, 0);
else
numlocation = 0;
}
break;
// 手動模式(數值)下數值增加,右(選到數字時後閃爍)
case '>':
// 僅在模式A下使用
while (A_Correction == true) {
// Anum_01
modeLCD_display("", 2, 1, online, "enbale", "return", " setup");
numLocationRecognition(Anum_02, 15, 1, 0);
}
// 僅在模式C下使用
while (C_Semi_automatic == true) {
modeLCD_display("", 2, 1, online, "enbale", "return", " setup");
// xsymbol
if (numlocation == 9) {
while (true) {
symbolChanges(xsymbol, 2, 1);
if (numlocation != 9)
break;
if (modeReturn == true) {
modeReturn = false;
C_Semi_automatic = false;
lcd.setCursor(8, 0);
lcd.print("return");
modeLCD_display("", 2, 1, online, " ", " OK ", " ");
break;
}
if (modeStart == true) {
modeStart = false;
D_modeStart = false;
motor_ON();
break;
}
lcd.setCursor(2, 1);
lcd.print(" ");
delay(150);
lcd.setCursor(0, 1);
lcd.print("X:");
lcd.print(xsymbol);
delay(150);
}
}
// xnum_01
else if (numlocation == 8) {
numLocationRecognition(xnum_01, 3, 1, 8);
}
// xnum_02
else if (numlocation == 7) {
numLocationRecognition(xnum_02, 4, 1, 7);
}
// xnum_03
else if (numlocation == 6) {
numLocationRecognition(xnum_03, 6, 1, 6);
}
// xnum_04
else if (numlocation == 5) {
numLocationRecognition(xnum_04, 7, 1, 5);
}
// ysymbol
else if (numlocation == 4) {
while (true) {
symbolChanges(ysymbol, 11, 1);
if (numlocation != 4)
break;
if (modeReturn == true) {
modeReturn = false;
C_Semi_automatic = false;
lcd.setCursor(8, 0);
lcd.print("return");
modeLCD_display("", 2, 1, online, " ", " OK ", " ");
break;
}
if (modeStart == true) {
modeStart = false;
motor_ON();
break;
}
lcd.setCursor(11, 1);
lcd.print(" ");
delay(150);
lcd.setCursor(9, 1);
lcd.print("Y:");
lcd.print(ysymbol);
delay(150);
}
}
// ynum_01
else if (numlocation == 3) {
numLocationRecognition(ynum_01, 12, 1, 3);
}
// ynum_02
else if (numlocation == 2) {
numLocationRecognition(ynum_02, 13, 1, 2);
}
// ynum_03
else if (numlocation == 1) {
numLocationRecognition(ynum_03, 15, 1, 1);
}
// ynum_04
else if (numlocation == 0) {
numLocationRecognition(ynum_04, 16, 1, 0);
}
// reset
else {
numlocation = 0;
}
}
// 僅在模式D下使用
while (D_Automatic == true) {
A_Correction = true;
numLocationRecognition(Dnum_01, 15, 1, 0);
//A_Correction = false;
break;
}
// 僅在模式E下使用
while (E_Point_custom == true) {
modeLCD_display("", 2, 1, online, "enbale", "return", " ");
if (numlocation == 1)
numLocationRecognition(Enum_02, 16, 2, 1);
else if (numlocation == 0)
numLocationRecognition(Enum_03, 17, 2, 0);
else
numlocation = 0;
}
break;
// 數值選擇、模式確認
case 's':
// 在模式T中无法触发's'
if (T_return == false) {
switch (modeChange) {
case 0:
mode_A();
B_Manual = false;
C_Semi_automatic = false;
D_Automatic = false;
E_Point_custom = false;
break;
case 1:
xsymbol = '.', ysymbol = '.';
online = false;
A_Correction = false;
C_Semi_automatic = false;
D_Automatic = false;
E_Point_custom = false;
mode_B();
break;
case 2:
// 數值歸零
xsymbol = '+', ysymbol = '+';
xnum_01 = 0, xnum_02 = 0, xnum_03 = 0, xnum_04 = 0;
ynum_01 = 0, ynum_02 = 0, ynum_03 = 0, ynum_04 = 0;
mode_C();
A_Correction = false;
B_Manual = false;
D_Automatic = false;
E_Point_custom = false;
break;
case 3:
// 數值歸零
xsymbol = '+', ysymbol = '+';
xnum_01 = 0, xnum_02 = 0, xnum_03 = 0, xnum_04 = 0;
ynum_01 = 0, ynum_02 = 0, ynum_03 = 0, ynum_04 = 0;
mode_D();
A_Correction = false;
B_Manual = false;
C_Semi_automatic = false;
E_Point_custom = false;
break;
case 4:
mode_E();
A_Correction = false;
B_Manual = false;
C_Semi_automatic = false;
D_Automatic = false;
break;
case 5:
mode_T();
break;
case 6:
mode_V();
break;
}
break;
}
// 如果再模式T中按下's', 则进入模式T
else if (T_return == true) {
mode_T();
}
// 模式切換、返回
case 'm':
if (modeChange < 6) {
// 模式E出來後關閉Again
Again = false;
lcd.clear();
// 如果从模式T中退出,则不加1
if (T_return == false) {
modeChange += 1;
if (modeChange == 1) {
modeLCD_display("mode_B", 2, 2, online, " ", " mode ", " setup");
} else if (modeChange == 2) {
modeLCD_display("mode_C", 12, 1, online, " ", " mode ", " setup");
} else if (modeChange == 3) {
modeLCD_display("mode_D", 12, 2, online, " ", " mode ", " setup");
} else if (modeChange == 4) {
lcd.clear();
modeLCD_display("mode_E", 2, 1, online, " ", " mode ", " setup");
} else if (modeChange == 5) {
modeLCD_display("mode_T", 2, 2, online, " ", " mode ", " setup");
} else if (modeChange == 6) {
modeLCD_display("mode_V", 12, 1, online, " ", " mode ", " setup");
}
}
else if (T_return == true && modeChange != 0 || modeChange == 0) {
if (modeChange == 1) {
modeLCD_display("mode_B", 2, 2, online, " ", " mode ", " setup");
} else if (modeChange == 2) {
modeLCD_display("mode_C", 12, 1, online, " ", " mode ", " setup");
} else if (modeChange == 3) {
modeLCD_display("mode_D", 12, 2, online, " ", " mode ", " setup");
} else if (modeChange == 4) {
lcd.clear();
modeLCD_display("mode_E", 2, 1, online, " ", " mode ", " setup");
} else if (modeChange == 5) {
modeLCD_display("mode_T", 2, 2, online, " ", " mode ", " setup");
} else if (modeChange == 6) {
modeLCD_display("mode_V", 12, 1, online, " ", " mode ", " setup");
} else {
modeLCD_display("mode_A", 2, 1, online, " ", " mode ", " setup");
}
T_return = false;
}
} else {
modeChange = 0;
lcd.clear();
modeLCD_display("mode_A", 2, 1, online, " ", " mode ", " setup");
}
break;
case 'p':
mode_T();
break;
case 'o':
// 僅在模式E時使用
if (Again == true) {
E_Point_custom = true;
// 預先歸零
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
Correction_procedure(2);
for (int i = 1; i < Enum_04; i++) {
E_x_axis[i] = B_x_axis[i] - B_x_axis[i - 1];;
E_y_axis[i] = B_y_axis[i] - B_y_axis[i - 1];;
Serial1.print("Point ");
Serial1.print(i);
Serial1.print(": (");
Serial1.print(B_x_axis[i]);
Serial1.print(", ");
Serial1.print(B_y_axis[i]);
Serial1.println(")");
}
delaytime = Enum_05;
B_i = Enum_04;
Enum_01 = 0;
motor_ON();
break;
}
}
}
// 校正模式(常規(紅)、碰到限位開關(藍)、移動(綠))
void mode_A() {
Serial.println("# 當前已進入模式A,點位校正(Correction Mode)模式!");
Serial.print("- 請輸入校正次數(建議02~09次):");
Serial1.println("# 當前已進入模式A,點位校正(Correction Mode)模式!");
Serial1.print("- 請輸入校正次數(建議02~09次):");
Anum_02 = 0;
lcd.clear();
LCD_Display('A', 0, 0, "mode_A: Input ", NULL);
LCD_Display('C', 0, 1, "set frequency: ", Anum_02);
modeLCD_display("", 2, 1, online, " ", "return", " ");
// 模式A_校正模式啟用
A_Correction = true;
}
// 手動模式(常規(紅)、碰到限位開關(藍)、移動(綠))
void mode_B() {
// Serial Display
Serial.println("# 當前已進入模式B,搖桿調整(Manual Mode)模式!");
Serial1.println("# 當前已進入模式B,搖桿調整(Manual Mode)模式!");
// 參數歸零
x_coordinate = 0;
y_coordinate = 0;
B_i = 1;
// LCD Display
lcd.clear();
LCD_Display('A', 0, 0, "mode_B: moving", NULL);
modeLCD_display("", 2, 1, online, " ", " ", " ");
// 预先校正
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
Serial.println("- 馬達正在校正中...");
Serial1.println("- 馬達正在校正中...");
Correction_procedure(2);
Serial.println("- 馬達已校正完成!");
Serial1.println("- 馬達已校正完成!");
Serial.println("- 請輸入數字'O(紀錄)'、'1~4, 6~9(方向)'、'R(返回)'、5'(停止)'以繼續!");
Serial1.println("- 請輸入數字'O(紀錄)'、'1~4, 6~9(方向)'、'R(返回)'、'5(停止)'以繼續!");
LCD_Display('A', 0, 0, "mode_B: Input ", NULL);
LCD_Display('A', 3, 1, "press Joystick", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " setup");
while (true) {
int buttonState = digitalRead(joyButton);
char keys = keypad.getKey();
if (Serial.available() > 0)
words = Serial.read();
else if (Serial1.available() > 0)
words = Serial1.read();
if (modeReturn == true || keys == 'm' || words == 'R') {
lcd.clear();
LCD_Display('A', 0, 0, "mode_B: return", NULL);
modeLCD_display("", 2, 1, online, " ", " OK ", " ");
Serial1.println("press A~D Choose mode!");
modeReturn = false;
buttonState = 0;
button = 0;
break;
}
while (buttonState == 0 || online == true) {
char keys = keypad.getKey();
if (Serial.available() > 0)
words = Serial.read();
else if (Serial1.available() > 0)
words = Serial1.read();
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, HIGH);
int xValue = analogRead(joyX);
int yValue = analogRead(joyY);
int buttonState = digitalRead(joyButton);
//Serial.print("\tButton: ");
//Serial.println(buttonState);
modeLCD_display("", 2, 1, online, "record", "return", " ");
// 状态面板显示 (通過按鍵's'切換 for 10/31)
if (B_display == false) {
LCD_Display('D', 0, 1, "", NULL);
LCD_Display('D', 0, 2, "", NULL);
LCD_Display('A', 3, 1, " Stick ON ", NULL);
LCD_Display('C', 3, 2, "X: ", xValue);
LCD_Display('C', 11, 2, "Y: ", yValue);
for (int i = 0; i < 200; i++) {
char keys = keypad.getKey();
if (keys == 'm') {
button = 0;
modeReturn = true;
digitalWrite(G_LED, LOW);
digitalWrite(R_LED, HIGH);
LCD_Display('A', 6, 1, "Stick OFF", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " setup");
break;
}
// 如果按下'o',則存儲位置
else if (keys == 'o') {
B_x_axis[B_i] = x_coordinate;
B_y_axis[B_i] = y_coordinate;
// 模式E再次執行用
//E_x_axis[B_i] = x_coordinate;
//E_y_axis[B_i] = y_coordinate;
lcd.setCursor(8, 0);
lcd.print("N: ");
lcd.setCursor(10, 0);
lcd.print(B_i);
Serial.print("Current X: ");
Serial.println(x_coordinate);
Serial.print("Current Y: ");
Serial.println(y_coordinate);
button = 0;
if (B_i < B_listSize)
B_i++;
else
break;
}
delay(1);
}
if (modeReturn == true) break;
} else if (B_display == true) {
LCD_Display('D', 0, 1, "", NULL);
LCD_Display('D', 0, 2, "", NULL);
LCD_Display('A', 0, 1, " XY coordinates", NULL);
LCD_Display('F', 0, 2, "X: ", x_coordinate);
LCD_Display('F', 11, 2, "X: ", y_coordinate);
delay(200);
}
// 馬達雙軸判斷
if ((xValue < 400 && yValue < 300) || words == '7') {
xsymbol = '@';
B_Manual = true;
// 狀態顯示(移動中(綠),print: move)
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "move ", NULL);
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
motor_ON();
} else if ((xValue > 800 && yValue < 50) || words == '9') {
ysymbol = '#';
B_Manual = true;
// 狀態顯示(移動中(綠),print: move)
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "move ", NULL);
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
motor_ON();
} else if ((xValue < 400 && yValue > 800) || words == '1') {
xsymbol = '$';
B_Manual = true;
// 狀態顯示(移動中(綠),print: move)
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "move ", NULL);
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
motor_ON();
} else if ((xValue > 800 && yValue > 800) || words == '3') {
ysymbol = '%';
B_Manual = true;
// 狀態顯示(移動中(綠),print: move)
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "move ", NULL);
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
motor_ON();
}
// 馬達單軸判斷
else if (xValue < 400 || words == '6') {
xsymbol = '+';
B_Manual = true;
// 狀態顯示(移動中(綠),print: move)
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "move ", NULL);
motion_DIR(xdirPin, x_state, y_state, z_state, '+');
motor_ON();
} else if (xValue > 800 || words == '4') {
xsymbol = '-';
B_Manual = true;
// 狀態顯示(移動中(綠),print: move)
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "move ", NULL);
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motor_ON();
} else if (yValue < 300 || words == '8') {
ysymbol = '-';
B_Manual = true;
// 狀態顯示(移動中(綠),print: move)
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "move ", NULL);
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
motor_ON();
} else if (yValue > 800 || words == '2') {
ysymbol = '+';
B_Manual = true;
// 狀態顯示(移動中(綠),print: move)
digitalWrite(G_LED, HIGH);
digitalWrite(R_LED, LOW);
LCD_Display('A', 8, 0, "move ", NULL);
motion_DIR(ydirPin, x_state, y_state, z_state, '+');
motion_DIR(zdirPin, x_state, y_state, z_state, '+');
motor_ON();
}
// 再次按下搖桿,跳出搖桿模式
else if (/*(buttonState == 0 && button == 1) || */(keys == 'm' && button == 1) || (words == '5' && button == 1)) {
button = 0;
// 直接離開
modeReturn = true;
digitalWrite(G_LED, LOW);
digitalWrite(R_LED, HIGH);
LCD_Display('A', 6, 1, "Stick OFF", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " setup");
Serial1.println("Stick OFF");
//Serial.println("press 'R' return or '5' continue, 'O' online, 'P' offline.");
break;
}
// 如果按下'o',則存儲位置
else if (keys == 'o' && button == 1 || words == 'O') {
B_x_axis[B_i] = x_coordinate;
B_y_axis[B_i] = y_coordinate;
// 模式E再次執行用
//E_x_axis[B_i] = x_coordinate;
//E_y_axis[B_i] = y_coordinate;
lcd.setCursor(8, 0);
lcd.print("N: ");
lcd.setCursor(10, 0);
lcd.print(B_i);
Serial.print("Current X: ");
Serial.println(x_coordinate);
Serial.print("Current Y: ");
Serial.println(y_coordinate);
button = 0;
if (B_i < B_listSize)
B_i++;
else
break;
}
// 如果按下's',則切換顯示介面
else if (keys == 's' && button == 1)
B_display = !B_display;
Serial.println(button);
button = 1;
Serial.println(button);
}
}
}
// 半自動模式(常規(紅)、藍))
void mode_C() {
// LCD顯示
lcd.clear();
Serial1.println("# 當前為mode_C,半自動(Semi Automatic Mode)模式!");
lcd.setCursor(0, 0);
lcd.print("mode_C: Input");
lcd.setCursor(0, 1);
lcd.print("X:");
lcd.print(xsymbol);
lcd.setCursor(3, 1);
lcd.print(xnum_01);
lcd.print(xnum_02);
lcd.print(".");
lcd.print(xnum_03);
lcd.print(xnum_04);
lcd.setCursor(9, 1);
lcd.print("Y:");
lcd.print(ysymbol);
lcd.setCursor(12, 1);
lcd.print(ynum_01);
lcd.print(ynum_02);
lcd.print(".");
lcd.print(ynum_03);
lcd.print(ynum_04);
lcd.setCursor(17, 1);
lcd.print("(mm");
modeLCD_display("", 2, 1, online, " ", "return", " setup");
// 模式C_半自動模式啟用
C_Semi_automatic = true;
}
// 自動模式
void mode_D() {
Dnum_01 = 0;
Dnum_02 = 0;
lcd.clear();
Serial1.println("# 當前為mode_D,自動(Automatic Mode)模式!");
lcd.setCursor(0, 0);
lcd.print("mode_D: Input");
lcd.setCursor(0, 1);
lcd.print("set frequency: ");
lcd.print(Dnum_01);
modeLCD_display("", 2, 1, online, " ", "return", " ");
// 預先歸零
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
Serial.println("- 馬達正在校正中...");
Serial1.println("- 馬達正在校正中...");
Correction_procedure(2);
Serial.println("- 馬達已校正完成!");
Serial1.println("- 馬達已校正完成!");
// 模式D_自動模式啟用
D_Automatic = true;
}
// 點位自定義模式(搖桿移動定位)
void mode_E() {
Enum_01 = 1;
Enum_02 = 0;
Enum_03 = 0;
lcd.clear();
Serial.println("# 當前已進入模式E,自定義(Point custom Mode)模式!");
Serial1.println("# 當前已進入模式E,自定義(Point custom Mode)模式!");
LCD_Display('A', 0, 0, "mode_E: Input ", NULL);
LCD_Display('A', 0, 1, "Documented point: ", NULL);
LCD_Display('C', 0, 1, "Documented point: ", B_i - 1);
LCD_Display('A', 0, 2, "set delay time: ", NULL);
lcd.print(Enum_02);
lcd.print(Enum_03);
LCD_Display('A', 18, 2, "(s", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " ");
for (int i = 1; i < B_i; i++) {
Serial.print("Point ");
Serial.print(i);
Serial.print(": (");
Serial.print(B_x_axis[i]);
Serial.print(", ");
Serial.print(B_y_axis[i]);
Serial.println(")");
}
// 預先歸零
motion_DIR(xdirPin, x_state, y_state, z_state, '-');
motion_DIR(ydirPin, x_state, y_state, z_state, '-');
motion_DIR(zdirPin, x_state, y_state, z_state, '-');
Serial.println("- 馬達正在校正中...");
Serial1.println("- 馬達正在校正中...");
Correction_procedure(2);
Serial.println("- 馬達已校正完成!");
Serial1.println("- 馬達已校正完成!");
Serial.println("- 請輸入延遲時間(S)!");
// 模式E_自定義模式啟用
E_Point_custom = true;
}
/* 开发者模式 - Developer Mode (for 12/2) */
// 限位控制 (limit switch), 步进延迟 (Step Delay), 运动方向 (Motion DIR), 线上/线下(online)
void mode_T() {
// 模式T(开发者模式)初始设置
T_mode = 0;
T_return = true;
lcd.clear();
LCD_Display('A', 0, 0, "mode_T: Input", NULL);
LCD_Display('A', 2, 1, "<", NULL);
LCD_Display('A', 4, 1, "limit switch", NULL);
LCD_Display('A', 17, 1, ">", NULL);
// 读取当前限位状态(12/1)
LCD_Display('A', 0, 2,
(T_Developer == 1) ? " - No-limit - " :
(T_Developer == 2) ? " - Xlimit - " :
(T_Developer == 3) ? " - Ylimit - " :
(T_Developer == 4) ? " - Zlimit - " :
(T_Developer == 5) ? " - XY-limit - " :
(T_Developer == 6) ? " - YZ-limit - " :
(T_Developer == 7) ? " - XZ-limit - " :
" - Nr-limit - ", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " F-set");
while (true) {
char keys = keypad.getKey();
if (keys == '<') {
T_mode -= 1;
// 开发者模式 - 是否启用上拉电阻 (Resistor Set)
if (T_mode == 4) {
LCD_Display('A', 4, 1, "Resistor Set", NULL);
LCD_Display('A', 0, 2, (potential == true) ? " - True - " : " - None - ", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " F-set");
}
// 开发者模式 - 线上/线下 (Online)
else if (T_mode == 3) {
LCD_Display('A', 4, 1, " Online ", NULL);
LCD_Display('A', 0, 2, (online == true) ? " - True - " : " - None - ", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " F-set");
}
// 开发者模式 - 步进延迟 (Step Delay)
else if (T_mode == 2) {
LCD_Display('A', 4, 1, " Step Delay ", NULL);
// 读取当前步进延迟(for 11/29)
LCD_Display('A', 0, 2,
(delayenable == 1) ? " - 600 ms - " :
(delayenable == 2) ? " - 800 ms - " :
(delayenable == 3) ? " - 1000 ms - " :
(delayenable == 4) ? " - 200 ms - " :
" - 400 ms - ", NULL);
}
// 开发者模式 - 运动方向 (Motion DIR)
else if (T_mode == 1) {
// 读取当前运动状态(11/28)
LCD_Display('A', 4, 1, " Motion DIR ", NULL);
if (state_enable == 0)
LCD_Display('A', 0, 2, (x_state == false) ? " - X-Normal - " : " - X-Invert - ", NULL);
else if (state_enable == 1)
LCD_Display('A', 0, 2, (y_state == false) ? " - Y-Normal - " : " - Y-Invert - ", NULL);
else if (state_enable == 2)
LCD_Display('A', 0, 2, (z_state == false) ? " - Z-Normal - " : " - Z-Invert - ", NULL);
}
// 开发者模式 - 限位控制 (limit switch)
else {
LCD_Display('A', 4, 1, "limit switch", NULL);
// 读取当前限位状态(12/1)
LCD_Display('A', 0, 2,
(T_Developer == 1) ? " - No-limit - " :
(T_Developer == 2) ? " - Xlimit - " :
(T_Developer == 3) ? " - Ylimit - " :
(T_Developer == 4) ? " - Zlimit - " :
(T_Developer == 5) ? " - XY-limit - " :
(T_Developer == 6) ? " - YZ-limit - " :
(T_Developer == 7) ? " - XZ-limit - " :
" - Nr-limit - ", NULL);
T_mode = 5;
}
} else if (keys == '>') {
T_mode += 1;
// 开发者模式 - 步进延迟 (Step Delay)
if (T_mode == 1) {
// 读取当前运动状态(11/28)
LCD_Display('A', 4, 1, " Motion DIR ", NULL);
if (state_enable == 0)
LCD_Display('A', 0, 2, (x_state == false) ? " - X-Normal - " : " - X-Invert - ", NULL);
else if (state_enable == 1)
LCD_Display('A', 0, 2, (y_state == false) ? " - Y-Normal - " : " - Y-Invert - ", NULL);
else if (state_enable == 2)
LCD_Display('A', 0, 2, (z_state == false) ? " - Z-Normal - " : " - Z-Invert - ", NULL);
}
// 开发者模式 - 步进延迟 (Step Delay)
else if (T_mode == 2) {
LCD_Display('A', 4, 1, " Step Delay ", NULL);
// 读取当前步进延迟(for 11/29)
LCD_Display('A', 0, 2,
(delayenable == 1) ? " - 600 ms - " :
(delayenable == 2) ? " - 800 ms - " :
(delayenable == 3) ? " - 1000 ms - " :
(delayenable == 4) ? " - 200 ms - " :
" - 400 ms - ", NULL);
}
// 开发者模式 - 线上/线下 (Online)
else if (T_mode == 3) {
LCD_Display('A', 4, 1, " Online ", NULL);
LCD_Display('A', 0, 2, (online == true) ? " - True - " : " - None - ", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " F-set");
}
// 开发者模式 - 线上/线下 (Online)
else if (T_mode == 3) {
LCD_Display('A', 4, 1, " Online ", NULL);
LCD_Display('A', 0, 2, (online == true) ? " - True - " : " - None - ", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " F-set");
}
// 开发者模式 - 是否启用上拉电阻 (Resistor Set)
else if (T_mode == 4) {
LCD_Display('A', 4, 1, "Resistor Set", NULL);
LCD_Display('A', 0, 2, (potential == true) ? " - True - " : " - None - ", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " F-set");
}
else {
LCD_Display('A', 4, 1, "limit switch", NULL);
// 读取当前限位状态(12/1)
LCD_Display('A', 0, 2,
(T_Developer == 1) ? " - No-limit - " :
(T_Developer == 2) ? " - Xlimit - " :
(T_Developer == 3) ? " - Ylimit - " :
(T_Developer == 4) ? " - Zlimit - " :
(T_Developer == 5) ? " - XY-limit - " :
(T_Developer == 6) ? " - YZ-limit - " :
(T_Developer == 7) ? " - XZ-limit - " :
" - Nr-limit - ", NULL);
T_mode = 0;
}
} else if (keys == 's') {
// T_mode:"limit switch"
if (T_mode == 0) {
// 读取当前限位状态(12/1)
LCD_Display('A', 0, 2,
(T_Developer == 1) ? " - No-limit - " :
(T_Developer == 2) ? " - Xlimit - " :
(T_Developer == 3) ? " - Ylimit - " :
(T_Developer == 4) ? " - Zlimit - " :
(T_Developer == 5) ? " - XY-limit - " :
(T_Developer == 6) ? " - YZ-limit - " :
(T_Developer == 7) ? " - XZ-limit - " :
" - Nr-limit - ", NULL);
while (true) {
char keys = keypad.getKey();
// 狀態:None(無限制)、All(全限制)、One(X軸單軸限制)、Two(YZ軸限制)。
if (keys == '<') {
limitenable++;
T_Developer = (limitenable == 1) ? 1 :
(limitenable == 2) ? 2 :
(limitenable == 3) ? 3 :
(limitenable == 4) ? 4 :
(limitenable == 5) ? 5 :
(limitenable == 6) ? 6 :
(limitenable == 7) ? 7 :
limitenable = 0;
// 读取当前限位状态(12/1)
LCD_Display('A', 0, 2,
(T_Developer == 1) ? " - No-limit - " :
(T_Developer == 2) ? " - Xlimit - " :
(T_Developer == 3) ? " - Ylimit - " :
(T_Developer == 4) ? " - Zlimit - " :
(T_Developer == 5) ? " - XY-limit - " :
(T_Developer == 6) ? " - YZ-limit - " :
(T_Developer == 7) ? " - XZ-limit - " :
" - Nr-limit - ", NULL);
} else if (keys == '>') {
limitenable--;
T_Developer = (limitenable == 7) ? 7 :
(limitenable == 6) ? 6 :
(limitenable == 5) ? 5 :
(limitenable == 4) ? 4 :
(limitenable == 3) ? 3 :
(limitenable == 2) ? 2 :
(limitenable == 1) ? 1 :
limitenable = 8;
// 读取当前限位状态(12/1)
LCD_Display('A', 0, 2,
(T_Developer == 1) ? " - No-limit - " :
(T_Developer == 2) ? " - Xlimit - " :
(T_Developer == 3) ? " - Ylimit - " :
(T_Developer == 4) ? " - Zlimit - " :
(T_Developer == 5) ? " - XY-limit - " :
(T_Developer == 6) ? " - YZ-limit - " :
(T_Developer == 7) ? " - XZ-limit - " :
" - Nr-limit - ", NULL);
} else if (keys == 'm') {
LCD_Display('D', 3, 2, "", 17);
break;
}
}
}
// T_mode:"Motion DIR"
else if (T_mode == 1 || T_mode == -2) {
// 变更当前运动状态(11/28)
while (true) {
char keys = keypad.getKey();
if (keys == '<') {
state_enable += 1;
// Y轴
if (state_enable == 1) {
LCD_Display('A', 0, 2, (y_state == false) ? " - Y-Normal - " : " - Y-Invert - ", NULL);
}
// Z轴
else if (state_enable == 2) {
LCD_Display('A', 0, 2, (z_state == false) ? " - Z-Normal - " : " - Z-Invert - ", NULL);
}
// X轴
else {
LCD_Display('A', 0, 2, (x_state == false) ? " - X-Normal - " : " - X-Invert - ", NULL);
state_enable = 0;
}
}
else if (keys == '>') {
state_enable -= 1;
// Z轴
if (state_enable == 2) {
LCD_Display('A', 0, 2, (z_state == false) ? " - Z-Normal - " : " - Z-Invert - ", NULL);
}
// Y轴
else if (state_enable == 1) {
LCD_Display('A', 0, 2, (y_state == false) ? " - Y-Normal - " : " - Y-Invert - ", NULL);
}
// X轴
else {
LCD_Display('A', 0, 2, (x_state == false) ? " - X-Normal - " : " - X-Invert - ", NULL);
state_enable = 3;
}
}
else if (keys == 's') {
if (state_enable == 0 || state_enable == 3) {
// 倘诺按下's'切换状态
x_state = (x_state == false) ? true :
(x_state == true) ? false :
x_state;
LCD_Display('A', 0, 2, (x_state == false) ? " - X-Normal - " : " - X-Invert - ", NULL);
}
if (state_enable == 1) {
// 倘诺按下's'切换状态
y_state = (y_state == false) ? true :
(y_state == true) ? false :
y_state;
LCD_Display('A', 0, 2, (y_state == false) ? " - Y-Normal - " : " - Y-Invert - ", NULL);
}
else if (state_enable == 2) {
// 倘诺按下's'切换状态
z_state = (z_state == false) ? true :
(z_state == true) ? false :
z_state;
LCD_Display('A', 0, 2, (z_state == false) ? " - Z-Normal - " : " - Z-Invert - ", NULL);
}
} else if (keys == 'm') {
LCD_Display('D', 3, 2, "", 17);
break;
}
}
}
// T_mode:"Step Delay"
else if (T_mode == -1 || T_mode == 2) {
while (true) {
char keys = keypad.getKey();
// 狀態:Delay 200, Delay 400, Delay 600, Delay 800, Delay 1000。
if (keys == '<') {
delayenable++;
motorDelay = (delayenable == 1) ? 600 :
(delayenable == 2) ? 800 :
(delayenable == 3) ? 1000 :
(delayenable == 4) ? 200 :
400;
if (delayenable > 4) delayenable = 0;
LCD_Display('A', 0, 2,
(motorDelay == 200) ? " - 200 ms - " :
(motorDelay == 600) ? " - 600 ms - " :
(motorDelay == 800) ? " - 800 ms - " :
(motorDelay == 1000) ? " - 1000 ms - " :
" - 400 ms - ", NULL);
} else if (keys == '>') {
delayenable--;
motorDelay = (delayenable == 1) ? 600 :
(delayenable == 2) ? 800 :
(delayenable == 3) ? 1000 :
(delayenable == 4) ? 200 :
400;
if (delayenable < 0) delayenable = 5;
LCD_Display('A', 0, 2,
(motorDelay == 200) ? " - 200 ms - " :
(motorDelay == 600) ? " - 600 ms - " :
(motorDelay == 800) ? " - 800 ms - " :
(motorDelay == 1000) ? " - 1000 ms - " :
" - 400 ms - ", NULL);
} else if (keys == 'm') {
LCD_Display('D', 3, 2, "", 17);
break;
}
}
}
// 开发者模式 - 线上/线下 (Online)
else if (T_mode == 3) {
online = (online == true) ? false : true;
LCD_Display('A', 0, 2, (online == true) ? " - True - " : " - None - ", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " ");
}
// 开发者模式 - 是否启用上拉电阻 (Resistor Set)
else if (T_mode == 4) {
if (potential == false) {
pinMode(xlimitPin, INPUT_PULLUP);
pinMode(ylimitPin, INPUT_PULLUP);
pinMode(zlimitPin, INPUT_PULLUP);
// 輸入上拉 且 設置高電位
potential = true;
LCD_Display('A', 0, 2, " - True - ", NULL);
}
else if (potential == true) {
pinMode(xlimitPin, INPUT);
pinMode(ylimitPin, INPUT);
pinMode(zlimitPin, INPUT);
// 恢復初始設置
potential = false;
LCD_Display('A', 0, 2, " - None - ", NULL);
}
}
} else if (keys == 'm') {
// 退出模式T
LCD_Display('A', 0, 0, "mode_T: return ", NULL);
LCD_Display('D', 3, 2, "", 17);
break;
} else if (keys == 'p') {
LCD_Display('A', 0, 0, "mode_T: fast_set", NULL);
LCD_Display('D', 3, 2, "", 17);
LCD_Display('A', 2, 1, "<", NULL);
LCD_Display('A', 4, 1, "Fast setmode", NULL);
LCD_Display('A', 17, 1, ">", NULL);
modeLCD_display("", 2, 1, online, " ", "return", " ");
// 模式判别
LCD_Display('A', 0, 2, (fast_setmode == 1) ? " - Tn set - " : " - None set - ", NULL);
while (true) {
char keys = keypad.getKey();
if (keys == 's') {
fast_setmode++;
if (fast_setmode == 1) {
LCD_Display('A', 0, 2, " - Tn set - ", NULL);
// 坦联快速设置
T_Developer = 7; potential = true; x_state = true;
} else {
fast_setmode = 0;
LCD_Display('A', 0, 2, " - None set - ", NULL);
T_Developer = 0; potential = false; x_state = false;
}
} else if (keys == 'm') {
LCD_Display('A', 0, 0, "mode_T: return ", NULL);
LCD_Display('D', 3, 2, "", 17);
break;
}
}
}
}
}
// Changelog(韌體版本)
void mode_V() {
lcd.clear();
LCD_Display('A', 0, 0, "mode_V: Input", NULL);
// 當前版本
LCD_Display('A', 0, 1, "Ver. : ", NULL);
lcd.print(updated_Version);
// 更新日期
LCD_Display('A', 0, 2, "V_Date: ", NULL);
lcd.print(updated_Date);
modeLCD_display("", 2, 1, online, " ", "return", " ");
}