0
\$\begingroup\$

I'm relatively new to programming, Arduinos, and online forums in general. I would really appreciate an explanation for one of the basic tutorials for Arduinos:

I connected 8 LEDs to 8 pins (2 through 9) and programmed a short script to light the LEDs up successively, based on a schematic (found here): http://www.instructables.com/files/deriv/F45/G030/FVT7A09T/F45G030FVT7A09T.LARGE.jpg

A sample code suggests:

int ledPins[] = {2,3,4,5,6,7,8,9);

void setup(){
  for(int i = 0; i<8; i++){
    pinMode(ledPins[i], OUTPUT);
  }
}    
void loop(){
  oneAfterAnotherNoLoop();
}    
void oneAfterAnotherNoLoop(){
  digitalWrite(ledPins[0], HIGH);
  delay(10);
  digitalWrite(ledPins[0], LOW);
  delay(10);
  ...
}

However, I'm confused about the function of the setup() method! I'm told that the setup() method only runs once, so shouldn't the lights stop blinking after one iteration? Or is it a way to initialize ALL 8 pins continuously (and not successively)? Additionally, why is the setup() and oneAfterAnotherNoLoop() methods needed? I wrote everything into the loop() method:

void loop(){
  for(int i = 0; i<10; i++){
    pinMode(ledPins[i], OUTPUT); 
    digitalWrite(ledPins[i], HIGH);
    delay(250);
    digitalWrite(ledPins[i], LOW);
    delay(250);
  }
}

And this ran pretty well, except after maybe 20 iterations, it just got stuck on the first LED light :(

Any feedback would be greatly appreciated! Thanks!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ setup() is defining the pins as outputs sequentially so only needs to run once. Your loop() is counting 0 to 9 (<10) and then will finish. \$\endgroup\$ Commented Jun 26, 2013 at 18:16
  • \$\begingroup\$ pinMode(); initializes the pin driver. It can configure a single pin as either INPUT OR OUTPUT and that usually only needs to be done once. Once configured as output, a digitalWrite(); will drive the output pin LOW or HIGH. \$\endgroup\$
    – jippie
    Commented Jun 26, 2013 at 18:25
  • \$\begingroup\$ post your whole program \$\endgroup\$
    – vicatcu
    Commented Jun 26, 2013 at 19:37

2 Answers 2

5
\$\begingroup\$

The setup() method run only once because it is used in order to set the direction of the pins (Input or Output).

The oneAfterAnotherNoLoop() method produce only a blink of the LEDs. It turns on the LEDs, wait a little bit, turns off the LEDs and wait another little bit.

The loop() method is an infinite loop that calls the oneAfterAnotherNoLoop() method in order to produce an infinite cycle of blinking.

In your code you initialize the pins every time and this isn't needed. Try this:

void loop() {
  for (int i = 0; i < 8; i++) {       // Initialize pins
    pinMode(ledPins[i], OUTPUT);      // Set pin i as an OUTPUT
  }
  while (1) {                         // Infinite loop
    for (int i = 0; i < 8; i++) {
      digitalWrite(ledPins[i], HIGH); // Drive LED i HIGH
      delay(250);                     // Wait
      digitalWrite(ledPins[i], LOW);  // Drive LED i LOW
      delay(250);                     // Wait
    }
  }
}
\$\endgroup\$
0
0
\$\begingroup\$

The Arduino runtime environment runs your setup method once at startup and then runs the loop method over and over again.

Therefore the setup method is the appropriate place to put your initialisation code. This includes things like setting pins to be output pins rather than input pins. This means you don't need your pinmode statement in your loop method. This saves unnecessary processing time.

The person who wrote the program ("sketch") you based yours on decided to create additional methods. This can be a good way to break up complex programs into simpler to understand pieces. In this case it wasn't particularly useful.

I don't see why your sketch stopped after a few iterations. Did you touch something? Did it run OK again after you reset it?

\$\endgroup\$
2
  • \$\begingroup\$ "I don't see why your sketch stopped after a few iterations. Did you touch something? Did it run OK again after you reset it?" No it didn't! Even more strange was: on the 18th and 19th iteration, the lights lit up "back and forth" between the 2nd and 3rd LED (and continued to run successively down the LED stretch) before getting stuck on the 20th iteration! Really confused! \$\endgroup\$
    – batlike
    Commented Jun 26, 2013 at 18:36
  • \$\begingroup\$ @batlike: I'd try adding a new line int i; after void loop() { \$\endgroup\$ Commented Jun 26, 2013 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.