What's new

Help Mga lods pahelp po arduino coding

Vermouth the Gin

✌𝓝𝓮𝓿𝓮𝓻 𝓑𝓮𝓽𝓽𝓮𝓻 ✌
Established
Joined
May 15, 2019
Posts
1,415
Solutions
1
Reaction
323
Points
768
Mga lods pahelp po about arduino coding,
Subject namin microprocessor kaka start ko lng po sa arduino
Ni study ko na po din to kasi para sa future programing ng thesis ko

Pahelp po paano mag shorten ng code pero same output result lng in po

Maraming Salamat po sa help!

ito po yung code

int ledPin = (9,10);
void setup(){
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop(){

digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
delay(500);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
delay(500);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
delay(500);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
delay(500);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
delay(500); digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
delay(500); digitalWrite(10, HIGH);
delay(500);
digitalWrite(10, LOW);
delay(500);

}

ardeniu.jpg ardeniu1.jpg
 

Attachments

Hi! I can see that your code is controlling two LEDs connected to pins 9 and 10 of your Arduino board. To shorten your code, you can use a loop to repeat the same code for both LEDs. Here's an example:

Code:
int ledPins[] = {9, 10}; // declare an array with the LED pins

void setup() {
  // set the LED pins as outputs
  for (int i = 0; i < 2; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  // loop through each LED and blink it 3 times
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
      digitalWrite(ledPins[i], HIGH);
      delay(500);
      digitalWrite(ledPins[i], LOW);
      delay(500);
    }
  }
}

In this example, we use an array ledPins to store the LED pins, and a for loop to set them as outputs in the setup() function. Then we use two nested for loops in the loop() function to loop through each LED and blink it three times, with a delay of 500ms between each blink.

I hope this helps! Let me know if you have any questions.
 

Similar threads

Back
Top