Hi, still new to Arduino so excuse if doing something stupid.
But getting can messages different to what Im sending.
Have managed to setup a UNO with,
LCD screen,
Multiple buttons working on same interrupt
Multiple led etc
All working fine and doing what I expect based on events and a timer.
Issue I have is with CAN messages.
Have this setup shown with these link using a MCP2515 Can module connected
I use the sample code and could see things happening
So was happy that wired up correctly
But then I noticed on serial monitor what see what was being sent.
If I send 0, 1, 2, 3, 4, 5, 6, 7 i get back
0, 0, 8, 16, 64, 129, 3, 8
If I send 1, 2, 3, 4, 5, 6, 7, 8 i get back
0, 8, 16, 64, 129, 3, 8,16
If I send 1, 2, 3, 4, 5, 6, 7, 8 i get back
4, 16, 48, 64, 255, 2,134,8
Message seem to go out when they should
Can see message come in when expect
Just seem to be values that not the same as what sent
So is this a hardware fault, have I forgot to terminate the ends or some step extra to do?
I did notice that my boards not the same as the ones from image.
So guessing that are a clone, so could that be an issue ?
Fond the issue: Was hardware, was lose GND or POWER wire on one of the boards.
Plus checked the terminator resistors on the can lines that where bridged.
This is the code used to send and receive from each side.
#include <SPI.h>
#include "mcp_can.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN);
void setup() {
SERIAL.begin(115200);
while (CAN_OK != CAN.begin(CAN_500KBPS))
{
SERIAL.println("CAN BUS Shield init fail");
delay(100);
}
SERIAL.println("CAN BUS Shield init ok!");
}
int sendCtr = 0;
void loop()
{
CheckMessage();
sendCtr++;
if( sendCtr >= 150 )
{
sendCtr = 0;
//- 150 x 10ms delay in loop means 1.5 sec so send new message
SendMessage();
}
delay(10);
}
void CheckMessage()
{
unsigned char len = 0;
unsigned char buf[8];
unsigned long rxId = 0;
char msgString[128];
if (CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);
//- Get id of message
rxId = CAN.getCanId();
//- Time stamp message
sprintf(msgString, "[@%02dms] ", millis());
SERIAL.print(msgString);
// Determine if ID is standard (11 bits) or extended (29 bits)
if((rxId & 0x80000000) == 0x80000000)
{
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d ", (rxId & 0x1FFFFFFF), len);
}
else
{
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d ", rxId, len);
}
SERIAL.print(msgString);
if((rxId & 0x40000000) == 0x40000000)
{
sprintf(msgString, "Remote ID: 0x%.8lX ", (rxId - 0x40000000));
}
// print the data
SERIAL.print("Data:");
for (int i = 0; i < len; i++)
{
SERIAL.print(buf[i], HEX);
SERIAL.print("\t");
}
SERIAL.println();
}
}
unsigned char sendBuffer[8] = {0, 1, 2, 3, 4, 5, 6,7};
void SendMessage()
{
byte status = CAN.sendMsgBuf(0x00, 0, 8, sendBuffer);
if( status != CAN_OK)
{
SERIAL.println("Failed to send");
}
else
{
SERIAL.println("Sent message ok");
}
}