Created
November 3, 2017 17:08
-
-
Save rocketjosh/1a2a3ed7f7c7c160d8ae84248f07e61a to your computer and use it in GitHub Desktop.
Display MPG information with LED display and Macchina M2 with ISO9141-based vehicle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* MPG LED display using Adafruit NeoPixel and Macchina M2 with a pre-CAN bus | |
* ISO9141 (K-line) based vehicle. | |
* | |
* These parts are used in this project: | |
* | |
* https://www.macchina.cc/content/m2-under-dash | |
* https://www.adafruit.com/product/2869 | |
* | |
* Requires "Macchina M2" board to be installed and selected. For instructions, go here: | |
* http://docs.macchina.cc/m2/getting-started/arduino.html | |
* | |
* This sketch requires the following libraries: | |
* | |
* https://github.com/iwanders/OBD9141 | |
* https://github.com/adafruit/Adafruit_NeoPixel | |
* | |
*/ | |
#include <Adafruit_NeoPixel.h> | |
#include "OBD9141.h" | |
// The RX pin of the Serial1 connected to 9141 K RX of tranciever. | |
#define RX_PIN LIN_KRX | |
// The Tx pin of the Serial1 connected to 9141 K TX of tranciever. | |
#define TX_PIN LIN_KTX | |
// The Enable pin connected to 9141 SLP pin of tranciever. | |
#define EN_PIN LIN_KSLP | |
#define OBD9141_DEBUG | |
#define PIXEL_PIN RXD3 // Digital IO pin connected to the NeoPixels. | |
#define PIXEL_COUNT 8 | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800); | |
OBD9141 obd; | |
float MPG = 0; | |
int MAF = 0; | |
int VSS = 0; | |
void setup() { | |
SerialUSB.begin(115200); | |
delay(2000); | |
pinMode(EN_PIN, OUTPUT); | |
digitalWrite(EN_PIN, HIGH); | |
pinMode(PS_J1850_9141, OUTPUT); | |
digitalWrite(PS_J1850_9141, HIGH); | |
obd.begin(Serial1, RX_PIN, TX_PIN); | |
// pinMode(BUTTON_PIN, INPUT_PULLUP); | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
} | |
void loop() { | |
SerialUSB.println("Looping"); | |
// obd.set_port(false); | |
bool init_success = obd.init(); | |
SerialUSB.print("init_success:"); | |
SerialUSB.println(init_success); | |
// init_success = true; | |
// Uncomment this line if you use the simulator to force the init to be | |
// interpreted as successful. With an actual ECU; be sure that the init is | |
// succesful before trying to request PID's. | |
if (init_success) { | |
bool res = true; | |
while (res) { | |
res = obd.getCurrentPID(0x10, 2); | |
if (res) { | |
// SerialUSB.print("Result 0x10 (MAF): "); | |
// SerialUSB.println(obd.readUint16()); | |
MAF = (int(obd.readUint16())); | |
// SerialUSB.println(MAF); | |
} | |
res = obd.getCurrentPID(0x0D, 1); | |
if (res) { | |
// SerialUSB.print("Result 0x0D (speed): "); | |
// SerialUSB.println(obd.readUint8()); | |
VSS = int((obd.readUint8())); | |
// SerialUSB.println(VSS); | |
} | |
MPG = 7107 * VSS; | |
MPG = MPG / MAF ; | |
MPG = MPG / 10; | |
delay(50); | |
SerialUSB.println(MPG); | |
SerialUSB.println(); | |
for ( int i = 0; i < 8; i++ ) { // turn them all off | |
strip.setPixelColor( i, 0, 0, 0 ); | |
} | |
if (MPG > 10) { // GREEN LED | |
for ( int i = 0; i < ((MPG / 8) - 1); i++ ) { // set solid pixels to whole numbers | |
strip.setPixelColor( i, 255, 0, 0 ); | |
} | |
strip.setPixelColor((MPG / 8), int((255 * ((MPG / 8) - int(MPG / 8)))), 0, 0 ); // set partial brightness on last LED | |
} | |
if (MPG <= 10) { // RED LED | |
for ( int i = 0; i < ((MPG / 8) - 1); i++ ) { // set solid pixels to whole numbers | |
strip.setPixelColor( i, 0, 255, 0 ); | |
} | |
strip.setPixelColor((MPG / 8), 0, int((255 * ((MPG / 8) - int(MPG / 8)))), 0 ); // set partial brightness on last LED | |
} | |
strip.show(); | |
} | |
} | |
delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment