
Video
Gambar Rajah Litar

Kod Sumber
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// LED Police Lights
4// ─────────────────────────────────────────
5// The red and blue LEDs each flash 3 times quickly in turn, creating a
6// police strobe effect. Shows for loops and timing control.
7//
8// Pins:
9// D2 -> Red LED
10// D3 -> Blue LED
11// Each LED with a 330 ohm resistor
12
13// Flash time (ms). A smaller value flashes faster
14int delayTime = 60;
15
16void setup() {
17 pinMode(2,OUTPUT); // Red
18 pinMode(3,OUTPUT); // Blue
19}
20
21void loop() {
22 // Red LED flashes 3 times
23 for(int i=0;i<3;i++){
24 digitalWrite(2,HIGH);
25 delay(delayTime);
26 digitalWrite(2,LOW);
27 delay(delayTime);
28 }
29
30 // Blue LED flashes 3 times
31 for(int i=0;i<3;i++){
32 digitalWrite(3,HIGH);
33 delay(delayTime);
34 digitalWrite(3,LOW);
35 delay(delayTime);
36 }
37}
38
Soalan Lazim
How can I make the strobe effect look more realistic?
Real police lights flash briefly a few times in every color. If you use a `for` loop to quickly turn on one LED three times (e.g., 50 ms on, 50 ms off) and then move on to the next one, you’ll get an effect that closely resembles a police light.
Can a red and a blue LED use the same resistor?
They work, but their brightness will differ: the forward voltage of the blue LED (about 3.2 V) is higher than that of the red one (about 1.9 V), so less current flows through them with the same resistor. To achieve the same brightness, you can choose a lower resistance for the blue LED; the LED resistance calculator performs this calculation.
What is the purpose of the "for" loop here?
This is used to repeat the same blinking pattern a specific number of times. Instead of copying and pasting the code multiple times, you can control the repetition from a single location by writing `for (int i = 0; i < 3; i++)`.
What can I use to make the flash brighter?
Design the circuit so that the current drawn from an Uno pin does not exceed 20 mA; the pin does not automatically limit the current to this value. For more powerful LEDs or LED strips, you need to drive them from a suitable separate power supply using a transistor or MOSFET rated for the load current; do not connect high-current loads directly to the pin.
