Mega 2560 wiring question

Hello,
I'm a newbie at programming and using Arduino and have an issue I cannot figure out. For the coding example below, it loops the lights continuously. How do I change the coding so that it doesn't loop?

/* A simple program to sequentially turn on and turn off 3 LEDs */

int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;

void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
}

void loop() {
digitalWrite(LED1, HIGH); // turn on LED1

digitalWrite(LED4, HIGH); // turn on LED4
delay(500);
digitalWrite(LED4, LOW); // turn off LED4
delay(500);
}

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Put the code in setup. The code will only run one time.
Here is your code with modified. Note that the code is formatted with the autoformat tool and posted in code tags.

/* A simple program to sequentially turn on and turn off 3 LEDs */

int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;

void setup()
{
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);

   digitalWrite(LED1, HIGH); // turn on LED1
   digitalWrite(LED4, HIGH); // turn on LED4
   delay(500);
   digitalWrite(LED4, LOW); // turn off LED4
   delay(500);
}

void loop()
{
}

Another way using a flag (doneOnceAlready).

/* A simple program to sequentially turn on and turn off 3 LEDs */

const byte LED1 = 13; // save a byte
const byte LED2 = 12; // by using byte for pin numbers
const byte LED3 = 11; // const puts the pin numbers
const byte LED4 = 10; // in PROGMEM

bool doneOnceAlready = false;

void setup()
{
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);

   digitalWrite(LED1, HIGH); // turn on LED1
   digitalWrite(LED4, HIGH); // turn on LED4
   delay(500);
   digitalWrite(LED4, LOW); // turn off LED4
   delay(500);
}

void loop()
{
   if (doneOnceAlready == false)
   {
      digitalWrite(LED1, HIGH); // turn on LED1
      digitalWrite(LED4, HIGH); // turn on LED4
      delay(500);
      digitalWrite(LED4, LOW); // turn off LED4
      delay(500);
      doneOnceAlready = true;
   }
}

Awesome. Thanks for the help. I knew I was missing something. I just wasn't sure what it was.

Under the replies is a solution checkbox. Tick the one under the most useful reply to let others know that your problem is solved.

And here is another one

void loop()
{
  digitalWrite(LED1, HIGH); // turn on LED1
  digitalWrite(LED4, HIGH); // turn on LED4
  delay(500);
  digitalWrite(LED4, LOW); // turn off LED4
  delay(500);

  for (;;)
  {

  }
}

The code will get stuck in the for-loop. But I would consider post #1 the correct answer.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.