From b6c5e2146584b98629ca3bffa74882af185b78da Mon Sep 17 00:00:00 2001
From: Stanley Huang <stanleyhuangyc@gmail.com>
Date: Sat, 5 Dec 2015 15:05:22 +1100
Subject: Update example sketches for OBD library

---
 .../OBD/examples/obd_i2c_test/obd_i2c_test.ino     | 34 +++++++++-----------
 .../OBD/examples/obd_uart_test/obd_uart_test.ino   | 36 +++++++++++-----------
 2 files changed, 33 insertions(+), 37 deletions(-)

(limited to 'libraries')

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 19db06f..9911897 100644
--- a/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino
+++ b/libraries/OBD/examples/obd_i2c_test/obd_i2c_test.ino
@@ -18,12 +18,11 @@ MPU6050 accelgyro;
 
 void testOut()
 {
-    static const char PROGMEM cmds[][6] = {"ATZ\r", "ATL1\r", "ATH0\r", "ATRV\r", "0100\r", "010C\r", "0902\r"};
+    static const char cmds[][6] = {"ATZ\r", "ATL1\r", "ATH0\r", "ATRV\r", "0100\r", "010C\r", "0902\r"};
     char buf[128];
 
     for (byte i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
-        char cmd[6];
-        memcpy_P(cmd, cmds[i], sizeof(cmd));
+        const char *cmd = cmds[i];
         Serial.print("Sending ");
         Serial.println(cmd);
         if (obd.sendCommand(cmd, buf, sizeof(buf))) {
@@ -74,22 +73,21 @@ void readMEMS()
 
 void readPID()
 {
-    static const byte PROGMEM pidlist[] = {PID_ENGINE_LOAD, PID_COOLANT_TEMP, PID_RPM, PID_SPEED, PID_TIMING_ADVANCE, PID_INTAKE_TEMP, PID_THROTTLE, PID_FUEL_LEVEL};
+    static const byte pidlist[] = {PID_ENGINE_LOAD, PID_COOLANT_TEMP, PID_RPM, PID_SPEED, PID_TIMING_ADVANCE, PID_INTAKE_TEMP, PID_THROTTLE, PID_FUEL_LEVEL};
     for (byte i = 0; i < sizeof(pidlist) / sizeof(pidlist[0]); i++) {
-        byte pid = pgm_read_byte(pidlist + i);
+        byte pid = pidlist[i];
         bool valid = obd.isValidPID(pid);
-        Serial.print('0');
         Serial.print((int)pid | 0x100, HEX);
         Serial.print('=');
         if (valid) {
             int value;
             if (obd.read(pid, value)) {
-              byte n = Serial.println(value);
+              Serial.print(value);
             }
-        } else {
-          Serial.println('X'); 
         }
+        Serial.print(' ');
      }
+     Serial.println();
 }
 
 void setup() {
@@ -99,23 +97,21 @@ void setup() {
   accelgyro.initialize();
   readMEMS();
 
-  //testOut();
-
-  Serial.println("Init...");
-  //while (!obd.init());  
+  do {
+    testOut();
+    Serial.println("Init...");
+  } while (!obd.init());  
 
-/*
-  char buf[OBD_RECV_BUF_SIZE];
-  if (obd.getVIN(buf)) {
+  char buf[64];
+  if (obd.getVIN(buf, sizeof(buf))) {
       Serial.print("VIN:");
       Serial.println(buf);
   }
   delay(1000);
-  */
 }
 
 void loop() {
-  //readPID();
+  readPID();
   readMEMS();
-  delay(500);
 }
+ 
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 b746cdd..5b03e95 100644
--- a/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino
+++ b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino
@@ -1,9 +1,9 @@
 /*************************************************************************
-* Sample sketch for Freematics OBD-II UART Adapter
-* Reads and prints several OBD-II PIDs value and MEMS sensor data
+* Testing sketch for Freematics OBD-II UART Adapter
+* Reads and prints several OBD-II PIDs value
 * Distributed under GPL v2.0
 * Visit http://freematics.com for more information
-* (C)2012-2015 Stanley Huang <stanleyhuangyc@gmail.com>
+* Written by Stanley Huang <stanleyhuangyc@gmail.com>
 *************************************************************************/
 
 #include <Arduino.h>
@@ -12,19 +12,21 @@
 #include <OBD.h>
 
 SoftwareSerial mySerial(A2, A3);
+// On Arduino Leonardo, Micro or MEGA, OBD-II adapter should connect to Serial1, so Serial can be used as output
+//#define mySerial Serial
+
 COBD obd;
 
 void testOut()
 {
-    static const char PROGMEM cmds[][6] = {"ATZ\r", "ATL1\r", "ATH0\r", "ATRV\r", "0100\r", "010C\r", "0902\r"};
-    char buf[OBD_RECV_BUF_SIZE];
+    static const char cmds[][6] = {"ATZ\r", "ATL1\r", "ATH0\r", "ATRV\r", "0100\r", "010C\r", "0902\r"};
+    char buf[128];
 
     for (byte i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++) {
-        char cmd[6];
-        memcpy_P(cmd, cmds[i], sizeof(cmd));
+        const char *cmd = cmds[i];
         mySerial.print("Sending ");
         mySerial.println(cmd);
-        if (obd.sendCommand(cmd, buf)) {
+        if (obd.sendCommand(cmd, buf, sizeof(buf))) {
             char *p = strstr(buf, cmd);
             if (p)
                 p += strlen(cmd);
@@ -47,27 +49,26 @@ void testOut()
 
 void readPID()
 {
-    static const byte PROGMEM pidlist[] = {PID_ENGINE_LOAD, PID_COOLANT_TEMP, PID_RPM, PID_SPEED, PID_TIMING_ADVANCE, PID_INTAKE_TEMP, PID_THROTTLE, PID_FUEL_LEVEL};
+    static const byte pidlist[] = {PID_ENGINE_LOAD, PID_COOLANT_TEMP, PID_RPM, PID_SPEED, PID_TIMING_ADVANCE, PID_INTAKE_TEMP, PID_THROTTLE, PID_FUEL_LEVEL};
     for (byte i = 0; i < sizeof(pidlist) / sizeof(pidlist[0]); i++) {
-        byte pid = pgm_read_byte(pidlist + i);
+        byte pid = pidlist[i];
         bool valid = obd.isValidPID(pid);
-        mySerial.print('0');
         mySerial.print((int)pid | 0x100, HEX);
         mySerial.print('=');
         if (valid) {
             int value;
             if (obd.read(pid, value)) {
-              byte n = mySerial.println(value);
+              mySerial.print(value);
             }
-        } else {
-          mySerial.println('X'); 
         }
+        mySerial.print(' ');
      }
+     mySerial.println();
 }
 
 void setup() {
   delay(500);
-  mySerial.begin(9600);
+  mySerial.begin(115200);
   obd.begin();
 
   do {
@@ -75,8 +76,8 @@ void setup() {
     mySerial.println("Init...");
   } while (!obd.init());  
 
-  char buf[OBD_RECV_BUF_SIZE];
-  if (obd.getVIN(buf)) {
+  char buf[64];
+  if (obd.getVIN(buf, sizeof(buf))) {
       mySerial.print("VIN:");
       mySerial.println(buf);
   }
@@ -85,5 +86,4 @@ void setup() {
 
 void loop() {
   readPID();
-  delay(500);
 }
-- 
cgit v1.2.3