Loading...
  Show Posts
Pages: 1 ... 6 7 [8] 9
106  Using Arduino / Programming Questions / Sodtwareserial problem. on: July 22, 2012, 05:36:57 am
Hello all, hop you can help me.

I have this bit of code

Code:
byte STN_transmit(char *str)
{
  while(*str != NULL)
  mySerial.write(*str++);
  }

and nothing is transmitted, just hangs on the line mySerial.write(*str++);. but if i use Serial.write it works fine. any ideas what is wrong here?

Thank you in advance.

Code:
///////////////////////////////
//Software serial port setup.//
///////////////////////////////
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //Rx pin 2, Tx pin 3.
//////////////
//LCD setup.//
//////////////
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define COLUMNS 16
#define ROWS 2

#define NULL '\0'
///////////////////////////////////////
//STN AT cmd's.                      //
//Asterisk (*) marks default setting.//
///////////////////////////////////////
#define STN_D    "ATD\r"   //Set all settings to defaults.
#define STN_DPN  "ATDPN\r" //Describe current protocol by number.
#define STN_E0   "ATE0\r"  //Echo off.
#define STN_E1   "ATE1\r"  //Echo on*.
#define STN_L0   "ATL0\r"  //Linefeeds off*.
#define STN_L1   "ATL1\r"  //Linefeeds on.
#define STN_M0   "ATMO\r"  //Memory off.
#define STN_M1   "ATM1\r"  //Memory on*.
#define STN_RV   "ATRV\r"  //Read voltage.
#define STN_WS   "ATWS\r"  //Warm start.
#define STN_S0   "ATS0\r"  //Printing of spaces off.
#define STN_S1   "ATS1\r"  //Printing of spaces on*.
#define STN_SP0  "ATSP0\r" //Protocol - Automatic.
#define STN_SP1  "ATSP1\r" //Protocol - SAE J1850 PWM (41.6 kbaud).
#define STN_SP2  "ATSP2\r" //Protocol - SAE J1850 VPW (10.4 kbaud).
#define STN_SP3  "ATSP3\r" //Protocol - ISO 9141-2 (5 baud init, 10.4 kbaud).
#define STN_SP4  "ATSP4\r" //Protocol - ISO 14230-4 KWP (5 baud init, 10.4kbaud).
#define STN_SP5  "ATSP5\r" //Protocol - ISO 14230-4 KWP (fast init, 10.4 kbaud).
#define STN_SP6  "ATSP6\r" //Protocol - ISO 15765-4 CAN (11 bit ID, 500 kbaud).
#define STN_SP7  "ATSP7\r" //Protocol - ISO 15765-4 CAN (29 bit ID, 500 kbaud).
#define STN_SP8  "ATSP8\r" //Protocol - ISO 15765-4 CAN (11 bit ID, 250 kbaud).
#define STN_SP9  "ATSP9\r" //Protocol - ISO 15765-4 CAN (29 bit ID, 250 kbaud).
#define STN_Z    "ATZ\r"   //Reset device.
/////////////
//PID list.//
/////////////
#define CEL     0x04 //Calculated engine load value.
#define ECT     0x05 //Engine coolant temperature.
#define FP      0x0A //Fuel pressure.
#define ERPM    0x0C //Engine RPM.
#define VS      0x0D //Vehicle speed.
#define MAF     0x10 //MAF air flow rate.
#define TP      0x11 //Throttle position.
#define FLI     0x2F //Fuel Level Input.
#define AAT     0x46 //Ambient air temperature.
#define EOT     0x5C //Engine oil temperature.
#define EFR     0x5E //Engine fuel rate.
///////////////////////////////////////////////////
//Transmits ASCII encoded HEX data to the ELM IC.//
//For example 010C\r.                            //
///////////////////////////////////////////////////
byte STN_transmit(char *str)
{
  while(*str != NULL)
  mySerial.write(*str++);
}
////////////////////////////////////////////////////////
//Places a command into a string and then transmitted.//
////////////////////////////////////////////////////////
byte STN_cmd(char *str, char *cmd)
{
  sprintf(str, "%s", cmd);
  STN_transmit(str);
}
///////////////////////////////
//Reads data from the ELM IC.//
//For example 41 0C 7B 7B>.  //
///////////////////////////////
byte STN_read(char *str)
{
  int temp;
  byte i = 0;
 
  while((temp = mySerial.read()) != '>')
  {
    //Serial.println("a");
    if(temp >= ' ')
    str[i++] = temp;
    //if(i >= 16)
    //return 0; //Read is probably bad.
  }
  str[i] = NULL;
  //return 1; //Read is probably good.
}
/////////////////////////////////////////////////////////////////
//Checks the ELM response header against the transmited header.//
/////////////////////////////////////////////////////////////////
byte STN_response_header_check(char *str_rx, char *str_tx)
{
  if(str_rx[1] == str_tx[1] && str_rx[3] == str_tx[2] && str_rx[4] == str_tx[3])
  return 1; //Header check is good.
  else
  return 0; //Header check is bad.
}
/////////////////////////////////////////////////////////////
//Skips the header and converts the received string to HEX.//
//For example 7B 7B to 0x7B7B.                             //
/////////////////////////////////////////////////////////////
byte STN_compact_response(char *str_rx, byte *str_cr)
{
  byte i = 0;
 
  str_rx += 6;
 
  while(*str_rx != NULL)
  str_cr[i++] = strtoul(str_rx, &str_rx, 16);
}
//////////////////////////////////////////////////
//Initialize and setup the STN IC ready for use.//
//////////////////////////////////////////////////
void STN_initialize()
{
  char str[24];
 
  STN_cmd(str, "ATZ\r");
  STN_read(str);
  delay(1000);
  Serial.println(str);
 
  /*mySerial.print("ATM0\r");
  STN_read();
  delay(1000);
  Serial.println(str_rx);
 
  mySerial.print("ATSP0\r");
  STN_read();
  delay(1000);
  Serial.println(str_rx);*/
}
///////////////////////////////////////////////////////////
//Requests the PID and returns the calculated PID result.//
///////////////////////////////////////////////////////////
/*int get_PID(byte PID, char *str)
{
  char str_tx[16]; //String for ELM transmit data.
  char str_rx[16]; //String for ELM receive data.
  byte str_cr[16]; //String for compact response data.
  int temp;
 
  sprintf(str_tx, "01%02X\r", PID);
  elm_transmit(str_tx);
 
  if(elm_read(str_rx) == 0)
  {
    return sprintf(str, "RERR");
  }
 
  if(elm_response_header_check(str_rx, str_tx) == 0)
  {
    return sprintf(str, "HERR");
  }
 
  elm_compact_response(str_rx, str_cr);
 
  switch(PID)
  {
    case CEL:
    temp = (str_cr[0] * 100) / 255;
    break;
    case ECT:
    temp = str_cr[0] - 40;
    break;
    case FP:
    temp = str_cr[0] * 3;
    break;
    case ERPM:
    temp = ((str_cr[0] * 256) + str_cr[1]) / 4;
    break;
    case VS:
    temp = (str_cr[0] * 10000U) / 16090U;
    break;
    case TP:
    temp = (str_cr[0] * 100) / 255;
    break;
    case FLI:
    temp = (100 * str_cr[0]) / 255;
    break;
    case AAT:
    temp = str_cr[0] -40;
    break;
    case EOT:
    temp = str_cr[0] - 40;
    break;
    case EFR:
    temp =((str_cr[0] * 256) + str_cr[1]) * 0.05;
    break;
  }
  return temp;
}*/
//////////
//Setup.//
//////////
void setup()
{
  Serial.begin(9600); //Initialize the hardware serial port.
  mySerial.begin(9600); //Initialize the software serial port.
 
  lcd.begin(COLUMNS, ROWS); //Initialize LCD.
 
  STN_initialize();
}
/////////
//Main.//
/////////
void loop()
{
  char str[16];
  int temp;
 
  //temp = get_PID(ERPM, str);
  //sprintf(str, temp);
}
/*
int calculate_MPG()
{
  int temp_VS;
  int temp_EFR;
  int result;
 
  temp_VS = get_PID(VS);
  temp_EFR = get_PID(EFR);
 
  result = temp_VS * 4.546 / temp_EFR;
  return result;
}
*/
107  Using Arduino / Programming Questions / Re: kph to mph on: June 10, 2012, 05:25:13 am
michael_x,

