From 251bd01111ab0626fb861a391578445d9e6c7d26 Mon Sep 17 00:00:00 2001 From: Stanley Huang Date: Tue, 10 Oct 2017 23:25:13 +1100 Subject: Added simple OBD data display example sketch --- simple_obd_display/simple_obd_display.ino | 105 ++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 simple_obd_display/simple_obd_display.ino (limited to 'simple_obd_display/simple_obd_display.ino') 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 +*************************************************************************/ + +#include +#include +#include + +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(); + } +} -- cgit v1.2.3