Browse Source

Upload files to 'MPU9250_json'

master
cs171027 5 years ago
parent
commit
d11b94bafd
  1. 52
      MPU9250_json/MPU9250_json.ino

52
MPU9250_json/MPU9250_json.ino

@ -0,0 +1,52 @@
#include <ArduinoJson.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
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);
}
Loading…
Cancel
Save