Code:
int temp;
temp = ((kph * 10000L + 5)/ 16090);

that worked fine but this also worked fine but only returned a value with no decimal. i don't want the decimal anyway.

Code:
int temp;
temp = str[0] / 1.609;

this is what i have done now and it looks to work fine.

Code:
    case VS:
    temp = (str[0] * 10000L) / 16090L;
    sprintf(PID_result, ("%d MPH"), temp);
    break;

thanks for the help.
108  Using Arduino / Programming Questions / Re: kph to mph on: June 10, 2012, 05:21:29 am
the value am reading is kph via a cars obd and i can't change that.
109  Using Arduino / Programming Questions / Re: kph to mph on: June 10, 2012, 05:08:43 am
thank you michael_x, i will give that a try and yes it's positive numbers.

thanks.
110  Using Arduino / Programming Questions / kph to mph on: June 10, 2012, 04:36:33 am
hi all, a little help needed.

kph / 1.609 = mph so i have seen on the internet but i don't know how i would do this in arduino. do i have to use a float?

thanks.
111  Using Arduino / Networking, Protocols, and Devices / Re: problems with elm323 on: May 19, 2012, 09:33:20 am
For comands just Google OBD pids an look for the Wiki page, should be first result. Sounds like the IC is working then. When your trying this are you connecting the ELM323 device to the car? If so it sounds like you may have a wiring problem from the car to the ELM ic.
112  Using Arduino / Networking, Protocols, and Devices / Re: problems with elm323 on: May 18, 2012, 03:04:24 pm
Do you get the ELM's start up message "ELM323----"? You don't need to send them commands. Try sending an OBD command like 01 0C an see what you get.

