SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By markd
#191423
I'm using the SPARKFUN CAN BUS shield with an arduino Uno and a sketch that I got from Instructables (https://www.instructables.com/id/CAN-Bu ... h-Arduino/) The sketch seems to work fine as long as I don't try and filter the data, when I uncomment the filter I get no data at all. The Libraries I'm using are the ones from the link to sparkfun on the Instructables page, so I think the libraries should be Ok.

#include <Canbus.h> // don't forget to include these
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

void setup()
{
Serial.begin(9600);
//Initialise MCP2515 CAN controller at the specified speed
if(Canbus.init(CANSPEED_500))
Serial.println("CAN Init ok");
else
Serial.println("Can't Init CAN");

delay(1000);
}
void loop()
{
tCAN message;

if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
if(message.id == 0x631 and message data[2] == 0xFF) //filtering based on CAN bus message ID. //I have tried filtering on just the message ID, but that doesn't help either
{
Serial.print("ID: ");
Serial.print(message.id,HEX);
Serial.print(", ");
Serial.print("Data: ");
for(int i=0;i<message.header.length;i++)
{
Serial.print(message.data,HEX);
Serial.print(" ");
}
Serial.println("");
}}}

}