From d11b94bafd9f07a529f937e3b3dd3222a1e76aea Mon Sep 17 00:00:00 2001 From: cs171027 Date: Wed, 22 Jan 2020 04:36:01 +0000 Subject: [PATCH] Upload files to 'MPU9250_json' --- MPU9250_json/MPU9250_json.ino | 52 +++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 MPU9250_json/MPU9250_json.ino diff --git a/MPU9250_json/MPU9250_json.ino b/MPU9250_json/MPU9250_json.ino new file mode 100644 index 0000000..f8563ad --- /dev/null +++ b/MPU9250_json/MPU9250_json.ino @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +Adafruit_MPU6050 mpu; + +void setup(void) { + Serial.begin(115200); + while (!Serial) + delay(10); // pause Zero, Leonardo, etc until serial console opens + mpu.setAccelerometerRange(MPU6050_RANGE_8_G); + mpu.setGyroRange(MPU6050_RANGE_500_DEG); + mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); + delay(100); +} + +void loop() { + + /* Take a new reading */ + mpu.read(); + + /* Get new sensor events with the readings */ + sensors_event_t accel_event, gyro_event, temp; + mpu.getEvent(&accel_event, &gyro_event, &temp); + + /*Create new json object from values x,y,z respectively*/ + + DynamicJsonBuffer jsonBuffer; /*or StaticJsonBuffer,200> jsonBuffer;*/ + JsonObject& root=jsonBuffer.createObject(); + + + /*Json object now gets input values from acceleration vectors*/ + + JsonObject& accel_data=root.createNestedObject("accel_data"); + accel_data["x"]=accel_event.acceleration.x; + accel_data["y"]=accel_event.acceleration.y; + accel_data["z"]=accel_event.acceleration.z; + + + /*Json object now gets input values from gyroscope vectors*/ + + JsonObject& gyro_data=root.createNestedObject("gyro_data"); + gyro_data["x"]=gyro_event.acceleration.x; + gyro_data["y"]=gyro_event.acceleration.y; + gyro_data["z"]=gyro_event.acceleration.z; + + /* Print out the values into JSON format*/ + root.printTo(Serial); + Serial.println(""); + delay(1000); +}