本文使用TinkerCad Circuits來示範LED燈的應用:「走馬燈看板」。
材料(Materials):
- Arduino UNO R3 x 1
- Small Breadboard x 10
- 220 ohm Resistor x 10
- LED x 10
電路(Circuit):
十顆 LED 燈的陽極(Anode)分別接到 UNO 的數位腳位 2 ~ 11。
程式碼(The Code):
無使用陣列版本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int sPin = 2; // The base pin ID | |
int nLEDs = 10; // The number of LEDs | |
int delayMS = 100; | |
void setup() | |
{ | |
for (int i = sPin; i < sPin + nLEDs; i++) | |
{ | |
pinMode(i, OUTPUT); | |
} | |
} | |
void loop() | |
{ | |
for(int i = sPin; i < sPin + nLEDs; i++) | |
{ | |
digitalWrite(i, LOW); | |
delay(delayMS); | |
} | |
for(int i = sPin; i < sPin + nLEDs; i++) | |
{ | |
digitalWrite(i, HIGH); | |
delay(delayMS); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int ledPins[] = {2,3,4,5,6,7,8,9,10,11}; // The LED pins | |
int num = sizeof(ledPins) / sizeof(int); | |
int delayMS = 100; | |
void setup() | |
{ | |
for (int i = 0; i < num ; i++) | |
{ | |
pinMode(ledPins[i], OUTPUT); | |
} | |
} | |
void loop() | |
{ | |
for (int i = 0; i < num ; i++) | |
{ | |
digitalWrite(ledPins[i], LOW); | |
delay(delayMS); | |
} | |
for (int i = 0; i < num ; i++) | |
{ | |
digitalWrite(ledPins[i], HIGH); | |
delay(delayMS); | |
} | |
} | |
範例連結(Example Link):
https://www.tinkercad.com/things/70zn6GiiAsQ
沒有留言:
張貼留言