8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ArduinoAdvent Calendar 2015

Day 3

arduinoでOBD2 LIVE DATA DISPLAY

Last updated at Posted at 2015-12-02

#概要
自動車のOBD2のライブデータをarduino nanoで読み取って、OLEDに表示します。

#写真
2015-11-30 14.04.17.jpg
2015-11-30 14.04.28.jpg

#回路図
live.JPG

#サンプルコード

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Ohisquirt.h"

Ohisquirt squirt;
Adafruit_SSD1306 display(4);
int state = 0;
int flg = 0;
byte mode = 0x0C;
void setup()
{
	pinMode(8, INPUT_PULLUP);
	squirt.begin();
	display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
	display.clearDisplay();
	display.display();
	display.setTextColor(WHITE);
	display.clearDisplay();
	display.setTextSize(2);
	display.setCursor(0, 0);
	display.print("live");
	display.setCursor(0, 25);
	display.setTextSize(4);
	display.print("init");
	display.display();
	while (!squirt.init());
	ELM_PROTOCOLS h = PROTO_ISO_9141_2;
	if (squirt.setProtocol(h))
	{
		display.clearDisplay();
		display.setTextSize(2);
		display.setCursor(0, 0);
		display.print("ISO_9141_2");
		display.display();
	}
}
void loop()
{
	int sw = digitalRead(8);
	int value = 0;
	byte pid = 0;
	display.clearDisplay();
	display.setTextSize(2);
	display.setCursor(0, 0);
	if (sw == HIGH)
	{
		flg = 0;
	}
	else
	{
		if (flg == 1)
		{
			state++;
			if (state > 9) state = 0;
		}
		flg = 1;
	}
	if (flg == 0)
	{
		squirt.sendQuery(mode);
		if (squirt.getResult(pid, value))
		{
			switch (mode)
			{
			case 0x04:
				display.print("ENGINE_LOAD");
			break;
			case 0x05:
				display.print("COOLANT_TEMP");
			break;
			case 0x06:
				display.print("SHORT_FUEL_TRIM");
			break;
			case 0x07:
				display.print("LONG_FUEL_TRIM");
			break;
			case 0x0B:
				display.print("INTAKE_MAP");
			break;
			case 0x0C:
				display.print("RPM");
			break;
			case 0x0D:
				display.print("SPEED");
			break;
			case 0x0E:
				display.print("TIMING_ADVANCE");
			break;
			case 0x0F:
				display.print("INTAKE_TEMP");
			break;
			case 0x11:
				display.print("THROTTLE");
			break;
			}
			display.setCursor(0, 35);
			display.setTextSize(4);
			display.print(value);
			display.setCursor(90, 50);
			display.setTextSize(2);
			switch (mode)
			{
			case 0x04:
				display.print("%");
			break;
			case 0x05:
				display.print("`C");
			break;
			case 0x06:
				display.print("%");
			break;
			case 0x07:
				display.print("%");
			break;
			case 0x0B:
				display.print("KPa");
			break;
			case 0x0C:
				display.print("RPM");
			break;
			case 0x0D:
				display.print("km/h");
			break;
			case 0x0E:
				display.print("deg");
			break;
			case 0x0F:
				display.print("`C");
			break;
			case 0x11:
				display.print("%");
			break;
			}
		}
	}
	else
	{
		switch (state)
		{
		case 0:
			display.print("RPM");
			mode = 0x0C;
		break;
		case 1:
			display.print("SPEED");
			mode = 0x0D;
		break;
		case 2:
			display.print("THROTTLE");
			mode = 0x11;
		break;
		case 3:
			display.print("COOLANT_TEMP");
			mode = 0x05;
		break;
		case 4:
			display.print("INTAKE_TEMP");
			mode = 0x0F;
		break;
		case 5:
			display.print("INTAKE_MAP");
			mode = 0x0B;
		break;
		case 6:
			display.print("ENGINE_LOAD");
			mode = 0x04;
		break;
		case 7:
			display.print("SHORT_FUEL_TRIM");
			mode = 0x06;
		break;
		case 8:
			display.print("LONG_FUEL_TRIM");
			mode = 0x07;
		break;
		case 9:
			display.print("TIMING_ADVANCE");
			mode = 0x0E;
		break;
		}
	}
	display.display();
	delay(700);
}

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?