adding an extra code to the loop but do not want to repeat itself

Hi guys,

I am doing a project for a memory game. My wiring has 4 LEDs with different colours (red,pink,white,orange) where each is attached to 4 different buttons. I also connected lcd and there is another single button which is attached to lcd.

When you press the button that is attached to the lcd, I get random text on the screen giving any random words of colours of the 4 LEDs (eg:red, pink, white, orange). If the screen gives white, I press the button which is attached to white and the white LED lights up.

I used the code below (CODE 1) and it is perfectly working so far, however I want to add another code to the beginning of the loop which is ; all of the LEDs are going to light up then after 5 seconds they all will go off. CODE 2 is the code needed for all LEDs to go on and off after 5 seconds. However, when I try to combine both codes and add CODE 2 to the beginning of the CODE 1, it does not work the way I want. The leds turn on and go off after 5 seconds and keeps repeating itself. What function can I use to restrict the CODE 2 to happen once at the beginning and not repeat itself and CODE 1 to keep repeating itself?

CODE 1:
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const char *colors={
" RED ",
" PINK ",
" ORANGE ",
" WHITE "
};
long colorrandom;

//Constants
const int redLed = 6;
const int pinkLed = 7;
const int whiteLed = 8;
const int orangeLed = 9;

//Variables
int buttonValue; //Stores analog value when button is pressed

#define BUTTON 10
int val = 0;

void setup() {
// set up the LCD’s number of columns and rows:
lcd.begin(16, 1);

// Print a message to the LCD.
randomSeed(666);

//LEDS setup
pinMode(redLed, OUTPUT);
pinMode(pinkLed, OUTPUT);
pinMode(whiteLed, OUTPUT);
pinMode(orangeLed, OUTPUT);

}

void loop() {

val = digitalRead(BUTTON);
buttonValue = analogRead(A0); //Read analog value from A0 pin
if (val == HIGH)
{ colorrandom = random(sizeof(colors)/sizeof(char*));
lcd.setCursor(0, 0); //placement of text in LCD
lcd.print(colors[colorrandom]);
}

//For 1st button:
if (buttonValue>=925 && buttonValue<=950){
digitalWrite(redLed, HIGH);

}
//For 2nd button:
else if (buttonValue>=840 && buttonValue<=870){
digitalWrite(pinkLed, HIGH);

}
//For 3rd button:
else if (buttonValue>=1010 && buttonValue<=1030){
digitalWrite(whiteLed, HIGH);

}
//For 4th button:
else if (buttonValue>=5 && buttonValue<=15){
digitalWrite(orangeLed, HIGH);

}
//No button pressed, turn off LEDs
else{
digitalWrite(redLed, LOW);
digitalWrite(pinkLed, LOW);
digitalWrite(whiteLed, LOW);
digitalWrite(orangeLed, LOW);

}
}

CODE 2:
digitalWrite(redLed, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(pinkLed, HIGH);
digitalWrite(whiteLed, HIGH);
digitalWrite(orangeLed, HIGH);
delay(5000); // wait for a second
digitalWrite(redLed, LOW); // turn the LED on (HIGH is the voltage level)
digitalWrite(pinkLed, LOW);
digitalWrite(whiteLed, LOW);
digitalWrite(orangeLed, LOW);
delay(5000);

Would be so happy to get a useful answer

thanks!!

What function can I use to restrict the CODE 2 to happen once at the beginning and not repeat itself

Use a boolean variable to prevent the code running again. Initially set the boolean to false and only run the code when it is false. In the code that should run once only set the boolean to true.

Of course, if you only want the code to run when the Arduino is first powered up or reset then you could just put it in setup()