#include <Servo.h>
int main_switch = 0; // 總開關狀態讀取int master_switch_count = 0; // 總開關狀態切換計數器
int light_switch = 0; // 燈泡開關狀態讀取int light_switch_count = 0; // 燈泡開關狀態切換計數器
int servo_motor_left = 0; // 伺服馬達左轉按鈕狀態int servo_motor_left_count = 0; // 左轉按鈕防彈跳/狀態計數器
int servo_motor_right = 0; // 伺服馬達右轉按鈕狀態int servo_motor_right_count = 0; // 右轉按鈕防彈跳/狀態計數器
int servo_motor_angle = 0; // 伺服馬達角度
int motor_forward = 0; // 直流馬達控制按鈕狀態int motor_forward_count = 0; // 直流馬達控制狀態計數器
Servo servo_4; // 宣告伺服馬達物件
void setup(){ // 輸出腳位設定(3, 6, 5, 7, 8, 2) pinMode(3, OUTPUT); // PWM控制腳位(用於指示燈或馬達速度控制) pinMode(6, OUTPUT); pinMode(5, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(2, OUTPUT); // 燈泡控制腳位
// 輸入腳位設定(A0 總開關、A1 燈泡開關、A2 左轉、A3 復位、A4 右轉、A5 直流馬達按鈕) pinMode(A0, INPUT); pinMode(A1, INPUT); pinMode(A2, INPUT); pinMode(A3, INPUT); pinMode(A4, INPUT); pinMode(A5, INPUT);
servo_4.attach(4, 500, 2500); // 伺服馬達接腳4,設定脈寬範圍
// 初始化指示燈PWM analogWrite(3, 204); // 3腳輸出中等亮度(紅燈) analogWrite(6, 0); analogWrite(5, 0);
master_switch_count = 1; // 初始總開關狀態計數器 servo_motor_angle = 90; // 伺服馬達初始位置設定90度}
void loop(){ // --- 伺服馬達復位按鈕 --- if (digitalRead(A3) == 1) { // A3腳復位按鈕按下 servo_motor_angle = 90; servo_4.write(servo_motor_angle); // 伺服馬達回復90度 }
// --- 伺服馬達左轉控制 --- servo_motor_left = digitalRead(A2); // 讀取左轉按鈕狀態 if (servo_motor_angle >= 179) { // 防止角度超過179度 servo_motor_angle = 179; analogWrite(3, 255); // 紅燈全亮(警示) analogWrite(6, 0); analogWrite(5, 0); } if (servo_motor_left == 1 && servo_motor_left_count == 1) { // 按鈕第一次偵測到按下 analogWrite(3, 255); // 紅燈全亮 analogWrite(6, 255); // 綠燈全亮 (混合顏色) analogWrite(5, 0); servo_motor_angle = (servo_motor_angle + 5); // 角度增加5度向左轉 servo_4.write(servo_motor_angle); servo_motor_left_count = 2; // 狀態改為已按下,防止重複執行 delay(200); // 按鈕延遲 analogWrite(3, 51); // 將燈恢復中間亮度 analogWrite(6, 204); analogWrite(5, 0); } if (servo_motor_left == 0 && servo_motor_left_count == 2) { // 按鈕放開,重置狀態 servo_motor_left_count = 1; }
// --- 伺服馬達右轉控制 --- servo_motor_right = digitalRead(A4); // 讀取右轉按鈕狀態 if (servo_motor_angle <= 1) { // 防止角度低於1度 analogWrite(3, 255); // 紅燈全亮(警示) analogWrite(6, 0); analogWrite(5, 0); servo_motor_angle = 1; } if (servo_motor_right == 1 && servo_motor_right_count == 1) { // 按鈕第一次偵測到按下 analogWrite(3, 51); // 紅燈低亮度 analogWrite(6, 102); // 綠燈中低亮度 analogWrite(5, 255); // 藍燈全亮 (混合顏色) servo_motor_angle = (servo_motor_angle - 5); // 角度減5度向右轉 servo_4.write(servo_motor_angle); servo_motor_right_count = 2; // 狀態改為已按下,防止重複執行 delay(200); analogWrite(3, 51); // 燈恢復中間亮度 analogWrite(6, 204); analogWrite(5, 0); } if (servo_motor_right == 0 && servo_motor_right_count == 2) { // 按鈕放開,重置狀態 servo_motor_right_count = 1; }
// --- 直流馬達控制(正轉、停止、反轉、停止循環)--- motor_forward = digitalRead(A5); if (motor_forward == 1 && motor_forward_count == 1) { digitalWrite(7, LOW); digitalWrite(8, HIGH); // 正轉 motor_forward_count = 2; } if (motor_forward == 0 && motor_forward_count == 2) { motor_forward_count = 3; } if (motor_forward == 1 && motor_forward_count == 3) { digitalWrite(7, LOW); digitalWrite(8, LOW); // 停止 motor_forward_count = 4; } if (motor_forward == 0 && motor_forward_count == 4) { motor_forward_count = 5; } if (motor_forward == 1 && motor_forward_count == 5) { digitalWrite(7, HIGH); digitalWrite(8, LOW); // 反轉 motor_forward_count = 6; } if (motor_forward == 0 && motor_forward_count == 6) { motor_forward_count = 7; } if (motor_forward == 1 && motor_forward_count == 7) { digitalWrite(7, LOW); digitalWrite(8, LOW); // 停止 motor_forward_count = 8; } if (motor_forward == 0 && motor_forward_count == 8) { motor_forward_count = 1; }
// --- 總開關控制 --- main_switch = digitalRead(A0); if (main_switch == 1 && master_switch_count == 1) { analogWrite(3, 51); // 綠燈亮 analogWrite(6, 204); analogWrite(5, 0); servo_4.write(90); // 伺服馬達復位90度 light_switch_count = 1; servo_motor_left_count = 1; servo_motor_right_count = 1; motor_forward_count = 1; master_switch_count = 2; } if (main_switch == 0 && master_switch_count == 2) { master_switch_count = 3; } if (main_switch == 1 && master_switch_count == 3) { analogWrite(3, 204); // 紅燈亮 analogWrite(6, 0); analogWrite(5, 0); digitalWrite(2, LOW); // 燈泡關閉 digitalWrite(7, LOW); // 直流馬達停止 digitalWrite(8, LOW); servo_4.write(90); // 伺服馬達回90度 light_switch_count = 0; servo_motor_left_count = 0; servo_motor_right_count = 0; motor_forward_count = 0; master_switch_count = 4; } if (main_switch == 0 && master_switch_count == 4) { master_switch_count = 1; }
// --- 燈泡開關控制 --- light_switch = digitalRead(A1); if (light_switch == 1 && light_switch_count == 1) { digitalWrite(2, HIGH); // 燈泡開 light_switch_count = 2; } if (light_switch == 0 && light_switch_count == 2) { light_switch_count = 3; } if (light_switch == 1 && light_switch_count == 3) { digitalWrite(2, LOW); // 燈泡關 light_switch_count = 4; } if (light_switch == 0 && light_switch_count == 4) { light_switch_count = 1; }}