Hi I am a newer user to Arduino and I have been trying to utilize some seeed studio CAN-BUS V2 shields to send and receive data over. I have been running into an issue where when I send the data from my Arduino Uno to my Arduino Mega which works fine, but when I try to send the data to another Mega it doesn’t work.
Can I not send and receive messages from the same Arduino device and or is there something im missing?
Here is my code
Uno
#include <SPI.h> //SPI is used to talk to the CAN Controller
#include <mcp_can.h>
#include <Encoder.h>
Encoder myEnc(2, 3); // connect encoder on digital Pin 2 and 3
MCP_CAN CAN(9); //set SPI Chip Select to pin 10
int encoderPin1 = 2;
int encoderPin2 = 3;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
volatile long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
byte data[4] = {225,3,0,0};
byte data_NMT[2] = {1,1};
long newPosition;
void setup()
{
Serial.begin(115200); //to communicate with Serial monitor
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
attachInterrupt(digitalPinToInterrupt(2), updateEncoder, CHANGE); //interrupts the pin based on a change noticed in the pin and calls a function
attachInterrupt(digitalPinToInterrupt(3), updateEncoder, CHANGE);
START_INIT:
if(CAN_OK == CAN.begin(CAN_1000KBPS)) //setting CAN baud rate to 1000Kbps but actually its going to be 500KBPS because CAN module has 8MHz crystal not 16MHz
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
//loading the data bytes of the message. Up to 8 bytes
void loop()
{
newPosition = encoderValue*.1066;
Serial.println(newPosition);
CAN.sendMsgBuf(27, 0, 1 ,(byte *) &newPosition );
delay(100);
}
void updateEncoder()
{
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //combining it with the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++; //clockwise
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --; //counter clockwise
lastEncoded = encoded; //store this value for next time
}
Mega 1
#include <SPI.h>
#include <mcp_can.h>
MCP_CAN CAN(9); //set SPI Chip Select to pin 10
unsigned char len = 0;
unsigned char buf[8];
unsigned int canID;
int angle;
int Newangle;
int pos;
int Newpos;
int current;
int Newcurrent;
void setup()
{
Serial.begin(115200); //to communicate with Serial monitor
START_INIT:
if(CAN_OK == CAN.begin(CAN_1000KBPS)) //setting CAN baud rate to 1000Kbps but actually its going to be 500KBPS because CAN module has 8MHz crystal not 16MHz
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
void loop()
{
if(CAN_MSGAVAIL == CAN.checkReceive()) //check if data is coming
{
CAN.readMsgBuf(&len, buf);
canID = CAN.getCanId();
//Serial.println(canID);
if(canID == 27)
{
//Serial.println(buf[0]);
angle = buf[0]*1.406;
Serial.println(angle);
Newangle = angle*.1066;
//CAN.sendMsgBuf(30, 0, 1 ,(byte *) &Newangle );
}
if(canID == 28)
{
pos = buf[0];
Serial.println(pos);
Newpos = pos;
//CAN.sendMsgBuf(31, 0, 1 ,(byte *) &Newpos );
}
if(canID == 29)
{
current = buf[0];
Serial.println(current);
Newcurrent = current;
//CAN.sendMsgBuf(32, 0, 1 ,(byte *) &Newcurrent );
}
//CAN.readMsgBuf(&len, buf); //read data, len: data length, buf: data buffe
//Serial.println(buf[0]*1.406); //Serial.write prints the character itsel
}
}
Mega 2
#include <SPI.h>
#include <mcp_can.h>
MCP_CAN CAN(9); //set SPI Chip Select to pin 10
unsigned char len = 0;
unsigned char buf[8];
unsigned int canID;
int angle;
int Newangle;
int pos;
int Newpos;
int current;
int Newcurrent;
void setup()
{
Serial.begin(115200); //to communicate with Serial monitor
START_INIT:
if(CAN_OK == CAN.begin(CAN_1000KBPS)) //setting CAN baud rate to 1000Kbps but actually its going to be 500KBPS because CAN module has 8MHz crystal not 16MHz
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
void loop()
{
if(CAN_MSGAVAIL == CAN.checkReceive()) //check if data is coming
{
CAN.readMsgBuf(&len, buf);
canID = CAN.getCanId();
if(canID == 30)
{
angle = buf[0]*1.406;
Serial.println(angle);
}
if(canID == 31)
{
pos = buf[0];
Serial.println(pos);
}
if(canID == 32)
{
current = buf[0];
Serial.println(current);
}
//CAN.readMsgBuf(&len, buf); //read data, len: data length, buf: data buffe
//Serial.println(buf[0]*1.406); //Serial.write prints the character itself
}
}
When I have the Sendmsg. commented out in the Mega 1 Code everything runs fine, but when I try to run it with the send message uncommented it no longer works