Running code once

I am trying to write code get my arduino to run code once as a timer. Basically when you start the arduino a tone will sound after a slight delay a LED will light for a period of time will extinguish then after a delay another tone will sound then a second LED will light for a period of time followed by another tone. At this stage I would like the arduino then to become dormant until the reset button is pressed to start the cycle again.
I have absolutely no idea how to do this. Any help would be appreciated.

Adrian

const int LED1 = 11;
const int LED2 = 12;

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

//tone 1 low
void loop()

{
  delay (5000);
  tone(7, 250, 500);// play a note on pin 7 for 500 ms:
  delay(1000);
  noTone(7);// turn off tone function for pin 7:  
  
  //led 1
 
 digitalWrite(LED1,HIGH);//Turn on LED1
  delay(1000);//turn on led 1 for 120 seconds *adjust time for final build
  
  digitalWrite(LED1,LOW);//Turn off LED1
    
  delay (1000);
  
  //tone 2
  
  tone(7, 494, 500);// play a note on pin 7 for 500 ms:
  delay(1000);
  noTone(7);// turn off tone function for pin 7:  
  
  //led 2
  
  digitalWrite(LED2,HIGH);//Turn on LED2
  delay(1000);//turn on led 1 for 120 seconds *adjust time for final build
  
  digitalWrite(LED2,LOW);//Turn off LED2
  
  delay (500);
  
  //tone 3
  //tone(7, 600, 3000);// play a note on pin 7 for 500 ms:
  //delay(1000);
  //delay (500);
 tone(7, 600, 5000);// play a note on pin 7 for 500 ms:
  delay(1000);
  //delay (500);
 //tone(7, 600, 3000);// play a note on pin 7 for 500 ms:
  //delay(1000); 
  
  noTone(7);// turn off tone function for pin 7:

}

Everything you want to run just once after reset is in the setup() function.
Copy all the code from the loop() at the bottom of the setup() function. The loop() wil be empty, a '{' and a '}' with nothing in between.

void setup() {
  // everything here
  ...
}

void loop() {
  // add sleep code later, for now empty.
}

Thanks for the reply and advice. I solved the problem using

for(; :wink: { /empty/ }

It now does exactly what I wanted. So now the code looks like

const int LED1 = 11;
const int LED2 = 12;

void setup()
{
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
}
//tone 1 low
void loop()
{
  delay (5000);
  tone(7, 250, 500);// play a note on pin 7 for 500 ms:
  delay(1000);
  noTone(7);// turn off tone function for pin 7:  
  
  //led 1
  digitalWrite(LED1,HIGH);//Turn on LED1
  delay(1000);//turn on led 1 for 120 seconds *adjust time for final build
  
  digitalWrite(LED1,LOW);//Turn off LED1
    
  delay (1000);
  
  //tone 2
  
  tone(7, 494, 500);// play a note on pin 7 for 500 ms:
  delay(1000);
  noTone(7);// turn off tone function for pin 7:  
  
  //led 2
  
  digitalWrite(LED2,HIGH);//Turn on LED2
  delay(1000);//turn on led 1 for 120 seconds *adjust time for final build
  
  digitalWrite(LED2,LOW);//Turn off LED2
  
  delay (500);
  
 tone(7, 600, 5000);// play a note on pin 7 for 500 ms:
  delay(1000); 
  
  noTone(7);// turn off tone function for pin 7:

for(;;) { /*empty*/  }
}

Or you could've put all the code in setup() and left loop() empty.

AWOL:
Or you could've put all the code in setup() and left loop() empty.

Could have and did try but kept getting compile errors.
This solution worked :slight_smile: This 60 year old brain sometimes go for the less obvious answer.

Adrian

adrianfu:
Could have and did try but kept getting compile errors.

You probably just missed or added an extra bracket somewhere. No biggie.

This 60 year old brain sometimes go for the less obvious answer.

Youngster :wink:

LarryD:
Youngster :wink:

Body says 102 mind says 30

Adrian

"It's not the years, it's the mileage" : Indiana Jones - It's not the years, it's the mileage - YouTube

adrianfu, can you find the auto-format-text-layout-tool in the menu ? or use Ctrl+T.
If you use that, any missing brackets can easily be spotted.