diff options
author | Stanley Huang <stanleyhuangyc@live.com> | 2018-04-11 11:38:05 +1000 |
---|---|---|
committer | Stanley Huang <stanleyhuangyc@live.com> | 2018-04-11 11:38:05 +1000 |
commit | 47b642a6e06e05d17b70c2369af5acbc2ae9daf2 (patch) | |
tree | b39c3f994a28862463c0ac04f0fddb81cc6f18ba /simple_obd_display/simple_obd_display.ino | |
parent | 26196c5808a0c1dd2fba7e357d9a7ba8aa7037d7 (diff) | |
parent | da6126b1c37ed3aa8e095a1dcc8509d4537c4819 (diff) | |
download | 2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.tar.gz 2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.tar.bz2 2021-arduino-obd-47b642a6e06e05d17b70c2369af5acbc2ae9daf2.zip |
Merge branch 'master' of https://github.com/stanleyhuangyc/ArduinoOBD
Diffstat (limited to 'simple_obd_display/simple_obd_display.ino')
-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(); + } +} |