I have written simple code that send commands to stereo (jeep) to be able to activate CD changers inputs.
the connection is stereo to ELM327 (pin 2 (J1850)) to Arduino ( pin 7 and8)
its works only if I change if ((strtol(&rxData[0],0,16)==141) to if ((strtol(&rxData[0],0,16)>0)
The plan was: after receiving 8D OF 20 90 replay ATSH8D9301 to stereo/
but seems that I need to polish my code to be able receive 8D 0F 20 90 and then replay 8D 93 01
Code: Select all
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(7, 8); // TX | RX
//char character;
//char DATAFROMMAFON=0;
char rxData[20];
char rxIndex=0;
int led = 13;
void setup()
{
Serial.begin(38400);
BTSerial.begin(38400);
if (BTSerial.available())
Serial.write(BTSerial.read());
if (Serial.available())
//BTSerial.flush();
BTSerial.write(Serial.read());
BTSerial.write("ATL1");
BTSerial.println();
delay (500);
BTSerial.write("ATH1");
BTSerial.println();
delay (500);
BTSerial.write("ATS1");
BTSerial.println();
delay (500);
BTSerial.write("ATAL");
BTSerial.println();
delay (500);
BTSerial.write("ATE0");
BTSerial.println();
delay (500);
Serial.println("ATMA"); //J1850
BTSerial.println();
delay (500);
if (BTSerial.available() > 0) {
BTSerial.println("ATSH8D9301");// Hi Im CD changer!
BTSerial.println();
delay(50);
BTSerial.println("0000");
BTSerial.println();
delay(200);\
}
}
void loop() {// run over and over
BTSerial.flush();
getResponse();
getResponse();//
if ((strtol(&rxData[0],0,16)==141) and (strtol(&rxData[3],0,16)==15)) {
digitalWrite(13, HIGH);
BTSerial.println("ATSH8D9301");//
BTSerial.println();
delay(20);
BTSerial.println("0000"); //п
BTSerial.println();
delay(150);
}
}
void getResponse(void){
char inChar=0;
{
while(inChar != '\r'){
if(BTSerial.available() > 0){
if(BTSerial.peek() == '\r'){
inChar=BTSerial.read();
rxData[rxIndex]='\0';
rxIndex=0;
}
else {
inChar = BTSerial.read();
rxData[rxIndex++]=inChar;
}
}
}
}
}