From c83f19feb55fb3c405431bd12af4132d402cf436 Mon Sep 17 00:00:00 2001
From: Stanley Huang <stanleyhuangyc@gmail.com>
Date: Mon, 19 Dec 2016 17:22:08 +1100
Subject: Updated example sketch

Reading DTC and MEMS data
---
 .../OBD/examples/obd_i2c_test/obd_i2c_test.ino     | 39 ++++++++--
 .../OBD/examples/obd_uart_test/obd_uart_test.ino   | 88 +++++++++++++++++++---
 .../examples/obd_uart_test/obd_uart_test.ino       | 87 ++++++++++++++++++---
 3 files changed, 185 insertions(+), 29 deletions(-)

diff --git a/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino b/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino
index c5b89fe..fd9e980 100644
--- a/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino
+++ b/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino
@@ -96,28 +96,55 @@ void readPIDs()
      Serial.println();
 }
 
+void readBatteryVoltage()
+{
+  Serial.print('[');
+  Serial.print(millis());
+  Serial.print(']');
+  Serial.print("Battery:");
+  Serial.print(obd.getVoltage(), 1);
+  Serial.println('V');
+}
+
 void setup() {
-  delay(500);
   Serial.begin(115200);
-  obd.begin();
+  delay(500);
   accelgyro.initialize();
-  readMEMS();
+  obd.begin();
 
+  // send some commands for testing and show response for debugging purpose
+  //testOut();
+ 
+  // initialize OBD-II adapter
   do {
-    testOut();
     Serial.println("Init...");
-  } while (!obd.init());  
+  } while (!obd.init());
 
   char buf[64];
   if (obd.getVIN(buf, sizeof(buf))) {
       Serial.print("VIN:");
       Serial.println(buf);
   }
-  delay(1000);
+  
+  unsigned int codes[6];
+  byte dtcCount = obd.readDTC(codes, 6);
+  if (dtcCount == 0) {
+    Serial.println("No DTC"); 
+  } else {
+    Serial.print(dtcCount); 
+    Serial.print(" DTC:");
+    for (byte n = 0; n < dtcCount; n++) {
+      Serial.print(' ');
+      Serial.print(codes[n], HEX);
+    }
+    Serial.println();
+  }
+  delay(3000);
 }
 
 void loop() {
   readPIDs();
+  readBatteryVoltage();
   readMEMS();
 }
  
diff --git a/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino
index 08106ec..477ac61 100644
--- a/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino
+++ b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino
@@ -6,13 +6,14 @@
 * Written by Stanley Huang <stanleyhuangyc@gmail.com>
 *************************************************************************/
 
-#include <Arduino.h>
 #include <Wire.h>
 #include <SoftwareSerial.h>
 #include <OBD.h>
 
 // On Arduino Leonardo, Micro, MEGA or DUE, hardware serial can be used for output
-// as OBD-II UART adapter connects to Serial1, otherwise we use software serial
+// as OBD-II UART adapter uses to Serial1
+// On Arduino UNO and those have no Serial1, we use software serial for output
+// as OBD-II UART adapter uses to Serial
 SoftwareSerial mySerial(A2, A3);
 //#define mySerial Serial
 
@@ -66,42 +67,105 @@ void readPIDMultiple()
     static const byte pids[] = {PID_SPEED, PID_ENGINE_LOAD, PID_THROTTLE, PID_COOLANT_TEMP, PID_INTAKE_TEMP};
     int values[sizeof(pids)];
     if (obd.readPID(pids, sizeof(pids), values) == sizeof(pids)) {
+      mySerial.print('[');
+      mySerial.print(millis());
+      mySerial.print(']');
       for (byte i = 0; i < sizeof(pids) ; i++) {
-        mySerial.print('[');
-        mySerial.print(millis());
-        mySerial.print(']');
         mySerial.print((int)pids[i] | 0x100, HEX);
         mySerial.print('=');
-        mySerial.println(values[i]);
+        mySerial.print(values[i]);
+        mySerial.print(' ');
        }
+       mySerial.println();
     }
 }
 
+void readBatteryVoltage()
+{
+  mySerial.print('[');
+  mySerial.print(millis());
+  mySerial.print(']');
+  mySerial.print("Battery:");
+  mySerial.print(obd.getVoltage(), 1);
+  mySerial.println('V');
+}
+
+void readMEMS()
+{
+  int x, y, z;
+  mySerial.print('[');
+  mySerial.print(millis());
+  mySerial.print(']');
+  if (obd.readAccel(x, y, z)) {
+    mySerial.print("ACC:");
+    mySerial.print(x);
+    mySerial.print('/');
+    mySerial.print(y);
+    mySerial.print('/');
+    mySerial.print(z);
+    mySerial.print(' ');
+  }
+  if (obd.readGyro(x, y, z)) {
+    mySerial.print("GYRO:");
+    mySerial.print(x);
+    mySerial.print('/');
+    mySerial.print(y);
+    mySerial.print('/');
+    mySerial.print(z);
+    mySerial.print(' ');
+  }
+  mySerial.println();
+}
+
 void setup()
 {
-  delay(500);
   mySerial.begin(115200);
+  while (!mySerial);
+  
   // this will begin serial
   obd.begin();
 
-  // send some commands for testing and show response
-  testOut();
-  
+  mySerial.print("Adapter version: ");
+  mySerial.println(obd.version);
+  delay(1000);
+
+  // send some commands for testing and show response for debugging purpose
+  //testOut();
+ 
   // initialize OBD-II adapter
   do {
     mySerial.println("Init...");
-  } while (!obd.init());  
+  } while (!obd.init());
 
   char buf[64];
   if (obd.getVIN(buf, sizeof(buf))) {
       mySerial.print("VIN:");
       mySerial.println(buf);
   }
