Lesson 2: More LEDs
Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
English
One pin for each LED
- Each LED needs its own pin and its own
pinMode. - In a real circuit each LED also needs a resistor (about 220 Ω), so it does not burn out.
void setup() {
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
delay(500);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
delay(500);
}
- Press Run: the two LEDs take turns.
- The board is very fast. Two
digitalWritelines with nodelaybetween them happen at (almost) the same time.
English
A traffic light
| Step | Red (10) | Amber (9) | Green (8) | Time |
|---|---|---|---|---|
| 1 | on | off | off | 2 s |
| 2 | on | on | off | 1 s |
| 3 | off | off | on | 2 s |
| 4 | off | on | off | 1 s |
- Then it starts again at step 1. This is the order in the UK.
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Pins 2 (blue) and 3 (red) take turns: pin 2 on and pin 3 off for 300 ms, then pin 2 off and pin 3 on for 300 ms, again and again.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Make the traffic light from the table: red on pin 10, amber on pin 9, green on pin 8. Steps of 2 s, 1 s, 2 s, 1 s, then again.
Nhấn Chạy để xem kết quả ở đây.