Here is the code that worked:
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int Green = 7; // select the pin for the LED
int Yellow = 8;
int Red = 9;
#include <Wire.h>
#include <OBD.h>
COBD obd;
void setup() {
// set up the number of columns and rows on the LCD
lcd.begin(16, 2);
// Pin Setup
pinMode(Green, OUTPUT);
pinMode(Yellow, OUTPUT);
pinMode(Red, OUTPUT);
// start communication with OBD-II adapter
obd.begin();
// initiate OBD-II connection until success
while (!obd.init());
// Print a message to the LCD.
lcd.print("Speed");
// set the cursor to column 0, line 1
// line 1 is the second row, since counting begins with 0
lcd.setCursor(12, 0);
lcd.print("km/t");
lcd.setCursor(0, 1);
lcd.print("Temp");
lcd.setCursor(8, 1);
lcd.print("RPM");
}
void loop() {
int rpm;
// save engine RPM in variable 'value', return true on success
if (obd.readPID(PID_RPM, rpm)) {
// light on LED on Arduino board when the RPM exceeds 3000
digitalWrite(Green, rpm > 3000 ? HIGH : LOW);
digitalWrite(Yellow, rpm > 4000 ? HIGH : LOW);
digitalWrite(Red, rpm > 5000 ? HIGH : LOW);
}
lcd.setCursor(12, 1);
lcd.print(rpm);
int Speed;
// save engine RPM in variable 'value', return true on success
if (obd.readPID(PID_SPEED, Speed))
lcd.setCursor(6, 0);
lcd.print(Speed);
int Water;
// save engine RPM in variable 'value', return true on success
if (obd.readPID(PID_COOLANT_TEMP, Water))
lcd.setCursor(5, 1);
lcd.print(Water);
}
Here is the full code for our project that does not work:
#include <LiquidCrystal.h> //LCD Screen Library and Pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int switchPin = 6; // momentary switch (Push Button) on Pin 6, other side connected to ground (Page Turner)
int Display = 0;
int Green = 7; // select the pins and names for the LED's
int Yellow = 8;
int Red = 9;
#include <Wire.h> //Library for Data Lines Between OBD-Plug and Arduino in 8-bit
#include <OBD.h> //Library for OBD PID Codes
COBD obd; //* for Model A (UART version) (Just write it)
void setup()
{
lcd.begin(16, 2);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH); // momentary switch (Push Button) turn on pullup resistor
lcd.begin(16, 2); // set up the number of columns and rows on the LCD (16x2)
pinMode(Green, OUTPUT); // LED Pin Setup
pinMode(Yellow, OUTPUT);
pinMode(Red, OUTPUT);
obd.begin(); // start communication with OBD-II adapter
while (!obd.init()); // initiate OBD-II connection until success
lcd.print("OK Project OBDII"); // Print a Start message to the LCD.
lcd.setCursor(0,1); // set the cursor to column 0, line 1
lcd.print("Beta Version"); // line 1 is the second row, since counting begins with 0
}
void loop()
{
// int #; // save/initiate PID 'value' as new name, return true on success
// if (obd.readPID(PID_#, #))
if (digitalRead(switchPin) == LOW){
delay(700); // delay to debounce switch, Hold it down for scroll
Display = Display + 1;
if(Display > 5){ // Number of pages (Display > #pages)
lcd.clear();
Display = 1;
}
switch (Display) {
case 1: {
lcd.clear();
int Speed; //Unit: km/h
if (obd.readPID(PID_SPEED, Speed))
lcd.setCursor(0, 0);
lcd.print("Speed");
lcd.setCursor(6, 0);
lcd.print(Speed);
lcd.setCursor(12, 0);
lcd.print("km/t");
int Water; //Unit: Degrees C
if (obd.readPID(PID_COOLANT_TEMP, Water))
lcd.setCursor(0, 1);
lcd.print("Temp");
lcd.setCursor(5, 1);
lcd.print(Water);
int rpm; //Unit: Ratio
if (obd.readPID(PID_RPM, rpm))
lcd.setCursor(8, 1);
lcd.print("RPM");
lcd.setCursor(12, 1);
lcd.print(rpm);
{ digitalWrite(Green, rpm > 2500 ? HIGH : LOW); // light the LED's when RPM exceeds wanted limit
digitalWrite(Yellow, rpm > 3500 ? HIGH : LOW);
digitalWrite(Red, rpm > 4000 ? HIGH : LOW);}
break; }
case 2: {
lcd.clear();
int IntakeTemp; //Unit: Degrees C
if (obd.readPID(PID_INTAKE_TEMP, IntakeTemp))
lcd.setCursor(0, 0);
lcd.print("Intake Temp");
lcd.setCursor(13, 0);
lcd.print(IntakeTemp);
int MAF; //Unit: g/s
if (obd.readPID(PID_MAF_FLOW, MAF))
lcd.setCursor(0, 1);
lcd.print("MAF");
lcd.setCursor(4, 1);
lcd.print(MAF);
int AFR; //Unit: Ratio
if (obd.readPID(PID_AIR_FUEL_EQUIV_RATIO, AFR))
lcd.setCursor(8, 1);
lcd.print("AFR");
lcd.setCursor(12, 1);
lcd.print(AFR);
break; }
case 3: {
lcd.clear();
int Runtime; //Unit: s since Engine start
if (obd.readPID(PID_RUNTIME, Runtime))
lcd.setCursor(0, 0);
lcd.print("Runtime");
lcd.setCursor(8, 0);
lcd.print(Runtime);
int FuelLVL; //Unit: %
if (obd.readPID(PID_FUEL_LEVEL, FuelLVL))
lcd.setCursor(0, 1);
lcd.print("Fuel Level");
lcd.setCursor(11, 1);
lcd.print(FuelLVL);
lcd.setCursor(15, 1);
lcd.print("%");
break; }
case 4: {
lcd.clear();
int Throttle; //Unit: %
if (obd.readPID(PID_THROTTLE, Throttle))
lcd.setCursor(0, 0);
lcd.print("Throttle");
lcd.setCursor(10, 0);
lcd.print(Throttle);
lcd.setCursor(15, 0);
lcd.print("%");
int Tadv; //Unit: Degrees before TDC
if (obd.readPID(PID_TIMING_ADVANCE, Tadv))
lcd.setCursor(0, 1);
lcd.print("Timing adv");
lcd.setCursor(13, 1);
lcd.print(Tadv);
break; }
case 5: {
lcd.clear();
int Oil; //Unit: Degrees C
if (obd.readPID(PID_ENGINE_OIL_TEMP, Oil))
lcd.setCursor(0, 0);
lcd.print("Oil Temp");
lcd.setCursor(10, 0);
lcd.print(Oil);
int ECU_V; //Unit: V
if (obd.readPID(PID_CONTROL_MODULE_VOLTAGE, ECU_V))
lcd.setCursor(0, 1);
lcd.print("ECU Voltage");
lcd.setCursor(12, 1);
lcd.print(ECU_V);
break; }
}
}
}
septillion but it does not read every time we press the button, it only reads the first time? 