I have a strange problem with a project im working on. Im connecting a Seed Studio CAN shield to an aftermarket car ECU. I modified some source code i found on GIT for the specific ECU, but for some reason i dont always get any data. If i reset the Arduino a few times in a row it usually starts printing to the serial monitor, i can confirm that once i receive data it’s always correct.
When i switched on the igntion (this is a race car so just a switch to turn the ECU on and a button to crank engine) i never get any data unless i keep resetting the Arduino. The arduino is powered up when i turn the ignition up, so ECU and Arduino starts at the same time, but i use voltage converter to make sure the Arduino only get 5v.
I tried to power the Ardunio directly from USB from the computer and then power up the ECU then the data usually appears in the serial monitor directly at least 9 times out of 10
Any ideas why this is happening? I did a serial print in the each case and it never prints out anything when i dont recieve any data so for somtimes it does not get the correct identifier.
The ECU i sends with 500 kbit and the protocol used in this example looks likes this for in this example RPM, engine temp and manifold temperature (AIM variant 2 protocol)
ID A ID = 0x770
Byte 0:1 =RPM
ID B = 0x771
Byte 0:1 = CLT
ID C = 0x772
Byte 2:3 = CLT
Is there something i have missed? Im suspecting im getting corrupted data sometimes
#include "mcp_can.h"
#include <SPI.h>
#include <stdio.h>
#define INT8U unsigned char
const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN);
INT8U can_len = 0;
INT8U can_buf[8];
INT8U ecu_error = 0;
INT8U ecu_throttle = 0;
INT8U ecu_rpm = 0;
INT8U ecu_clt = 0;
INT8U ecu_mat = 0;
int ecu_map = 0;
float ecu_lambda = 0;
float ecu_volt = 0;
void setup() {
Serial.begin(57600);
delay(250);
CAN.begin(CAN_500KBPS);
delay(250);
attachInterrupt(0, MCP2515_ISR, FALLING);
delay(250);
}
// MAIN LOOP!
void loop() {
updateLCD();
delay(100);
}
// Interrupt Display Data
void updateLCD() {
// Copy data to Serial1
Serial.print("U: ");
Serial.println(ecu_volt);
Serial.print("CLT: ");
Serial.println(ecu_clt, DEC);
Serial.print("MAT: ");
Serial.println(ecu_mat, DEC);
Serial.println("---");
}
// Interrupt CAN Data Reseived
void MCP2515_ISR() {
CAN.readMsgBuf(&can_len, can_buf);
parseData(CAN.getCanId());
}
// Parse data from buffer
void parseData(int id) {
switch(id) {
case 0x770:
ecu_rpm = ((int)(word(can_buf[0],can_buf[1])));
break;
case 0x771:
ecu_clt = ((int)(word(can_buf[0],can_buf[1]))/10);
ecu_volt = ((float)(word(can_buf[4],can_buf[5]))*5/1024);
ecu_throttle = ((float)(word(can_buf[6],can_buf[7]))/10);
break;
case 0x772:
ecu_map = ((int)(word(can_buf[0],can_buf[1])));
ecu_mat = ((int)(word(can_buf[2],can_buf[3]))/10);
ecu_lambda = (int)(word(can_buf[6],can_buf[7]));
break;
case 0x773:
ecu_error = (int)(word(can_buf[4],can_buf[5]));
break;
}
}