LED右移
2025/08/17 20:04
瀏覽70
迴響0
推薦0
引用0
程式碼:
// 定義 LED 數量與起始腳位
const int ledCount = 8;
const int ledPins[ledCount] = {2, 3, 4, 5, 6, 7, 8, 9}; // LED0(左) ~ LED7(右)
void setup() {
// 設定所有 LED 腳位為輸出模式
for (int i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// 右移效果:從左到右點亮 LED (LED7 → LED0)
for (int pos = ledCount - 1; pos >= 0; pos--) {
// 關閉所有 LED
for (int i = 0; i < ledCount; i++) {
digitalWrite(ledPins[i], LOW);
}
// 點亮當前位置的 LED
digitalWrite(ledPins[pos], HIGH);
delay(200); // 控制移動速度(單位:毫秒)
}
}
測試影片網址:https://www.youtube.com/shorts/f1Ak1poGHmQ
你可能會有興趣的文章:
限會員,要發表迴響,請先登入



