diff options
author | Stanley Huang <stanleyhuangyc@gmail.com> | 2016-02-26 13:43:45 +1100 |
---|---|---|
committer | Stanley Huang <stanleyhuangyc@gmail.com> | 2016-02-26 13:43:45 +1100 |
commit | d484853a7af5a6fbef8aaff198aa9c900c4f69c7 (patch) | |
tree | fd1ae8ecb4539c1867b7a0d653d14b259e322bbd /libraries/OBD/examples/obd_uart_test/obd_uart_test.ino | |
parent | 3df4070034d4fc9dc8464d08fab53b65e3729a2e (diff) | |
download | 2021-arduino-obd-d484853a7af5a6fbef8aaff198aa9c900c4f69c7.tar.gz 2021-arduino-obd-d484853a7af5a6fbef8aaff198aa9c900c4f69c7.tar.bz2 2021-arduino-obd-d484853a7af5a6fbef8aaff198aa9c900c4f69c7.zip |
Added demo of multiple OBD-II PIDs reading API
Diffstat (limited to 'libraries/OBD/examples/obd_uart_test/obd_uart_test.ino')
-rw-r--r-- | libraries/OBD/examples/obd_uart_test/obd_uart_test.ino | 42 |
1 files changed, 25 insertions, 17 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 3f04cdf..484262e 100644 --- a/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino +++ b/libraries/OBD/examples/obd_uart_test/obd_uart_test.ino @@ -12,7 +12,7 @@ #include <OBD.h> // On Arduino Leonardo, Micro, MEGA or DUE, hardware serial can be used for output -// as OBD-II adapter should connect to Serial1, otherwise we use software serial +// as OBD-II UART adapter connects to Serial1, otherwise we use software serial SoftwareSerial mySerial(A2, A3); //#define mySerial Serial @@ -48,26 +48,33 @@ void testOut() mySerial.println(); } -void readPID() +void readPIDSingle() { - 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}; + int value; mySerial.print('['); mySerial.print(millis()); mySerial.print(']'); - for (byte i = 0; i < sizeof(pidlist) / sizeof(pidlist[0]); i++) { - byte pid = pidlist[i]; - bool valid = obd.isValidPID(pid); - mySerial.print((int)pid | 0x100, HEX); + mySerial.print("RPM="); + if (obd.read(PID_RPM, value)) { + mySerial.print(value); + } + mySerial.println(); +} + +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.read(pids, sizeof(pids), values) == sizeof(pids)) { + for (byte i = 0; i < sizeof(pids) ; i++) { + mySerial.print('['); + mySerial.print(millis()); + mySerial.print(']'); + mySerial.print((int)pids[i] | 0x100, HEX); mySerial.print('='); - if (valid) { - int value; - if (obd.read(pid, value)) { - mySerial.print(value); - } - } - mySerial.print(' '); - } - mySerial.println(); + mySerial.println(values[i]); + } + } } void setup() @@ -95,5 +102,6 @@ void setup() void loop() { - readPID(); + readPIDSingle(); + readPIDMultiple(); } |