-  delay(1000);
+  
+  unsigned int codes[6];
+  byte dtcCount = obd.readDTC(codes, 6);
+  if (dtcCount == 0) {
+    mySerial.println("No DTC"); 
+  } else {
+    mySerial.print(dtcCount); 
+    mySerial.print(" DTC:");
+    for (byte n = 0; n < dtcCount; n++) {
+      mySerial.print(' ');
+      mySerial.print(codes[n], HEX);
+    }
+    mySerial.println();
+  }
+  delay(3000);
 }
 
+
 void loop()
 {
   readPIDSingle();
   readPIDMultiple();
+  readBatteryVoltage();
+  if (obd.version > 10) {
+    readMEMS();
+  }
 }
diff --git a/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino b/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino
index bd1c618..2d35485 100644
--- a/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino
+++ b/libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino
@@ -10,7 +10,9 @@
 #include <OBD2UART.h>
 
 // On Arduino Leonardo, Micro, MEGA or DUE, hardware serial can be used for output
-// as OBD-II UART adapter connects to Serial1, otherwise we use software serial
+// as OBD-II UART adapter uses to Serial1
+// On Arduino UNO and those have no Serial1, we use software serial for output
+// as OBD-II UART adapter uses to Serial
 SoftwareSerial mySerial(A2, A3);
 //#define mySerial Serial
 
@@ -64,42 +66,105 @@ void readPIDMultiple()
     static const byte pids[] = {PID_SPEED, PID_ENGINE_LOAD, PID_THROTTLE, PID_COOLANT_TEMP, PID_INTAKE_TEMP};
     int values[sizeof(pids)];
     if (obd.readPID(pids, sizeof(pids), values) == sizeof(pids)) {
+      mySerial.print('[');
+      mySerial.print(millis());
+      mySerial.print(']');
       for (byte i = 0; i < sizeof(pids) ; i++) {
-        mySerial.print('[');
-        mySerial.print(millis());
-        mySerial.print(']');
         mySerial.print((int)pids[i] | 0x100, HEX);
         mySerial.print('=');
-        mySerial.println(values[i]);
+        mySerial.print(values[i]);
+        mySerial.print(' ');
        }
+       mySerial.println();
     }
 }
 
+void readBatteryVoltage()
+{
+  mySerial.print('[');
+  mySerial.print(millis());
+  mySerial.print(']');
+  mySerial.print("Battery:");
+  mySerial.print(obd.getVoltage(), 1);
+  mySerial.println('V');
+}
+
+void readMEMS()
+{
+  int x, y, z;
+  mySerial.print('[');
+  mySerial.print(millis());
+  mySerial.print(']');
+  if (obd.readAccel(x, y, z)) {
+    mySerial.print("ACC:");
+    mySerial.print(x);
+    mySerial.print('/');
+    mySerial.print(y);
+    mySerial.print('/');
+    mySerial.print(z);
+    mySerial.print(' ');
+  }
+  if (obd.readGyro(x, y, z)) {
+    mySerial.print("GYRO:");
+    mySerial.print(x);
+    mySerial.print('/');
+    mySerial.print(y);
+    mySerial.print('/');
+    mySerial.print(z);
+    mySerial.print(' ');
+  }
+  mySerial.println();
+}
+
 void setup()
 {
-  delay(500);
   mySerial.begin(115200);
+  while (!mySerial);
+  
   // this will begin serial
   obd.begin();
 
-  // send some commands for testing and show response
-  testOut();
-  
+  mySerial.print("Adapter version: ");
+  mySerial.println(obd.version);
+  delay(1000);
+
+  // send some commands for testing and show response for debugging purpose
+  //testOut();
+ 
   // initialize OBD-II adapter
   do {
     mySerial.println("Init...");
-  } while (!obd.init());  
+  } while (!obd.init());
 
   char buf[64];
   if (obd.getVIN(buf, sizeof(buf))) {
       mySerial.print("VIN:");
       mySerial.println(buf);
   }
-  delay(1000);
+  
+  unsigned int codes[6];
+  byte dtcCount = obd.readDTC(codes, 6);
+  if (dtcCount == 0) {
+    mySerial.println("No DTC"); 
+  } else {
+    mySerial.print(dtcCount); 
+    mySerial.print(" DTC:");
+    for (byte n = 0; n < dtcCount; n++) {
+      mySerial.print(' ');
+      mySerial.print(codes[n], HEX);
+    }
+    mySerial.println();
+  }
+  delay(3000);
 }
 
+
 void loop()
 {
   readPIDSingle();
   readPIDMultiple();
+  readBatteryVoltage();
+  if (obd.version > 10) {
+    readMEMS();
+  }
 }
-- 
cgit v1.2.3