I want to activate buzzer with accelerometer and gyroscope

I am currently trying to learn about arduino. I use accelerometer and gyroscope MPU6050 - GY521. As we know that sensor has 3 axis each, accel_x, accel_y, accel_z, gyro_x, gyro_y, and gyro_z. Just say that, how can I activate my buzzer if the condition is ‘after such as gyro_x reach a certain number after certain times’. Like an example, I want to activate the buzzer when gyro_x > 10000 in it’s 100th. So does anyone can help ? thank you so much

This library has complete Example sketches that may be ready or could easily be adapted to do what you are wanting.

https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/MPU6050

Post some of your code so we can see how to help you. Z

thanks zhomeslice for replying,

here is the code

 // (c) Michael Schoeffler 2017, http://www.mschoeffler.de

#include "Wire.h" // This library allows you to communicate with I2C devices.

const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.

int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data

char tmp_str[7]; // temporary variable used in convert function

char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
  sprintf(tmp_str, "%6d", i);
  return tmp_str;
}

float temp;
int tempPin = 0;
int buzzer=8; // buzz positive to digital pin 8



void setup() {
  pinMode(buzzer,OUTPUT); // set pin mode as output
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
  Wire.write(0x6B); // PWR_MGMT_1 register
  Wire.write(0); // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
   beep(500);


  
}
void loop() {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
  Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
  Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers
  
  // "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
  accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

 
  
//  mpu.setXGyroOffset(220);
//  mpu.setYGyroOffset(76);
//  mpu.setZGyroOffset(-85);

  // print out data
  Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
  Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
  Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));
  // the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
  Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53);
  Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
  Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
  Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));
  Serial.println();

  //digitalWrite(buzzer,LOW);


 if (gyro_y > 30000) { beep(50); beep(50); }
 
  temp = analogRead(tempPin);
 temp = temp * 0.48828125;

 Serial.print("Temp lm 35 = ");
 Serial.print(temp);
 Serial.print("*C");
 Serial.println();
 delay(1000);  
}
 
void beep(unsigned char delayms){
  digitalWrite(buzzer,HIGH);
  delay(delayms); // wait for a delayms ms
  digitalWrite(buzzer,LOW);
  delay(delayms); // wait for a delayms ms
}

I had read the libraries but I still don’t get it. Like u see at my code, I can activate my buzzer if the condition is when ‘gyro_y > 30000’. But I don’t know how to activate if the condition is like I said before. If anyone knows, please help, thank you.

Like an example, I want to activate the buzzer when gyro_x > 10000 in it’s 100th

Sorry. That makes no sense. What range of values are possible for gyro_x? Since a gyroscope measure angles, it doesn’t seem likely that the angle will ever exceed 10000 radians. What “in it’s 100th” means is mystery.

char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
  sprintf(tmp_str, "%6d", i);
  return tmp_str;
}

Why does this function return a global variable?

  // "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
  accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

Doesn’t matter how much data is available to read. We are going to read all 14 bytes, damn it.

Not a good idea!

