Hello everyone, I have been working with this project for some days and this hasn't occurred to me since now.
So the thing is I am using 2 MCP2515 CAN Module to send data from one Arduino to the other (see the Schematic bellow/next post).
I am just using the examples provided by the library (which the one I use is this: GitHub - Seeed-Studio/Seeed_Arduino_CAN: Seeed Arduino CAN-BUS library - MCP2518FD&MCP2515&MCP2551) where a simple message is sent and received. The problem comes when I open the Serial monitor of the receiver Arduino. It is initialized good but the moment he tries to detect the CAN ID I declared at the sender code it just fails and automatically detects the CAN ID: 1FFFFFF which is a vector full of 255s... (see the picture below/next post)
Can some1 explain to me why is the receiver not detecting the ID that I declare (in this case 0x70) and how can I fix it?
SENDER CODE:
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;
const int ledHIGH = 1;
const int ledLOW = 0;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println(" Init CAN BUS Shield again");
delay(100);
}
SERIAL.println("CAN BUS Shield init ok!");
}
unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7};
void loop()
{ SERIAL.println("In loop");
// send data: id = 0x70, standard frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x70, 0, 8, stmp);
delay(1000); // send data once per second
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
RECEIVER CODE:
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;
const int LED = 8;
boolean ledON = 1;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
SERIAL.begin(115200);
pinMode(LED,OUTPUT);
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
SERIAL.println("CAN BUS Shield init fail");
SERIAL.println("Init CAN BUS Shield again");
delay(100);
}
SERIAL.println("CAN BUS Shield init ok!");
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned long canId = CAN.getCanId();
SERIAL.println("-----------------------------");
SERIAL.println("get data from ID: 0x");
SERIAL.println(canId, HEX);
for(int i = 0; i<len; i++) // print the data
{
SERIAL.print(buf[i]);
SERIAL.print("\t");
if(ledON && i==0)
{
digitalWrite(LED, buf[i]);
ledON = 0;
delay(500);
}
else if((!(ledON)) && i==4)
{
digitalWrite(LED, buf[i]);
ledON = 1;
}
}
SERIAL.println();
}
}
//END FILE

