diff options
author | Stanley Huang <stanleyhuangyc@gmail.com> | 2017-10-10 23:25:13 +1100 |
---|---|---|
committer | Stanley Huang <stanleyhuangyc@gmail.com> | 2017-10-10 23:25:13 +1100 |
commit | 251bd01111ab0626fb861a391578445d9e6c7d26 (patch) | |
tree | 6aae4207f61431a3d881b2beaa2e0728bf4d419f | |
parent | 668e49d0471c9caa7243d51d416dde34bb0e00f3 (diff) | |
download | 2021-arduino-obd-251bd01111ab0626fb861a391578445d9e6c7d26.tar.gz 2021-arduino-obd-251bd01111ab0626fb861a391578445d9e6c7d26.tar.bz2 2021-arduino-obd-251bd01111ab0626fb861a391578445d9e6c7d26.zip |
Added simple OBD data display example sketch
-rw-r--r-- | simple_obd_display/simple_obd_display.ino | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/simple_obd_display/simple_obd_display.ino b/simple_obd_display/simple_obd_display.ino new file mode 100644 index 0000000..ca36236 --- /dev/null +++ b/simple_obd_display/simple_obd_display.ino @@ -0,0 +1,105 @@ +/************************************************************************* +* Simple OBD Data Display +* Works with any Arduino board connected with SH1106 128*64 I2C OLED and +* Freematics OBD-II UART Adapter - https://freematics.com/products +* Distributed under public domain +* Written by Stanley Huang <stanley@freematics.com.au> +*************************************************************************/ + +#include <Wire.h> +#include <OBD2UART.h> +#include <MicroLCD.h> + +LCD_SH1106 lcd; +COBD obd; + +void reconnect() +{ + lcd.clear(); + lcd.setFontSize(FONT_SIZE_MEDIUM); + lcd.print("Reconnecting"); + //digitalWrite(SD_CS_PIN, LOW); + for (uint16_t i = 0; !obd.init(); i++) { + if (i == 5) { + lcd.clear(); + } + delay(3000); + } +} + +void showData(byte pid, int value) +{ + switch (pid) { + case PID_RPM: + lcd.setCursor(64, 0); + lcd.setFontSize(FONT_SIZE_XLARGE); + lcd.printInt((unsigned int)value % 10000, 4); + break; + case PID_SPEED: + lcd.setCursor(0, 0); + lcd.setFontSize(FONT_SIZE_XLARGE); + lcd.printInt((unsigned int)value % 1000, 3); + break; + case PID_THROTTLE: + lcd.setCursor(88, 5); + lcd.setFontSize(FONT_SIZE_MEDIUM); + lcd.printInt(value % 100, 3); + lcd.setFontSize(FONT_SIZE_SMALL); + lcd.print(" %"); + break; + case PID_ENGINE_LOAD: + lcd.setCursor(12, 5); + lcd.setFontSize(FONT_SIZE_MEDIUM); + lcd.printInt(value, 3); + lcd.setFontSize(FONT_SIZE_SMALL); + lcd.print(" %"); + break; + } +} + +void initScreen() +{ + lcd.clear(); + lcd.setFontSize(FONT_SIZE_SMALL); + lcd.setCursor(24, 3); + lcd.print("km/h"); + lcd.setCursor(110, 3); + lcd.print("rpm"); + lcd.setCursor(0, 7); + lcd.print("ENGINE LOAD"); + lcd.setCursor(80, 7); + lcd.print("THROTTLE"); +} + +void setup() +{ + lcd.begin(); + lcd.setFontSize(FONT_SIZE_MEDIUM); + lcd.println("OBD DISPLAY"); + + delay(500); + obd.begin(); + + lcd.println(); + lcd.println("Connecting..."); + while (!obd.init()); + initScreen(); +} + +void loop() +{ + static byte pids[]= {PID_RPM, PID_SPEED, PID_ENGINE_LOAD, PID_THROTTLE}; + static byte index = 0; + byte pid = pids[index]; + int value; + // send a query to OBD adapter for specified OBD-II pid + if (obd.readPID(pid, value)) { + showData(pid, value); + } + index = (index + 1) % sizeof(pids); + + if (obd.errors >= 2) { + reconnect(); + setup(); + } +} |