View Single Post
Old 06-27-2008, 03:54 PM   #21 (permalink)
n8thegr8
EcoModding Lurker
 
Join Date: Jun 2008
Location: USA
Posts: 32

N8sPony - '98 Ford Mustang
Thanks: 0
Thanked 0 Times in 0 Posts
Well, this is what I've done so far, ignore the serial stuff, I was doing that out of naivety, (actually, it might work if you have a rs232>OBDII adapter), feel free to take whatever you want out of it. Basically all I was interested in was instant mpg, speed, and rpm at the moment, so all the calculations and such should work. The display code definitely works as tested, haven't hooked it into a vehicle yet though. I'm waiting on a serial>usb adapter for my laptop to continue work on it.

Code:
/*
    OpenScanGauge
*/

#include <LCD4Bit.h> 
#include <stdio.h>

LCD4Bit lcd = LCD4Bit(4); 

double vss;
double maf;
double mpg;
int rpm;

//pins to use for data
int busneg=0;
int buspos=1;

//arrays to store values returned from ECU
char vss_byte[10];
char maf_byte[10];
char rpm_byte[10];

//hex commands for speed, MAF, and rpm
char* vss_cmd[6] = {"61", "6A", "F1", "01", "0D", "8B"};
char* maf_cmd[6] = {"61", "6A", "F1", "01", "10", "C7"};
char* rpm_cmd[6] = {"61", "6A", "F1", "01", "0C", "96"};

//array to look up binary values for hex chars
char* hexconvert[15][2] = {{"0","0000"},{"1","0001"},{"2","0010"},{"3","0011"},{"4","0100"},{"5","0101"},{"6","0110"},{"7","0111"},{"8","1000"},{"9","1001"},{"A","1010"},{"B","1011"},{"C","1100"},{"D","1101"},{"E","1110"},{"F","1111"}}
char vss_str[5];
char mpg_str[4];
char rpm_str[5];

void setup() {
   lcd.init();
}

void loop() {  
  getValues();
  printOutput();
}

void printOutput() {
  lcd.clear();
  lcd.cursorTo(1,0);
  lcd.printIn("MPG: ");
  lcd.cursorTo(1,5);
  mpg = (710.7 * vss) / maf;
  sprintf(mpg_str, "%f", mpg);
  lcd.printIn(mpg_str);
  lcd.cursorTo(1,9);
  lcd.printIn("Speed: ");
  lcd.cursorTo(1,16);
  vss = vss * 0.6214;
  sprintf(vss_str, "%f", vss);
  lcd.printIn(vss_str);
  lcd.cursorTo(3,0);
  lcd.printIn("RPM: ");
  lcd.cursorTo(3,5);
  sprintf(rpm_str, "%d", rpm);
  lcd.printIn(rpm_str);
  delay (1000);
}

void getValues() {
    pwm_send(vss_cmd);
    vss_byte = pwm_read();
    vss = vss_byte[5];
    pwm_send(rpm_cmd);
    rpm_byte = pwm_read();
    rpm = .25 * (rpm_byte[5] * 256 + rpm_byte[6]);
    pwm_send(maf_cmd);
    maf_byte = pwm_read();
    maf = .01 * ((256 * maf_byte[5]) + maf_byte[6]);
}

void pwm_send(char* cmd[]) {
    char bincmd[] = hex2bin(cmd[]);
    pinmode(busneg, OUTPUT);
    
    //send SOF and drop back to neutral
    AnalogWrite(buspos, 255);
    digitalWrite(busneg, LOW);
    delayMicroseconds(32);
    AnalogWrite(buspos, 0);
    digitalWrite(busneg, LOW);
    
    //loop through array of bits
    for (int i=0; i < 48; i++) {
    	//send 0
        if bincmd[i] = '0' {
            analogwrite(buspos, 255);
            digitalwrite(busneg, LOW);
            delaymicroseconds(16);
            AnalogWrite(buspos, 0);
            digitalWrite(busneg, LOW);
        }
        //send 1 
        else if (bincmd[i] = '1') {
            analogwrite(buspos, 255);
            digitalwrite(busneg, LOW);
            delaymicroseconds(8);
            AnalogWrite(buspos, 0);
            digitalWrite(busneg, LOW);
        }
    }
    //send EOF to be here
}

char[] pwm_read() {
    pinmode(busneg, INPUT);
    //code for reading values here (complex methinks)
}

//convert hex command to binary
char[] hex2bin(char* cmd[]) {
    char bincmd[];
    int binloc = 0;
    for(int i=0; i < 6; i++) {
        for(int j=0; j < 15; j++) {
            if (cmd[i][0] = hexconvert[j][0]) {
                for (int k=; k < 4; k++) {
                    bincmd[binloc] = hexconvert[j][1][k];
                    binloc++;
                }
              }
        }
        for(int j=0; j < 15; j++) {
            if (cmd[i][1] = hexconvert[j][0]) {
                for (int k=; k < 4; k++) {
                bincmd[binloc] = hexconvert[j][1][k];
                binloc++;
                }
            }
        }
    }
    return bincmd;
}


Last edited by n8thegr8; 06-30-2008 at 03:23 PM..
  Reply With Quote