Make the code pause (on command) and let it continue (on command)

I want to make the Imperial March play while a move detector detects movement. It needs to stop when there is no movement en continue where it left off.. I tried a few things, but what happened is that it runs the whole Imperial March code and doesnt stop when there is no movement. I tried a "do" statement and at the end a while. I also tried it with while in the beginning the code i have now had an "if.." and "else..." statement. For "else" I tried to delay it, but that results in the following error:

In function 'void loop()':
error: too few arguments to function 'void delay(long unsigned int)'
error: at this point in file

Without the delay I'm not getting the error..

So the code

 /**
const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
 
const int buzzerPin = 8; 
const int ledPin1 = 12; 
const int ledPin2 = 13; 
const int moveme = 3; 

int movemeValue = 0;
 
int counter = 0;

const int firstSection()
{
  beep(a, 500);
  beep(a, 500);    
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);  
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
 
  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);  
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
}
const int secondSection()
{
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);    
  beep(fSH, 250);
 
  delay(325);
 
  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);  
  beep(cSH, 175);  
  beep(cH, 125);  
  beep(b, 125);  
  beep(cH, 250);  
 
  delay(350);
}
const int beep(int note, int duration)
{
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);
 
  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  }else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }
 
  //Stop tone on buzzerPin
  noTone(buzzerPin);
 
  delay(50);
 
  //Increment counter
  counter++;
}
 
void setup()
{
  //Setup pin modes
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(moveme, INPUT); 
  Serial.begin(9600);
}
 
void loop(){
/* Are the if and else correct?*/
  //Play first section
  //movedetction instal 
  
Serial.println(digitalRead(moveme));
movemeValue = digitalRead(moveme);
delay(10); 
 
   
if (movemeValue == 1){
 
  firstSection();
 
  //Play second section
  secondSection();
 
  //Variant 1
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 350);  
  beep(a, 125);
  beep(cH, 500);
  beep(a, 375);  
  beep(cH, 125);
  beep(eH, 650);
 
  delay(500);
 
  //Repeat second section
  secondSection();
 
  //Variant 2
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 650);  
 
  delay(650);
  
 
}
else {
 delay();} 
}

Can anyone help me with this? Thanks in advance!

I wrote a long reply to this and for some reason the browser refreshed and lost it.

Rather than me re write the original response, have a look in the arduino menu under file/examples/digital and you will find an example sketch called 'tone melody'

This shows how to play a tune using an array - one array holds the note to play, the other holds the duration. what you want is essentially the same as this sketch, but you will make a test between each tone to decide whether to play it or play nothing.

Duane B

rcarduino.blogspot.com

Dagmar:
Can anyone help me with this? Thanks in advance!

Is that really the code you're running? It appears to be entirely commented out.

In function 'void loop()':
error: too few arguments to function 'void delay(long unsigned int)'
error: at this point in file

Without the delay I'm not getting the error..

At which point? Putting each { and each } on its own line, and using Tools + Auto Format would make your code much more readable.

but you will make a test between each tone to decide whether to play it or play nothing.

This is indeed exactly what I want! But I don't know how to fix the array.. If I make an array whether there is movement or not it will still run the whole code. And testing which tone played would not do anything either right? Which conditions do I need in the array? So how do I make do test between each tone, if there is movement or not, without running the whole code.

I have put the auto format on it the make it more readable!

const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;

const int buzzerPin = 8; //speaker oranje
const int ledPin1 = 12; // groen
const int ledPin2 = 13; // wit 
const int moveme = 3; 

int movemeValue = 0;

int counter = 0;

const int firstSection()
{
  beep(a, 500);
  beep(a, 500);    
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);  
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);

  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);  
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);

  delay(500);
}
const int secondSection()
{
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);    
  beep(fSH, 250);

  delay(325);

  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);  
  beep(cSH, 175);  
  beep(cH, 125);  
  beep(b, 125);  
  beep(cH, 250);  

  delay(350);
}
const int beep(int note, int duration)
{
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);

  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  }
  else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }

  //Stop tone on buzzerPin
  noTone(buzzerPin);

  delay(50);

  //Increment counter
  counter++;
}

void setup()
{
  //Setup pin modes
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(moveme, INPUT); 
  Serial.begin(9600);
}

void loop(){
  /* if and else check if its the right place!*/
  //Play first section
  //movedetction instal 

  Serial.println(digitalRead(moveme));
  movemeValue = digitalRead(moveme);
  delay(10); 


  if (movemeValue == 1){

    firstSection();

    //Play second section
    secondSection();

    //Variant 1
    beep(f, 250);  
    beep(gS, 500);  
    beep(f, 350);  
    beep(a, 125);
    beep(cH, 500);
    beep(a, 375);  
    beep(cH, 125);
    beep(eH, 650);

    delay(500);

    //Repeat second section
    secondSection();

    //Variant 2
    beep(f, 250);  
    beep(gS, 500);  
    beep(f, 375);  
    beep(cH, 125);
    beep(a, 500);  
    beep(f, 375);  
    beep(cH, 125);
    beep(a, 650);  

    delay(650);


  }
  else {
delay(); 
  }
}

And im also wondering why i am having an error in the delay at my

else {
delay(); 
  }

part (at the end of the code) without delay(); I dont get the error message.. I want to have the delay unsigned long, so i tried to code that first by just delay(); then i tried delay(unsigned); and delay(unsigned long);, but the last two both got the same error:

.cpp: In function 'void loop()':
:175: error: expected primary-expression before 'unsigned'

And im also wondering why i am having an error in the delay at my
Code:

else {
delay();
}

part (at the end of the code) without delay(); I dont get the error message.

The delay() function takes an argument. How LONG do you want to wait? It needs to be some value. It is not going to just wait a little while and then resume.

I hate my browser, its just refreshed and lost my response again !

The quickest way for you to get the result you want is to use the example I mentioned previously. Its not the tune you want, but get the functionality you want first and one you know you can play a tune with pauses/stop/start where you want, change the tune.

Here is the array reference again it will help you to understand the example code
http://www.arduino.cc/en/Reference/Array

An array is basically a list of values, so instead of writting your code as -

tone(a);
tone(a);
tone(b);

you write it a little different to play an item from the list each time instead - look at the example to see how its done.

The two arrays in the example are melody and noteDurations.

The code loops over these, what you want is an additional line of code in the loop which tests to see if there is movement. If not do nothing, if there is play the tone.

Duane B