Notifications
Clear all
0
02/05/2024 12:38 pm
Topic starter
Hi,
I am using 8 digital pins to control the 8 LEDs separately. Instead of writing "pinMode(pin,OUTPUT)" for every other pin, is there any short method?
3 Answers
2
29/08/2024 7:23 am
For loop will do the job if the pins are in series:
for (int i = 2; i <= 9; i++) {
pinMode(i, OUTPUT);
}
If pins are random, use an array like this:
int buttons[]={2,4,5,8,10,13};
for(int i:buttons){
pinMode(i, OUTPUT);
}
1
03/05/2024 7:42 am
You can use for loop for this; here's how
for (int i = 2; i <= 9; i++) {
pinMode(i, OUTPUT);
}
This loop goes from pin 2 to 9 and sets each pin as OUTPUT.
.
1
26/09/2024 5:09 am
Here's an example using a for loop to configure 8 digital pins (from pin 2 to pin 9) as outputs:
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // Array of pin numbers
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT); // Set each pin as an output
}
}
void loop() {
// Your code to control LEDs goes here
}
If the pins are not in a series, you can still use an array:
int ledPins[] = {2, 5, 7, 8, 10, 12, 13, A0}; // Array of specific pin numbers
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT); // Set each specified pin as an output
}
}
void loop() {
// Your code to control LEDs goes here
}