I am currenty doing a project with this IC to create a digital dash.
113  Using Arduino / Programming Questions / Re: Help understanding (char *). on: May 11, 2012, 08:15:31 am
It's just a standard 16x2 LCD an thanks, any examples of what you said with an array?

Thanks.
114  Using Arduino / Programming Questions / Re: Help understanding (char *). on: May 11, 2012, 07:35:31 am
Ok thanks, so it may not be good to use it then.

One more thing with this code is when i print getERPM(); to the LCD and the value is 3200 lets say and when the value gets down to 892 the 3 still remains on the lcd like 3892? Also i want my LCD to say RPM 3200 but i get errors when i try and do lcd.print("RPM ", getERPM());?

Thanks again.
115  Using Arduino / Programming Questions / Re: Help understanding (char *). on: May 11, 2012, 07:17:03 am
Many thanks all and the compiler likes it wildbill.

Am new at this and writen some code that works (below) but just can't help thinking it's the long way round and it could be more flexible. Tacke a look and tell me what you think but this is all new to me so learning all the way lol.

Code:
//LCD setup.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define Rows 2
#define Columns 16

#define Shift_LED 13

//ELMxxx AT commands.
#define ELM_RESET                    "ATZ\r"
#define ELM_DEFAULTS                 "ATD\r"
#define ELM_ECHO_OFF                 "ATE0\r"
#define ELM_LINEFEEDS_OFF            "ATL0\r"

//Vehicle PIDs.
#define ECT                          "0105"
#define ERPM                         "010C"
#define VS                           "010D"
#define EOT                          "015C"

#define NULL '\0'                                //ASCII null char.
#define PROMPT '>'                               //ASCII prompt char.

char ELM_Rx[16];                                 //ELM receive buffer.

char Byte_A[3];                                  //Byte A of PID response.
char Byte_B[3];                                  //Byte B of PID response.
byte Byte_AH;                                    //Byte A, HEX converted ASCII.
byte Byte_BH;                                    //Byte B, HEX converted ASCII.

void setup()
{
  Serial.begin(9600);                            //Initialize the serial communication.
  lcd.begin(Rows, Columns);                      //Initialize the LCD.
 
  pinMode(Shift_LED, OUTPUT);

  lcd.setCursor(3,0);
  lcd.print("OBDuno323.");
  lcd.setCursor(6,1);
  lcd.print("v0.b");
  delay(2000);
  ELM_Init();
  lcd.clear();
}

void ELM_Init()
{
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("Initialising");
  Serial.print(ELM_RESET);
  delay(1000);
  Serial.print(ELM_ECHO_OFF);                    //Turns the echoing of sent commands off.
  delay(500);
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("Initialising");
  lcd.setCursor(6, 1);
  lcd.print("Done");
  delay(1500);
}

void loop()
{
  Serial.print(getERPM());
  /*
  lcd.setCursor(0, 0);
  lcd.print(getERPM());
  if(getERPM() >= 3000)
  {
    digitalWrite(Shift_LED, HIGH);
    delay(50);
    digitalWrite(Shift_LED, LOW);
    delay(50);
  }
  */
}

byte ELM_Transmit(char* PID)
{
  Serial.println(PID);
}

