mpu6050

hello,
I am new to learning arduino and the mpu6050 and I would like to make a simple motion, and knock code with the mpu6050.
I was wondering how to integrate the two codes together that I found inline.

#include <Wire.h>
const int MPU=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup()
{
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
voidloop(){
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();

Serial.print("AcX=");Serial.print(AcX);
Serial.print("|AcY=");Serial.print(AcY);
Serial.print("|AcZ=");Serial.print(AcZ);
Serial.print("|Tmp=");Seria.print(Tmp/340.00+36.53);
Serial.print("|GyX=");Serial.print(GyX);
Serial.print("|GyY=");Serial.print(GyY);
Serial.print("|GyZ=");Serial.println(GyZ);
delay(333);
}

the code below to integrate with the code below to have a simple motion detect and motion knock.

 if(abs(ax) > 30000 || abs(ay) > 30000 || 
      abs(az) > 30000){
        //knock
//mp3 sound
      }  
      
      if((abs(ax) > 25000 && abs(ax) < 30000) || 
      (abs(ay) > 25000 && abs(ay) < 30000) || 
      (abs(az) > 25000 && abs(az) < 30000)){
        //motion detect
//different mp3 sound
      }

By far the best approach is to study each program carefully until you understand what each line does and why it might be needed. (I say "might be" because many programs you find on line have errors or are poor examples).

Then, take the relevant bits from each program, and use them to create a new program that does what you want.

If you run into trouble, post the code (using code tags) and explain what goes wrong.

voidloop(){Two words.

I do have it in the loop but for some reason it wont detect motion. When I use the first code I can clearly see the values but when I integrate the second code it dont work. This is what I have so far.

#include <Wire.h>
#include <DFPlayer_Mini_Mp3.h>
#include <SoftwareSerial.h>
bool detect1 = false;
bool detect2 = false;

const int MPU=0x68;
int16_t AcX,AcY,AcZ;
void setup()
{
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);

 mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
 mp3_set_volume (60);
}
voidloop(){
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();

if(abs(AcX) > 30000 || abs(AcY) > 30000 ||
      abs(AcZ) > 30000){
        //detect1
 if (detect1 == false){
      mp3_play (1); //play 0001.mp3
      delay (1200); //2.5 sec, time delay to allow 0001.mp3 to finish playing
      mp3_stop();
      detect1 = true;
      }     
      mp3_play(4);
      delay(2000);
    }
    else{
      detect1 = false;
      }
 if((abs(AcX) > 25000 && abs(AcX) < 30000) ||
      (abs(AcY) > 25000 && abs(AcY) < 30000) ||
      (abs(AcZ) > 25000 && abs(AcZ) < 30000)){
         if (detect2 == false){
      mp3_play (2); //play 0001.mp3
      delay (1200); //2.5 sec, time delay to allow 0001.mp3 to finish playing
      mp3_stop();
      detect2 = true;
      }     
      mp3_play(4);
      delay(2000);
    }
    else{
      detect2 = false;
      }
      } 
}

See reply #2

i seemed to figure it out, thanks guys. Also i am using a dfplayer and I have an rgb led but for some reason it wont let me change through the colors but the sound plays. Im thinking it might have something to do with the library.
I have tried this way.

#include <DFPlayer_Mini_Mp3.h>
#include <SoftwareSerial.h>

 
void setup () {
 Serial.begin (9600);
 mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
 mp3_set_volume (50);
}
 
void loop () {       
 
 mp3_play (1); //play 0001.mp3
 delay (3000); //10 sec, time delay to allow 0001.mp3 to finish playing
 
 mp3_play (2);
 delay (5000);
}

and this way

#include <DFPlayer_Mini_Mp3.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 7); // RX, TX

 
void setup () {
 Serial.begin (9600);
Serial.begin(9600);
mySerial.begin (9600);

mp3_set_serial (mySerial); //set Serial for DFPlayer-mini mp3 module
 mp3_set_volume (60);
 
void loop () {       
 
 mp3_play (1); //play 0001.mp3
 delay (3000); //10 sec, time delay to allow 0001.mp3 to finish playing
 
 mp3_play (2);
 delay (5000);
}

Of course the colors don’t change. You don’t have any code in the loop() that even mentions LEDs, much less controls them.

I have the code for the rgb led that lets me change colors on button press but when I integrate the dfplayer code above into that code it only stays one solid color and does not change.

You have way too many pieces of code running around for anyone to make sense of what you’re asking. Please post the one specific piece of code that you are using and having trouble with (ie your best attempt at combining the two sketches you mentioned).

If you have tried to integrate an MP3s playing and LED colours changing then that integrated code is what we need to see.

Looking at 2 different ways to play MP3s without trying to do anything with LEDs doesn't help.

Steve