summaryrefslogtreecommitdiff
path: root/libraries/OBD2UART
diff options
context:
space:
mode:
authorStanley Huang <stanleyhuangyc@gmail.com>2016-12-19 17:22:08 +1100
committerStanley Huang <stanleyhuangyc@gmail.com>2016-12-19 17:22:08 +1100
commitc83f19feb55fb3c405431bd12af4132d402cf436 (patch)
tree01c89e7489ec0cf080e0d862e41b31b51a1c20fd /libraries/OBD2UART
parente14808fc32529033a615416f4cf35f6918750e60 (diff)
download2021-arduino-obd-c83f19feb55fb3c405431bd12af4132d402cf436.tar.gz
2021-arduino-obd-c83f19feb55fb3c405431bd12af4132d402cf436.tar.bz2
2021-arduino-obd-c83f19feb55fb3c405431bd12af4132d402cf436.zip
Updated example sketch
Reading DTC and MEMS data
Diffstat (limited to 'libraries/OBD2UART')
-rw-r--r--libraries/OBD2UART/examples/obd_uart_test/obd_uart_test.ino87
1 files changed, 76 insertions, 11 deletions
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();
+ }
}