summaryrefslogtreecommitdiff
path: root/libraries/OBD2UART/examples/orientation_demo
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@live.com>2018-04-11 11:38:05 +1000
committerStanley Huang <stanleyhuangyc@live.com>2018-04-11 11:38:05 +1000
commit47b642a6e06e05d17b70c2369af5acbc2ae9daf2 (patch)
treeb39c3f994a28862463c0ac04f0fddb81cc6f18ba /libraries/OBD2UART/examples/orientation_demo
parent26196c5808a0c1dd2fba7e357d9a7ba8aa7037d7 (diff)
parentda6126b1c37ed3aa8e095a1dcc8509d4537c4819 (diff)
download2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.tar.gz
2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.tar.bz2
2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.zip
Merge branch 'master' of https://github.com/stanleyhuangyc/ArduinoOBD
Diffstat (limited to 'libraries/OBD2UART/examples/orientation_demo')
-rw-r--r--libraries/OBD2UART/examples/orientation_demo/orientation_demo.ino97
1 files changed, 97 insertions, 0 deletions
diff --git a/libraries/OBD2UART/examples/orientation_demo/orientation_demo.ino b/libraries/OBD2UART/examples/orientation_demo/orientation_demo.ino
new file mode 100644
index 0000000..e04949c
--- /dev/null
+++ b/libraries/OBD2UART/examples/orientation_demo/orientation_demo.ino
@@ -0,0 +1,97 @@
+/*************************************************************************
+* Testing sketch for Freematics OBD-II UART Adapter V2.1
+* Reads and prints motion sensor data and computed orientation data
+* Distributed under BSD
+* Visit https://freematics.com/products for more product information
+* Written by Stanley Huang <stanley@freematics.com.au>
+*************************************************************************/
+
+#include <OBD2UART.h>
+
+// On Arduino Leonardo, Micro, MEGA or DUE, hardware serial can be used for output as the adapter occupies Serial1
+// On Arduino UNO and those have no Serial1, we use software serial for output as the adapter uses Serial
+#ifdef ARDUINO_AVR_UNO
+#include <SoftwareSerial.h>
+SoftwareSerial mySerial(A2, A3);
+#else
+#define mySerial Serial
+#endif
+
+#if defined(ESP32) && !defined(Serial1)
+HardwareSerial Serial1(1);
+#endif
+
+COBD obd;
+
+void setup()
+{
+ mySerial.begin(115200);
+ while (!mySerial);
+
+ // this will begin serial
+
+ for (;;) {
+ delay(1000);
+ byte version = obd.begin();
+ mySerial.print("Freematics OBD-II Adapter ");
+ if (version > 0) {
+ mySerial.println("detected");
+ break;
+ } else {
+ mySerial.println("not detected");
+ }
+ }
+
+ // initialize MEMS with sensor fusion enabled
+ bool hasMEMS = obd.memsInit(true);
+ mySerial.print("Motion sensor is ");
+ mySerial.println(hasMEMS ? "present" : "not present");
+ if (!hasMEMS) {
+ for (;;) delay(1000);
+ }
+}
+
+
+void loop()
+{
+ int16_t acc[3] = {0};
+ int16_t gyro[3] = {0};
+ int16_t mag[3] = {0};
+
+ if (!obd.memsRead(acc, gyro, mag)) return;
+
+ mySerial.print("ACC:");
+ mySerial.print(acc[0]);
+ mySerial.print('/');
+ mySerial.print(acc[1]);
+ mySerial.print('/');
+ mySerial.print(acc[2]);
+
+ mySerial.print(" GYRO:");
+ mySerial.print(gyro[0]);
+ mySerial.print('/');
+ mySerial.print(gyro[1]);
+ mySerial.print('/');
+ mySerial.print(gyro[2]);
+
+ mySerial.print(" MAG:");
+ mySerial.print(mag[0]);
+ mySerial.print('/');
+ mySerial.print(mag[1]);
+ mySerial.print('/');
+ mySerial.print(mag[2]);
+
+ mySerial.println();
+
+ float yaw, pitch, roll;
+ if (obd.memsOrientation(yaw, pitch, roll)) {
+ mySerial.print("Orientation: ");
+ mySerial.print(yaw, 2);
+ mySerial.print(' ');
+ mySerial.print(pitch, 2);
+ mySerial.print(' ');
+ mySerial.println(roll, 2);
+ }
+
+ delay(100);
+}