Hello everyone,
I am using two Arduino Uno Boards and a sparkfun CAN Shield and I am recieving signals from external Hardware over CAN Bus to one Arduino board and these signals are then send to other Arduino Board unsing I2C Communication.
But Sometimes CAN Signals are getting skipped and sometimes they are not. And when wire transmission is not involved then it’s working perfectly fine.
Mostly Message id: 0x3 is getting skipped.
Can anyone please help me with this issue as I need to use wire transmission?
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
#include <stdlib.h>
#include <math.h>
#include <Wire.h>
byte Bot_1[8];
byte Bot_2[8];
byte Bot_3[8];
byte Bot_4[8];
//********************************Setup Loop*********************************//
void setup() {
Wire.begin();
Serial.begin(9600); // For debug use
Serial.println("CAN Read - Testing receival of CAN Bus message");
if (Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
Serial.println("CAN Init ok");
else
Serial.println("Can't init CAN");
}
//********************************Main Loop*********************************//
void loop()
{
tCAN message;
if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
if (message.id == 0x2) //Botschaft 1
{
Bot_1[0]=message.data[0];
Bot_1[1]=message.data[1];
Bot_1[2]=message.data[2];
Bot_1[3]=message.data[3];
Bot_1[4]=message.data[4];
Bot_1[5]=message.data[5];
Bot_1[6]=message.data[6];
Bot_1[7]=message.data[7];
Serial.println("Bot_1");
}
if (message.id == 0x1) //Botschaft 2
{
Bot_2[0]=message.data[0];
Bot_2[1]=message.data[1];
Bot_2[2]=message.data[2];
Bot_2[3]=message.data[3];
Bot_2[4]=message.data[4];
Bot_2[5]=message.data[5];
Bot_2[6]=message.data[6];
Bot_2[7]=message.data[7];
Serial.println("Bot_2");
}
if (message.id == 0x0) //Botschaft 3
{
Bot_3[0]=message.data[0];
Bot_3[1]=message.data[1];
Bot_3[2]=message.data[2];
Bot_3[3]=message.data[3];
Bot_3[4]=message.data[4];
Bot_3[5]=message.data[5];
Bot_3[6]=message.data[6];
Bot_3[7]=message.data[7];
Serial.println("Bot_3");
}
if (message.id == 0x3) //Botschaft 4
{
Bot_4[0]=message.data[0];
Bot_4[1]=message.data[1];
Bot_4[2]=message.data[2];
Bot_4[3]=message.data[3];
Bot_4[4]=message.data[4];
Bot_4[5]=message.data[5];
Bot_4[6]=message.data[6];
Bot_4[7]=message.data[7];
Serial.println("Bot_4");
}
Wire.beginTransmission(8); // transmit to device #8
Wire.write(Bot_1,8);
Wire.write(Bot_2,8);
Wire.write(Bot_3,8);
Wire.write(Bot_4,8);
Wire.endTransmission(); // stop transmitting
}
}
}