Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2. DynamicJsonDocument doc(1024);
  3.  
  4. #include <limits.h>
  5.  
  6. /* ============================================
  7.  *  MPU6050
  8.  * ===============================================
  9. */
  10.  
  11. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  12. // is used in I2Cdev.h
  13. #include "Wire.h"
  14. #include "SPI.h"
  15.  
  16. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  17. // for both classes must be in the include path of your project
  18. #include "I2Cdev.h"
  19. #include "MPU6050.h"
  20.  
  21. // class default I2C address is 0x68
  22. // specific I2C addresses may be passed as a parameter here
  23. // AD0 low = 0x68 (default for InvenSense evaluation board)
  24. // AD0 high = 0x69
  25. MPU6050 accelgyro;
  26.  
  27. int16_t ax=SHRT_MAX, ay=SHRT_MAX, az=SHRT_MAX;
  28. int16_t gx=SHRT_MAX, gy=SHRT_MAX, gz=SHRT_MAX;
  29. int8_t threshold, count;
  30. float temp;
  31. bool zero_detect;
  32. bool TurnOnZI = false;
  33.  
  34. bool XnegMD, XposMD, YnegMD, YposMD, ZnegMD, ZposMD;
  35.  
  36. bool blinkState = false;
  37. /*  Getting_BPM_to_Monitor prints the BPM to the Serial Monitor, using the least lines of code and PulseSensor Library.
  38.     Tutorial Webpage: https://pulsesensor.com/pages/getting-advanced
  39.   --------Use This Sketch To------------------------------------------
  40.   1) Displays user's live and changing BPM, Beats Per Minute, in Arduino's native Serial Monitor.
  41.   2) Print: "♥  A HeartBeat Happened !" when a beat is detected, live.
  42.   2) Learn about using a PulseSensor Library "Object".
  43.   --------------------------------------------------------------------*/
  44.  
  45. #define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
  46. #include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.  
  47.  
  48. //  Variables
  49. const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
  50. //const int LED13 = 13;          // The on-board Arduino LED, close to PIN 13.
  51. int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
  52. // Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
  53. // Otherwise leave the default "550" value.
  54. int myBPM ;
  55.  
  56. PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
  57.  
  58. void pulseSensorSetup(){
  59.     // Configure the PulseSensor object, by assigning our variables to it.
  60.     pulseSensor.analogInput(PulseWire);
  61.     //pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  62.     pulseSensor.setThreshold(Threshold);
  63.  
  64.     // Double-check the "pulseSensor" object was created and "began" seeing a signal.
  65.     if (pulseSensor.begin()) {
  66.       Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.
  67.     }
  68.   }
  69.  
  70. void mpuSetup(){
  71.     // join I2C bus (I2Cdev library doesn't do this automatically)
  72.   Wire.begin();
  73.  
  74.   // initialize serial communication
  75.   // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
  76.   // it's really up to you depending on your project)
  77.   //Serial.begin(38400);
  78.  
  79.   // initialize device
  80.   Serial.println("Initializing I2C devices...");
  81.   accelgyro.initialize();
  82.  
  83.   // verify connection
  84.   Serial.println("Testing device connections...");
  85.   Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  86.  
  87.   Serial.println("Setting motion detection threshold to 2...");
  88.   accelgyro.setMotionDetectionThreshold(2);
  89.  
  90.   Serial.println("Setting motion detection duration to 40...");
  91.   accelgyro.setMotionDetectionDuration(40);
  92.   }  
  93. void setup() {
  94.   Serial.begin(9600);          // initialize serial communication
  95.   // put your setup code here, to run once:
  96.   pulseSensorSetup();
  97. }
  98. void pulseSensorLoop(){
  99.     myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
  100.     // "myBPM" hold this BPM value now.
  101.  
  102.     if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened".
  103.       //Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
  104.       //Serial.print("BPM: ");                        // Print phrase "BPM: "
  105.       //Serial.println(myBPM);                        // Print the value inside of myBPM.
  106.     }
  107.     //delay(20);                    // considered best practice in a simple sketch.
  108.   }
  109.  
  110. void mpuLoop(){
  111.     // read raw accel/gyro measurements from device
  112.    
  113.     //Serial.println("Getting raw accwl/gyro measurements");
  114.     //accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  115.     //Serial.print(ax / 16384.); Serial.print(",");
  116.     //Serial.print(ay / 16384.); Serial.print(",");
  117.     //Serial.print(az / 16384.); Serial.print(",");
  118.     //Serial.print(gx / 131.072); Serial.print(",");
  119.     //Serial.print(gy / 131.072); Serial.print(",");
  120.     //Serial.print(gz / 131.072); Serial.print(",");
  121.    
  122.   }  
  123. void lm35(){};
  124. void sendJSON(){};
  125.  
  126. void loop() {
  127.   // put your main code here, to run repeatedly:
  128.   //pulseSensorLoop();
  129.  
  130.   //accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  131.  
  132.   doc["temp"] = 3.4028235E+38;
  133.   doc["heartBeat"]   = SHRT_MAX ;
  134.  
  135.   JsonArray mpu6050 = doc.createNestedArray("mpu6050");
  136.   mpu6050.add(ax / 16384.); // ax
  137.   mpu6050.add(ay / 16384.); // ay
  138.   mpu6050.add(az / 16384.); // az
  139.   mpu6050.add(gx / 131.072); // gx
  140.   mpu6050.add(gy / 131.072); // gy
  141.   mpu6050.add(gz / 131.072); // gz
  142.  
  143.   serializeJson(doc, Serial);
  144.   Serial.println(measureJson(doc));
  145.   // This prints:
  146.   // {"temp":"gps","heartbeat":1351824120,"mpu6050":[48.756080,2.302038]}
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement