Mpu6050 as motion detection sensor

Hi i am trying to use mpu6050 accelerator as a motion detection sensor. The acc (x,y,z) data is calculated to gather an average value. I assigned thresholds while i get the average number as (1.0) . If the readings off the threshold i am hoping to get a distinctive pulsing sound every 6 seconds from a buzzer. And if the the values are between the threshold then i want to play another tone . In this case because i don't have a buzzer, i used led(13) to simulate it. I tried to write some code inside the loop but i think they interfere with each other. When it waits for the interval i don't want reading to be interrupted. So eventually what i want arduino to do is ; while running beep every 6 seconds while staying still for 3 seconds play a sound. Any help appreciated. Thanks.

void loop () {
Arx  = accel_t_gyro.value.x_accel; 
  Ary  =accel_t_gyro.value.y_accel; 
  Arz  = accel_t_gyro.value.z_accel;
  Amag = sqrt(square(Arx)+square(Ary)+square(Arz));
  Amag = Amag/16840;         // After this if there is  no movement in any axis it reads almost 1.0 all the time.


  Serial.println(Amag); // checking the reading

  tolerance = 0.15;
  xNorm = 1.0;

  if (Amag >(xNorm+tolerance)||Amag < (xNorm-tolerance))
  {

    Running();
    Serial.println("Running");
  }

  else {
    Serial.println("Stopped");
    Stopped(); 
  }

  delay(200);

}

}

void Running(){
  digitalWrite(ledPin, HIGH);
  delay(500);

  digitalWrite(ledPin, LOW);
  delay(500);
}


void Stopped(){
  if ((unsigned long)(millis() - waitUntil) >= interval) {                // interval value is 6000.
    digitalWrite(13, !digitalRead(13)); // Toggle the LED on Pin 13, later on i want to get a sound.
    waitUntil = waitUntil + interval; // wait another interval cycle
  }

OK. I made some progress already.

#define interval 6000
double Amag,xNorm,tolerance;
const int ledPin = 13;
int ledState = LOW;
unsigned long waitUntil=0;
long previousMillis = 0; 
const int numReadings = 20;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

void setup()
for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;    


void loop()

 double Arx, Ary,Arz;

Arx  = accel_t_gyro.value.x_accel; 
  Ary  =accel_t_gyro.value.y_accel; 
  Arz  = accel_t_gyro.value.z_accel;
  Amag = sqrt(square(Arx)+square(Ary)+square(Arz));
  Amag = Amag/16840;
  Amag= Amag*100;
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = Amag; 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings) {             

    index = 0; 
    // calculate the average:
    average = total / numReadings;  

  }
  // send it to the computer as ASCII digits
  Serial.print("average=  ");
  Serial.print(average); 
  Serial.print("Amag=  ");
  Serial.println(Amag);  
  delay(5);        // delay in between reads for stability            



  tolerance = 10; // Will be adjusted with a pot
  xNorm = 95; // open for fine tuning

  if (average >(xNorm+tolerance)||average < (xNorm-tolerance))
  {
     Serial.println("Running  ");
     unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;   
      if (ledState == LOW)
        ledState = HIGH;

      else
        ledState = LOW;
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
  }
  else {
    Stopped();
    
  }
}


void Stopped(){
  Serial.println("Stopped  ");
  if ((unsigned long)(millis() - waitUntil) >= 100) { // check for rollover
    // It's time to do something!
    digitalWrite(13, !digitalRead(13)); // Toggle the LED on Pin 13
    waitUntil = waitUntil + 100; // wait another interval cycle
    
  }

These are core parts.I applied smoothing to get stable values. And this gives me some time for shifting between states.
I think i have to connect a buzzer . Find a nice tune, and make some adjustments afterwards.