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 resend the data back from my Mega it stops receiving data from the Uno correctly and doesn't even send the message.
Can I not send and receive messages from the same Arduino device and or is there something im missing?
Here is my code for the Arduino Uno and Mega Respectively
#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
}
#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
}
}
without seeing your code, it's hard to say what the issue could be.
kindly please post your code for the UNO and MEGA respectively and if possible a schematic for your connection between the 2 devices.
I just posted the code, I can upload a schematic in a minute
LOL... type too quicky!
the code you posted shows how message being sent baack from MEGA -> UNO
But if I attached another Mega with a shield onto the CAN H and CAN L ports of the Original Mega would it be able to receive the output. (I will attach a new schematic )
please see reply already posted for same issue here:
The response was that D2 and D3 were used as interrupt pins to record the output of the encoder as it changes
I'll spell it out then since you seem to have failed to see the point being made once more!
the sheild and your encoder are BOTH uisng pin2.
the sheild uses pin 2 to notify that it has received CAN messages.
I am sorry, I can't believe that went right over my head!
Thank you for the help but I am still running into issues, I have added an Arduino MEGA with shield
This is the current set up with wires connecting the CAN H and CAN L ports of each device to one another while the code for each device is as follows
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 Recieve and Sender
#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 Reciever
#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
}
}
Here I am attempting to send a message from Uno to Mega, where the Mega then sends it to the other Mega
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.