#include <Arduino.h>
#include <MatrixMini.h>
// ==========================================
// Matrix Mini 競賽多模式系統 V6.1
// 生成時間: 2026/3/17 下午 3:30
// 模式總數: 4 (SELECT切換, TRIANGLE啟動)
// 總步驟數: 6
// ==========================================
// 舵機當前角度追蹤 (用於相對角度調整)
int current_RC1 = 90;
int current_RC2 = 90;
int current_RC3 = 90;
int current_RC4 = 90;
int currentMode = 0; // 當前模式 (0=紅, 1=綠, 2=藍, 3=黃)
bool scriptRunning = false; // 腳本是否正在執行
bool scriptPaused = false; // 腳本是否已暫停
int currentStep = 0; // 當前執行到第幾步
void setup() {
Mini.begin(8.1, 0, 115200);
Mini.PS2.begin();
Mini.M1.set(0);
Mini.M2.set(0);
// 初始化舵機到中間位置
Mini.RC1.set(current_RC1);
Mini.RC2.set(current_RC2);
Mini.RC3.set(current_RC3);
Mini.RC4.set(current_RC4);
// 初始化板載 RGB LED
Mini.RGB1.begin();
Mini.RGB2.begin();
// 顯示初始模式 (紅燈 = 模式 A)
showCurrentMode();
}
void loop() {
Mini.PS2.polling();
// ==========================================
// 1. 十字鍵與搖桿行走控制
// ==========================================
if (Mini.PS2.UP) { Mini.M1.set(100); Mini.M2.set(100); }
else if (Mini.PS2.DOWN) { Mini.M1.set(-100); Mini.M2.set(-100); }
else if (Mini.PS2.LEFT) { Mini.M1.set(-70); Mini.M2.set(70); }
else if (Mini.PS2.RIGHT) { Mini.M1.set(70); Mini.M2.set(-70); }
else {
// 搖桿控制
int moveL = map(Mini.PS2.LY, 0, 255, 100, -100);
int moveR = map(Mini.PS2.RY, 0, 255, 100, -100);
if (abs(moveL) < 15) moveL = 0;
if (abs(moveR) < 15) moveR = 0;
Mini.M1.set(moveR);
Mini.M2.set(moveL);
}
// ==========================================
// 2. SELECT 鍵: 切換模式
// ==========================================
if (Mini.PS2.SELECT && !scriptRunning) {
currentMode = (currentMode + 1) % 4;
showCurrentMode();
delay(300); // 防彈跳
}
// ==========================================
// 3. TRIANGLE 鍵: 啟動/繼續當前模式腳本
// ==========================================
if (Mini.PS2.TRIANGLE) {
if (!scriptRunning) {
// 首次啟動腳本
scriptRunning = true;
scriptPaused = false;
currentStep = 0;
// 🎯 關鍵: 根據當前模式執行對應腳本
switch(currentMode) {
case 0: runModeA(); break; // 執行模式 A 的腳本
case 1: runModeB(); break; // 執行模式 B 的腳本
case 2: /* 模式 C 無腳本 */ break;
case 3: /* 模式 D 無腳本 */ break;
}
scriptRunning = false;
scriptPaused = false;
showCurrentMode(); // 執行完畢後重新顯示模式燈號
} else if (scriptPaused) {
// 從暫停狀態繼續執行
scriptPaused = false;
Serial.println("繼續執行腳本...");
}
delay(300); // 防彈跳
}
// ==========================================
// 4. START 鍵 (播放鍵): 暫停腳本
// ==========================================
if (Mini.PS2.START) {
if (scriptRunning && !scriptPaused) {
// 暫停腳本
scriptPaused = true;
Mini.M1.set(0);
Mini.M2.set(0);
Serial.println("腳本已暫停 (按 TRIANGLE 繼續)");
delay(300);
}
}
// ==========================================
// 5. CIRCLE 鍵: 緊急停止馬達
// ==========================================
if (Mini.PS2.CIRCLE) {
Mini.M1.set(0);
Mini.M2.set(0);
}
// ==========================================
// 6. 舵機配置
// ==========================================
if (Mini.PS2.L1) { current_RC2 = 135; Mini.RC2.set(135); }
else if (Mini.PS2.L2) { current_RC2 = 45; Mini.RC2.set(45); }
if (Mini.PS2.R1) { current_RC1 = 135; Mini.RC1.set(135); }
else if (Mini.PS2.R2) { current_RC1 = 45; Mini.RC1.set(45); }
if (Mini.PS2.SQUARE) { current_RC3 = 180; Mini.RC3.set(180); }
else if (Mini.PS2.CROSS) { current_RC3 = 0; Mini.RC3.set(0); }
delay(20);
}
// 顯示當前模式 (RGB LED 指示燈)
void showCurrentMode() {
Serial.print("當前模式: ");
Serial.println(currentMode);
// 控制板載 RGB LED 顯示不同顏色
switch(currentMode) {
case 0: // 紅燈 - 模式 A
Mini.RGB1.setRGB(255, 0, 0);
Mini.RGB2.setRGB(255, 0, 0);
break;
case 1: // 綠燈 - 模式 B
Mini.RGB1.setRGB(0, 255, 0);
Mini.RGB2.setRGB(0, 255, 0);
break;
case 2: // 藍燈 - 模式 C
Mini.RGB1.setRGB(0, 0, 255);
Mini.RGB2.setRGB(0, 0, 255);
break;
case 3: // 黃燈 - 模式 D
Mini.RGB1.setRGB(255, 255, 0);
Mini.RGB2.setRGB(255, 255, 0);
break;
}
}
// ========================================
// 🔴 模式 A - 起點1
// ========================================
void runModeA() {
// 步驟 1: 前進 (2000ms)
Mini.M1.set(100); Mini.M2.set(100);
delay(2000);
Mini.M1.set(0); Mini.M2.set(0);
// 步驟 2: 右自轉 (500ms)
Mini.M1.set(70); Mini.M2.set(-70);
delay(500);
Mini.M1.set(0); Mini.M2.set(0);
// 步驟 3: 前進 (1000ms)
Mini.M1.set(100); Mini.M2.set(100);
delay(1000);
Mini.M1.set(0); Mini.M2.set(0);
Mini.M1.set(0);
Mini.M2.set(0);
}
// ========================================
// 🟢 模式 B - 起點2
// ========================================
void runModeB() {
// 步驟 1: 左自轉 (500ms)
Mini.M1.set(-70); Mini.M2.set(70);
delay(500);
Mini.M1.set(0); Mini.M2.set(0);
// 步驟 2: 前進 (3000ms)
Mini.M1.set(100); Mini.M2.set(100);
delay(3000);
Mini.M1.set(0); Mini.M2.set(0);
// 步驟 3: 停止 (500ms)
Mini.M1.set(0); Mini.M2.set(0);
delay(500);
Mini.M1.set(0); Mini.M2.set(0);
Mini.M1.set(0);
Mini.M2.set(0);
}