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(; { /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*/ }
}