int ELM_Read()
{
  char temp;
  byte i = 0;
  do
  {
    temp = Serial.read();
    if (temp >= ' ')
    {
      ELM_Rx[i++] = temp;
    }
  }
  while (temp != PROMPT);
 
  ELM_Rx[i -1] = NULL;
 
  if (i == 9)
  {
    Byte_A[0] = ELM_Rx[6];
    Byte_A[1] = ELM_Rx[7];
    Byte_A[2] = NULL;
   
    Byte_AH = strtoul(Byte_A, NULL, 16);
   
    return 1;                                    //No error in response lengh.
  }
  else if (i == 13)
  {
    Byte_A[0] = ELM_Rx[6];
    Byte_A[1] = ELM_Rx[7];
    Byte_A[2] = NULL;
   
    Byte_B[0] = ELM_Rx[9];
    Byte_B[1] = ELM_Rx[10];
    Byte_B[2] = NULL;
   
    Byte_AH = strtoul(Byte_A, NULL, 16);
    Byte_BH = strtoul(Byte_B, NULL, 16);
   
    return 1;                                    //No error in response lengh.
  }
  else
  {
    return 0;                                    //Error in response lengh.
  }
}

//Engine coolant temperature block.
int getECT()                                     //Engine Coolant Temperature.
{
  int result;                                    //Variable to store calculation result.
  ELM_Transmit((char*) ECT);
  if (ELM_Read() == 1)                           //Calls the ELM_Read block.
  {
    result = Byte_AH - 40;                       //Function calculation.
    return result;
  }
  else
  return 0;
}

//Engine RPM block.
int getERPM()                                    //Engine RPM.
{
  int result;                                    //Variable to store calculation result.
  ELM_Transmit((char*) ERPM);
  if (ELM_Read() == 1)                           //Calls the ELM_Read block.
  {
    result = ((Byte_AH * 256) + Byte_BH) / 4;    //Function calculation.
    return result;
  }
  else
  return 0;
}

//Vehicle speed block.
int getVSPD()                                    //Vehicle Speed.
{
  int result;                                    //Variable to store calculation result.
  ELM_Transmit((char*) VS);
  if (ELM_Read() == 1)                           //Calls the ELM_Read block.
  {
    result = Byte_AH;
    return result;
  }
  else
  return 0;
}

//Engine oil temperature block.
int getEOT()                                     //Engine Oil Temperature.
{
  int result;                                    //Variable to store calculation result.
  ELM_Transmit((char*) EOT);
  if (ELM_Read() == 1)                           //Calls the ELM_Read block.
  {
    result = Byte_AH - 40;                       //Function calculation.
    return result;
  }
  else
  return 0;
}
116  Using Arduino / Programming Questions / Re: Help understanding (char *). on: May 11, 2012, 06:49:26 am
Ok thanks. Can you tell me why it needs the brackets?

Many thanks.

Sorry just noticed its #define ECT "0105".
117  Using Arduino / Programming Questions / Help understanding (char *). on: May 11, 2012, 06:43:20 am
Hi all.

Can anyone explain to me why the (char *) has to be in brackets in this line of code.

Code:
ELM_Transmit((char *)ECT);

(ECT is #define ECT "0105"

Many thanks.
118  Using Arduino / Programming Questions / Re: Pass a value to a function from a function on: May 02, 2012, 05:20:17 pm
Code:

I want to use the value i pass to ELM_Read() i.e 2 that means i should have received 2 bytes of data back and 1 for 1 byte back as a way of checking and sorting the bytes of data into the correct places.

Thanks.
119  Using Arduino / Programming Questions / Re: Pass a value to a function from a function on: May 02, 2012, 05:11:28 pm
Many thanks for your replys.

Ok i see now but what if i want to replace the number 2 with a name like "rlengh"?

Thanks.
120  Using Arduino / Programming Questions / Re: Pass a value to a function from a function on: May 02, 2012, 04:21:43 pm
I might not have been clear enough lol. I have this block of code,

Code:
int getERPM()                                    //Engine RPM.
{
  int result;                                    //Variable to store calculation result.
  Serial.print(ENGINE_RPM);
  if (ELM_Read() == 1)                           //Calls the ELM_Read block.
  {
    result = ((Byte_AH * 256) + Byte_BH) / 4;    //Function calculation.
    return result;
  }
  return 0;
}

that is being called from my main loop. So inside this block i have,

Code:
if (ELM_Read() == 1)                           //Calls the ELM_Read block.
.

I want to pass the value of 2 to the ELM_Read() function. Is that possible?

Many thanks.
Pages: 1 ... 6 7 [8] 9