Thank you James I really appreciate your support, I got it working, my code now is this and serial monitor is showing the tilt & the “beep”
CODE:
#include <Qduino.h>
#include <Wire.h>
#include <MPU9250.h>
// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
int status;
float x0 = 0 ;
float y0 = 0 ;
float z0 = 0 ;
int x_start = 0;
int x_now = 0;
void setup() {
// serial to display data
Serial.begin(115200);
while(!Serial) {}
// start communication with IMU
status = IMU.begin();
if (status < 0) {
Serial.println(“IMU initialization unsuccessful”);
Serial.println(“Check IMU wiring or try cycling power”);
Serial.print("Status: ");
Serial.println(status);
while(1) {}
}
x0 = IMU.getAccelX_mss();
y0 = IMU.getAccelY_mss();
z0 = IMU.getAccelZ_mss();
}
void loop() {
// read the sensor
IMU.readSensor();
float x = IMU.getAccelX_mss();
float y = IMU.getAccelY_mss();
float z = IMU.getAccelZ_mss();
// Print raw values
Serial.print(“x = “);
Serial.print(x);
Serial.print(” y = “);
Serial.print(y);
Serial.print(”, z = “);
Serial.print(z);
Serial.println(” m/s^2”);
// Calculate the angles for the 3 axis
float X = atan(x/(sqrt(yy + zz)));
float Y = atan(y/(sqrt(xx + zz)));
float Z = atan((sqrt(xx + yy))/z);
// Convert acceleration to angular movement in radians
x = atan(x/(sqrt(yy + zz)));
// Convert radians to degrees and ignore the decimal point
x_now = (int)(x * 180/M_PI);
// Calculate tilt angle with respect to original at rest position
// and that Vertical = 90 degrees
int x_tilt = x_now - x_start + 90;
// Beep
if ((x_tilt < 160) || (x_tilt > 130)) {
// beep function to make noise
Serial.println("===================");
Serial.println(" BEEP !!! “);
Serial.println(”===================");
}
Serial.print("x_tilt = ");
Serial.println(x_tilt);
delay(2000);
}
S.MONITOR:
x = 6.13 y = -4.27, z = 6.34 m/s^2
BEEP !!!!
===================
x_tilt = 128
However, instead of defining the start value as ‘90’ degrees, like the previous code, I want the starting value to be at 0, so when the user wears the device in an upright sitting posture and turn on the device, the angle measurement can be starting from that point. (so wherever they place the device and doesn’t matter how the device is placed when the device is turned on, that has to be the starting point). Do I need to put this: float t = acos((ax0 ax + ay0 ay + az0 az)/sqrt((ax ax + ay ay + az az) (ax0 ax0 + ay0 ay0 + az0 az0))); code again? or is there another way to do it?
Moreover, I’m thinking of adding in the side inclination as well (30degrees on each side; y-axis), to do this should should the code be like:
#include <Qduino.h>
#include <Wire.h>
#include <MPU9250.h>
// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
int status;
float x0 = 0 ;
float y0 = 0 ;
float z0 = 0 ;
int x_start = 0;
int x_now = 0;
int y_start = 0;
int y_now = 0;
void setup() {
// serial to display data
Serial.begin(115200);
while(!Serial) {}
// start communication with IMU
status = IMU.begin();
if (status < 0) {
Serial.println(“IMU initialization unsuccessful”);
Serial.println(“Check IMU wiring or try cycling power”);
Serial.print("Status: ");
Serial.println(status);
while(1) {}
}
x0 = IMU.getAccelX_mss();
y0 = IMU.getAccelY_mss();
z0 = IMU.getAccelZ_mss();
}
void loop() {
// read the sensor
IMU.readSensor();
float x = IMU.getAccelX_mss();
float y = IMU.getAccelY_mss();
float z = IMU.getAccelZ_mss();
// Print raw values
Serial.print(“x = “);
Serial.print(x);
Serial.print(” y = “);
Serial.print(y);
Serial.print(”, z = “);
Serial.print(z);
Serial.println(” m/s^2”);
// Calculate the angles for the 3 axis
float X = atan(x/(sqrt(yy + zz)));
float Y = atan(y/(sqrt(xx + zz)));
float Z = atan((sqrt(xx + yy))/z);
// Convert acceleration to angular movement in radians
x = atan(x/(sqrt(yy + zz)));
y = atan(y/(sqrt(xx + zz)));
// Convert radians to degrees and ignore the decimal point
x_now = (int)(x * 180/M_PI);
y_now = (int)(y * 180/M_PI);
// Calculate tilt angle with respect to original at rest position
// and that Vertical = 90 degrees
int x_tilt = x_now - x_start + 90;
int y_tilt = y_now - y_start + 180;
// Beep
if ((x_tilt < 70) || (x_tilt > 130)) {
// beep function to make noise
Serial.println("===================");
Serial.println(" xBEEP !!! “);
Serial.println(”===================");
if ((y_tilt < 150) || (y_tilt > 210)) {
// beep function to make noise
Serial.println("===================");
Serial.println(" yBEEP !!! “);
Serial.println(”===================");
}
Serial.print("x_tilt = ");
Serial.println(x_tilt);
Serial.print("y_tilt = ");
Serial.println(y_tilt);
delay(2000);
}
}
like this?
(Sorry, my qduino is not showing up on the port again, it got turned off when i was just about to run this code above, so i couldn’t check. I need to work on the problem again. the light are on everything seems fine so I don’t know what went wrong just then. )
I thank you for your consideration in advance. Thank you so much!