void beep(unsigned char delayms){

So you can “beep” for ‘Y’ milliseconds? byte or short or int or long make sense. char does not.

PaulS:
Sorry. That makes no sense. What range of values are possible for gyro_x? Since a gyroscope measure angles, it doesn’t seem likely that the angle will ever exceed 10000 radians. What “in it’s 100th” means is mystery.

umm I don’t mean to get that 10000 radians,below here’s an example of result, u can see I could reach the amount more than 10000. Fyi I am still working on it on breadboardd and just do some movement. and what i mean 100th is when that data read such as 10000 or any, after 100 times or at 100th. Anyway ignore the temp lm 35, I have not connected it. *edited

 ⸮aX =   2624 | aY =    -92 | aZ =   6204 | tmp = 31.49 | gX =    787 | gY =  -1709 | gZ =     55
Temp lm 35 = 499.51*C
aX =   1644 | aY =    516 | aZ =   5844 | tmp = 31.64 | gX =  -3029 | gY =  -4894 | gZ =  -6081
Temp lm 35 = 454.59*C
aX =   2992 | aY =    -80 | aZ =   6332 | tmp = 31.45 | gX =    765 | gY =   3128 | gZ =    644
Temp lm 35 = 456.54*C
aX =   1508 | aY =  -7892 | aZ =   3736 | tmp = 31.54 | gX =  32767 | gY =  -4488 | gZ =  -3720
Temp lm 35 = 463.38*C
aX =   1152 | aY =  -1660 | aZ =   6528 | tmp = 31.45 | gX = -32768 | gY =  -4579 | gZ =  -3852
Temp lm 35 = 492.19*C
aX =   1444 | aY =   4428 | aZ =   4388 | tmp = 31.54 | gX =  32503 | gY =  -1652 | gZ =  -2135
Temp lm 35 = 499.51*C
aX =   2432 | aY =  -5756 | aZ =   4684 | tmp = 31.49 | gX = -15707 | gY =  -2023 | gZ =  -1861
Temp lm 35 = 499.51*C
aX =   1872 | aY =   -148 | aZ =   6104 | tmp = 31.59 | gX =   -253 | gY =   -494 | gZ =    199
Temp lm 35 = 499.51*C
aX =   2348 | aY =   -208 | aZ =   6384 | tmp = 31.49 | gX =  -1026 | gY =  -7874 | gZ =   1029
Temp lm 35 = 498.05*C
aX =   9048 | aY =   -624 | aZ =  -2332 | tmp = 31.59 | gX =    823 | gY =  32767 | gZ =   1197
Temp lm 35 = 454.10*C
aX =   4524 | aY =    472 | aZ =  -9436 | tmp = 31.54 | gX =  -4009 | gY = -21394 | gZ =  -2008
Temp lm 35 = 458.01*C
aX =   2324 | aY =  -1264 | aZ =   6536 | tmp = 31.54 | gX =    687 | gY =   -639 | gZ =  -1583
Temp lm 35 = 458.01*C
aX =    232 | aY =   -496 | aZ =   4728 | tmp = 31.64 | gX =  14690 | gY =  -3874 | gZ =  30319
Temp lm 35 = 465.33*C
aX =   7964 | aY =    -48 | aZ =   -200 | tmp = 31.49 | gX =   -811 | gY =  32767 | gZ =  -1572
Temp lm 35 = 499.51*C
aX =   3688 | aY =  -2908 | aZ =   6032 | tmp = 31.54 | gX =  -3871 | gY =  13421 | gZ = -30973
Temp lm 35 = 489.26*C
aX =    868 | aY =   -140 | aZ =   6588 | tmp = 31.64 | gX =     12 | gY =   -221 | gZ =    194
Temp lm 35 = 499.51*C
aX =    768 | aY =   -152 | aZ =   6480 | tmp = 31.54 | gX =     27 | gY =   -260 | gZ =    194
Temp lm 35 = 499.51*C
aX =    820 | aY =   -124 | aZ =   6604 | tmp = 31.59 | gX =     50 | gY =   -245 | gZ =    178
Temp lm 35 = 499.51*C

PaulS: void beep(unsigned char delayms){

So you can "beep" for 'Y' milliseconds? byte or short or int or long make sense. char does not.

so I need to change char to int or long ? sorry I am new and just start learning arduino in last sunday

and what i mean 100th is when that data read such as 10000 or any, after 100 times or at 100th.

You will have problems translating your requirements into code unless you can learn to express them better.

After what happens 100 times?

If you mean

readingNumber++;
if(gyro_x > 10000 || readingNumber >= 100)

then say that.

PaulS: You will have problems translating your requirements into code unless you can learn to express them better.

After what happens 100 times?

If you mean

readingNumber++;
if(gyro_x > 10000 || readingNumber >= 100)

then say that.

I think you get it wrong, or is it me ? okay I'll try to explain what I mean , for example, I am working on this project for reading human's step of walking. after i put the sensor and board on a kind of position on my leg, and after I move my leg, it will results data right ? just say that it is accel_x and I take 4 steps. Ignoring error, we could see 4 datas with certain amount that should be similar, as a following example such as 1010,1020,1200,1100 and just say that the result when it is idle, is about 0. Naaah, if i set the program 'if (accel_x > 1000) blabla' the buzzer will works when it says 1010 right ? and this is what I mean, how if I want the buzzer will be activated when it says 1100, which is the 4th step ?

after i put the sensor and board on a kind of position on my leg, and after I move my leg, it will results data right ?

Yes. Every time you read the sensor you will get the current acceleration and angle values (in some units).

just say that it is accel_x and I take 4 steps. Ignoring error, we could see 4 datas with certain amount that should be similar

Wrong. You need to read the sensor continuously. 4 steps will NOT result in 4 readings. The Arduino doesn't know if you are walking, riding a bike, or lying in a hammock. That is what you want it to determine, and, if it determines that you are walking, how many steps you have taken.

The values read for each of the 3 axes will, if you plot them, follow some kind of curve as you take each step. You need to understand what that curve looks like, and where the peaks and troughs are.

Then, if you can relate the peaks and troughs to the movement of your leg, you can determine that you have taken a step because you have detected a peak or trough.

This is nowhere near as trivial as you are trying to make it.

PaulS: The values read for each of the 3 axes will, if you plot them, follow some kind of curve as you take each step. You need to understand what that curve looks like, and where the peaks and troughs are.

Then, if you can relate the peaks and troughs to the movement of your leg, you can determine that you have taken a step because you have detected a peak or trough.

This is nowhere near as trivial as you are trying to make it.

woahh i see, i get it now. really thanks to u PaulS, so I've been misunderstand how this sensor works, I thought that each of axes work on their own value then I should only get an axes with it's peak, not the plot of other's too. Now I could imagine what should I do next. Thanks once again ^_^