Paso 3: Código para cargar en ARDUINO
Abajo está el código que usé para esto!
Tengo la mayor parte de este.
incluye el código de biblioteca:
#include
Declarar leer Serial variables int CmdCount = 1; inData bytes; inChar Char; String BuildINString = ""; Cadena DisplayString = ""; DisplayValue largo; String SentMessage = ""; int ByteCount = 0; A largo; int B; int WorkingVal; String WorkingString = ""; int RefreshCounter = 0; Cuando la lectura debe actualizar int RefreshCounterMax = 50; Ajustar esta frecuencia int RESETMenuName = 0;
MenuAnalogVal int x; Declarar botones int MenuID = 0; int UpDownMenu = 0;
inicializar la biblioteca con los números de los pines de la interfaz lcd LiquidCrystal (8, 9, 4, 5, 6, 7);
void setup() {/ / configurar número la pantalla del LCD de filas y columnas: lcd.begin (16, 2); / / imprimir un mensaje en el LCD. lcd.setCursor (0, 0); Arranque (); //Simulate el proceso de arranque no tiene que ser aquí esto sólo hace le mirada fresca Reintentar: lcd.setCursor (0, 0); LCD.Print ("conectando... "); lcd.setCursor (0, 1); LCD.Print(""); Serial.Begin(38400); Delay(500); Enviar un mensaje de prueba para ver si el Chip ELM327 responde SentMessage = "ATI"; Serial.println("ATI");DELAY(500); ReadData(); Si (BuildINString.substring(0,1)=="elm327") {lcd.setCursor (0, 0); lcd.print ("Bienvenido... "); lcd.setCursor (0, 1); LCD.Print ("conexión OK"); Delay(1500); DisplayString = "Leyendo datos..."; }
Delay(1500); }
void loop() {RefreshCounter ++;
x = analogRead (0); Serial.println(x); lcd.setCursor(10,1); Si (x > 800 y x < 820){lcd.print ("Select");} Si (x > 620 y x < 630) {si (MenuID > 0) {MenuID--;} DisplayString = "lectura Data...";delay(250);} Izquierda if (x > 400 y < 415) {si (UpDownMenu < 3) {UpDownMenu ++;} Delay(250); RESETMenuName = 0;} ABAJO si (x > 190 y x < 215) {si (UpDownMenu > 0) {UpDownMenu--;} Delay(250); RESETMenuName = 0;} ARRIBA si (x >-10 y x < 10) {si (MenuID < 13) {MenuID ++;} DisplayString = "lectura Data...";delay(250);} Derecho
Si (UpDownMenu == 0) {if(RESETMenuName==0) {lcd.setCursor (0, 0);lcd.print ("lecturas de motor"); lcd.setCursor (0, 1);lcd.print(""); delay(2000); RESETMenuName = 1;} Si (MenuID==0){lcd.setCursor (0, 0);lcd.print ("Temp refrigerante 01"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage = "01 05"; Serial.println ("01 05");delay(50); ReadData() ;}} Si (MenuID==1){lcd.setCursor (0, 0);lcd.print («02 IAT Temp"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage ="01 0F"; Serial.println ("01 0F");delay(50); ReadData() ;}} Si (MenuID==2){lcd.setCursor (0, 0);lcd.print ("Temp ambiente 03"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage = "01 46"; Serial.println ("01 46");delay(50); ReadData() ;}} Si (MenuID==3){lcd.setCursor (0, 0);lcd.print ('04% del acelerador'); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage = "01 11"; Serial.println ("01 11");delay(50); ReadData() ;}} Si (MenuID==4){lcd.setCursor (0, 0);lcd.print («05 CAT 1 Temp"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage ="01 3"; Serial.println ("01 3C");delay(50); ReadData() ;}} Si (MenuID==5){lcd.setCursor (0, 0);lcd.print ("06 CAT 2 Temp"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage = "01 3D"; Serial.println ("01 3D");delay(50); ReadData() ;}} Si (MenuID==6){lcd.setCursor (0, 0);lcd.print ("07 CAT 3 Temp"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage = "01 3E"; Serial.println ("01 3E");delay(50); ReadData() ;}} Si (MenuID==7){lcd.setCursor (0, 0);lcd.print («08 CAT 4 Temp"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage ="01 3F"; Serial.println ("01 3F");delay(50); ReadData() ;}} Si (MenuID==8){lcd.setCursor (0, 0);lcd.print ("09 RPM"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage = "01 0C"; Serial.println ("01 0C");delay(50); ReadData() ;}} Si (MenuID==9){lcd.setCursor (0, 0);lcd.print ("10 velocidad del vehículo"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage = "01 0d"; Serial.println ("01 0D");delay(50); ReadData() ;}} Si (MenuID==10){lcd.setCursor (0, 0);lcd.print («11 caudal de aire"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage =" 01 10"; Serial.println ("01 10");delay(50); ReadData() ;}} Si (MenuID==11){lcd.setCursor (0, 0);lcd.print («barométrico 12"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage =" 01 33"; Serial.println ("01 33");delay(50); ReadData() ;}} Si (MenuID==12){lcd.setCursor (0, 0);lcd.print ("EGT 13"); lcd.setCursor (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage = "01 78"; Serial.println ("01 78");delay(50); ReadData() ;}} Si (MenuID==13){lcd.setCursor (0, 0);lcd.print ("Temp");lcd.setCursor de aceite 14 (0, 1);lcd.print (DisplayString); si (RefreshCounter == RefreshCounterMax) {SentMessage = "01 5"; Serial.println ("01 5C");delay(50); ReadData() ;}} } Si (UpDownMenu == 1) {if(RESETMenuName==0) {lcd.setCursor (0, 0);lcd.print ("Trans lecturas"); lcd.setCursor (0, 1);lcd.print(""); delay(2000); RESETMenuName = 1;} lcd.setCursor (0, 0);lcd.print ("no implementado");} Si (UpDownMenu == 2) {if(RESETMenuName==0) {lcd.setCursor (0, 0);lcd.print ("configuración"); lcd.setCursor (0, 1);lcd.print(""); delay(2000); RESETMenuName = 1;} lcd.setCursor (0, 0);lcd.print ("no implementado");} Si (UpDownMenu == 3) {if(RESETMenuName==0) {lcd.setCursor (0, 0);lcd.print ("Zackery France");lcd.setCursor (0, 1);lcd.print ("\\Sensors Final"); delay(2000); RESETMenuName = 1;} lcd.setCursor (0, 0);lcd.print ("versión del sistema");} Reiniciar RefreshCounter si (RefreshCounter > = RefreshCounterMax) {RefreshCounter = 0;} delay(1);
}
Leer los datos y actuar en consecuencia void ReadData() {}
BuildINString = ""; while(serial.Available() > 0) {inData = 0; inChar = 0; inData = Serial.read(); inChar=char(inData); BuildINString = BuildINString + inChar; } / / if(BuildINString!=""){Serial.print(BuildINString);} BuildINString.replace(SentMessage,""); BuildINString.replace (">",""); BuildINString.replace("OK",""); BuildINString.replace("STOPPED",""); BuildINString.replace("SEARCHING",""); BuildINString.replace ("sin datos",""); BuildINString.replace("?",""); BuildINString.replace(",",""); Serial.Print(BuildINString); Comprobar que se envió el comando OBD y calcular el valor //Calculate devuelto RPM es decir bytes será 41 1B de 0 C E0 //RPM Si (SentMessage == "01 C 0") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16); WorkingString = BuildINString.substring(10,12); B = strtoul(WorkingString.c_str(),NULL,16); DisplayValue = ((A * 256) + B) / 4; DisplayString = String(DisplayValue) + "rpm"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //Calculate vehículo velocidad es decir devuelto bytes será 41 1B de 0 C E0 si (SentMessage == "01 0d") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16);
DisplayValue = A; DisplayString = String(DisplayValue) + "km/h"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //Coolant temp si (SentMessage == "01 05") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16);
DisplayValue = A - 40; DisplayString = String(DisplayValue) + "C"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //IAT temp si (SentMessage == "01 0F") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16);
DisplayValue = un -40; DisplayString = String(DisplayValue) + "C"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //Air velocidad de flujo si (SentMessage == "01 10") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16); WorkingString = BuildINString.substring(11,13); B = strtoul(WorkingString.c_str(),NULL,16); DisplayValue = ((A * 256) + B) / 100; DisplayString = String(DisplayValue) + g "s"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //Ambient temp si (SentMessage == "01 46") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16);
DisplayValue = un -40; DisplayString = String(DisplayValue) + "C"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } if tEMP de aceite de //eNGINE (SentMessage == "01 C 5") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16);
DisplayValue = un -40; DisplayString = String(DisplayValue) + "C"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //Throttle posición si (SentMessage == "01 11") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16);
DisplayValue = A * 100/255; DisplayString = String(DisplayValue) + "%"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //Barometric de presión si (SentMessage == "01 33") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16);
DisplayValue = A; DisplayString = String(DisplayValue) + "kpa"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //CAT 1 temp / / ((A*256) + B) / 10-40 if (SentMessage == "01 3 C") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16); WorkingString = BuildINString.substring(10,12); B = strtoul(WorkingString.c_str(),NULL,16); DisplayValue = ((A * 256) + B) / 10-40; DisplayString = String(DisplayValue) + "C"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //CAT 2 temp / / ((A*256) + B) / 10-40 if (SentMessage == "01 3D") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16); WorkingString = BuildINString.substring(10,12); B = strtoul(WorkingString.c_str(),NULL,16); DisplayValue = ((A * 256) + B) / 10-40; DisplayString = String(DisplayValue) + "C"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //CAT 3 temp / / ((A*256) + B) / 10-40 if (SentMessage == "01 3E") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16); WorkingString = BuildINString.substring(10,12); B = strtoul(WorkingString.c_str(),NULL,16); DisplayValue = ((A * 256) + B) / 10-40; DisplayString = String(DisplayValue) + "C"; lcd.setCursor (0, 1); LCD.Print(DisplayString); } //CAT 4 temp / / ((A*256) + B) / 10-40 if (SentMessage == "01 3F") {WorkingString = BuildINString.substring(7,9); A = strtoul(WorkingString.c_str(),NULL,16); WorkingString = BuildINString.substring(10,12); B = strtoul(WorkingString.c_str(),NULL,16); DisplayValue = ((A * 256) + B) / 10-40; DisplayString = String(DisplayValue) + "C"; lcd.setCursor (0, 1); LCD.Print(DisplayString); }
}
void Bootup() {lcd.print ("coche lector"); para (int i = 0; i < = 1; i ++) {para (int j = 1; j < = 4; j ++) /-/ |\-{if(j==1) {lcd.setCursor (0, 1);lcd.print ("-"); delay(200);} if(j==2) {lcd.setCursor (0, 1);lcd.print ("/"); delay(200);} if(j==3) {lcd.setCursor (0, 1);lcd.print ("|"); Delay(200);} if(j==4) {lcd.setCursor (0, 1);lcd.print "(\\");delay(200);}} } lcd.setCursor (2, 1); LCD.Print ("Zackery Francia"); Delay(2000); }