summaryrefslogtreecommitdiff
path: root/libraries/OBD/examples/obd_uart_test
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/OBD/examples/obd_uart_test')
-rw-r--r--libraries/OBD/examples/obd_uart_test/obd_uart_test.ino88
1 files changed, 76 insertions, 12 deletions
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();
+ }
}