add multiple blinky

This commit is contained in:
2024-11-11 07:29:01 +05:30
parent 0ad46d8b85
commit d89f54c6e8

View File

@@ -69,7 +69,7 @@ sleep_ms(500);
This program blinks an LED connected to one of the GPIO(General Purpose Input/Output) ports.
For this example, I used Pin 20 == GP15. I connected a resistor to this pin and connected an LED in series with the resistor to the ground, as shown in the image below.
For this example, I used Pin 20 == GP15. I connected a resistor to this pin and connected an LED in series with the resistor to the ground, as shown in the image below. This resistor limits the amount of current drawn by the LED. There are other ways to limit the current, which are discussed in this document later.
```c
const uint LEDPin = 15;
@@ -89,3 +89,45 @@ while (true)
```
The only change here is in line 1, `PICO_DEFAULT_LED_PIN` is changed to `15`, to represent GP15. Remember: in the program, a GPIO pin is represented by the GP number and NOT by the pin number, as shown in the [Pico's Pinout](https://pico.pinout.xyz/).
---
## Multiple Blinky
This program controls multiple LED connected to various GPIO ports. Here, the program is written such that it looks like a loading bar. For this setup, internal current limits are used. They are toggled programatically as shown in the code below.
```c
int LED1 = 11;
int LED2 = 12;
int LED3 = 13;
int LED4 = 14;
int LED5 = 15;
int LEDS[5] = {LED1, LED2, LED3, LED4, LED5};
for (int i = 0; i < 5; i++)
{
gpio_init(LEDS[i]);
gpio_set_dir(LEDS[i], GPIO_OUT);
gpio_set_drive_strength(LEDS[i], GPIO_DRIVE_STRENGTH_2MA);
}
while (true)
{
for (int i = 0; i < 5; i++)
{
gpio_put(LEDS[i], true);
sleep_ms(100);
}
for (int i = 0; i < 5; i++)
{
gpio_put(LEDS[i], false);
sleep_ms(100);
}
}
sleep_ms(500);
```
### Explanation:
- `int LEDS[5] = {...};` declares a list of integers of length 5. GPIO numbers of the 5 connected LEDS are stored here.
- `gpio_set_drive_strength(LED[i], GPIO_DRIVE_STRENGTH_2MA)` sets the **Drive Strength**/Current Limit of GPIO port. The RP2040 C SDK provides 4 drive strengths, 2mA, 4mA, 8mA and 12mA.
The rest of the code is similar to the prior examples, which toggles the LEDS one by one, in series and turns them off, creating a loading effect. Visual output can be seen below.