Programming multiple NODE for CAN BUS SHEILD ?

Hi all, i have been trying to get the CAN bus recieve code to work from the wiki site,

And it’s work , unfortunately in wiki , just for 2 Node… :cry:

Now , I try to communicate using 3 Can-bus shield , 1 for master , others for slave.

(Master for reading all input that given by slave like RPM, Voltage) :bulb:

I’m using Arduino R3 and CAN BUS SHIELD from Seeedstudio ,

Has anyone ever tried to do it before :question: :question:

Any comments will be appreciate,

Thanks

Gerry

hi Gerry, do you want one CANBUS send data and the other two receive ?
if so, you can set the receive mask to make it works~

Hi loovee ,

Great thanks ,you have answered me ,:slight_smile:

yes ,but I mean one CAN BUS for receive and the other send data.

btw could you make simple tutorial how to do use masking and filtering ?

I’m newbie learning in CAN ,

Thanks

hi Gerry, you can use INT8U init_Mask(INT8U num, INT8U ext, INT32U ulData) to config the mask and init_Filt(INT8U num, INT8U ext, INT32U ulData) to config the filt.

as i know ~ one can bus for receive and many for send data is ok~ do you meet any problem ? :smiley:

I try using Masking and reading output data using serial monitor in extended mode…

sorry , I edited the code,but still no data output in serial monitor
Please correct me if there are any mistakes

CAN RX modul
Enter setting mode success
set rate success!!
Enter Normal Mode Success!!
Begin to set Mask!!
set Mask success!!
Begin to set Filter!!
set Filter success!!


//RECEIVE CODE//
#include "mcp_can.h"
#include <SPI.h>
//#include <stdio.h>
#define INT8U unsigned char

//boolean firstTime = false;
INT8U Flag_Recv = 0;
//INT8U len = 0;
INT8U ID1 = 0x700;
//INT8U ID2 = 0;
INT8U buf[8];
//const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
//int buttonState = 0;   
//char str[20];
void setup()
{
  Serial.begin(115200);
  Serial.println("CAN RX modul ");
  CAN.begin(CAN_500KBPS);
  CAN.init_Mask(0,1, 0);         /* init Masks                   */
  CAN.init_Filt(0, 1, 0);        /* init filters                    */
  attachInterrupt(0, MCP2515_ISR, FALLING); // digital pin 2
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
 
}

void MCP2515_ISR()
{
  Flag_Recv = 1;
}

void loop()
{
  if(Flag_Recv)
  {

    Flag_Recv = 0;
    //CAN.readMsgBuf(&len, buf);
     
    ID1=CAN.getCanId();
    ID1=CAN.checkReceive();
    Serial.print("Ready");
    CAN.readMsgBuf(&ID1, buf);
    
    if (buf[0]==0){
    // turn LED on:     
    digitalWrite(ledPin, HIGH);
    Serial.print("LED ON ");  
    } 
    else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
    Serial.print("LED OFF "); 
      
    }   
  
  }
}

// SENDING CODE //

#include "mcp_can.h"
#include <SPI.h>
INT8U ID1=0x700;
INT8U buf[8];
const int buttonPin = 4; 
// variables will change:
int buttonState = 0; 
//INT32U ID2=0x7e8;
void setup()
{
  Serial.begin(115200);
  if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print("can init ok!!\r\n");
  else Serial.print("Can init fail!!\r\n");
  pinMode(buttonPin, INPUT); 
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  ID1=CAN.getCanId();
     if (digitalRead (buttonPin) == LOW)
     {
       buf[0] = 0;
       CAN.sendMsgBuf(ID1,1,8,buf);
       Serial.println ("Sent 0");
     }
     else
     {
        buf[0] = 1;
        CAN.sendMsgBuf(ID1,1,8,buf);
        Serial.println ("sent one");
     }
     delay (10);   


}

seems right!! I’ll try next Monday~ I don’t have a canbus shield by hand! :confused:

still no luck… :frowning: :frowning:

init successfull but no data receive…

hey~ I got a canbus shield finally ~ and I had modified you code, just a little~ and it works~ :slight_smile:

[code]// SENDING CODE //
#include “mcp_can.h”
#include <SPI.h>

INT8U ID1=0x700;
INT8U buf[8]= {0, 0, 0, 0, 0, 0, 0, 0}; // you should init it first
const int buttonPin = 4;

int buttonState = 0;

void setup()
{
Serial.begin(115200);
if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print(“can init ok!!\r\n”);
else Serial.print(“Can init fail!!\r\n”);
pinMode(buttonPin, INPUT);
}

void loop()
{
buttonState = digitalRead(buttonPin);
//ID1=CAN.getCanId(); when you get some data from can bus, you can use this function to get who send this

if (digitalRead (buttonPin) == LOW)
{
    buf[0] = 0;
    CAN.sendMsgBuf(ID1,1,8,buf);
    Serial.println ("Sent 0");
}
else
{
    buf[0] = 1;
    CAN.sendMsgBuf(ID1,1,8,buf);
    Serial.println ("sent one");
}

delay (10);

}
[/code]

[code]//RECEIVE CODE//
#include “mcp_can.h”
#include <SPI.h>
//#include <stdio.h>
#define INT8U unsigned char

//boolean firstTime = false;
INT8U Flag_Recv = 0;
//INT8U len = 0;
INT8U ID1 = 0x700;
//INT8U ID2 = 0;
INT8U buf[8];
//const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
//int buttonState = 0;
//char str[20];
void setup()
{
Serial.begin(115200);
Serial.println("CAN RX modul ");
CAN.begin(CAN_500KBPS);
CAN.init_Mask(0,1, 0); /* init Masks /
CAN.init_Filt(0, 1, 0); /
init filters */
attachInterrupt(0, MCP2515_ISR, FALLING); // digital pin 2
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);

}

void MCP2515_ISR()
{
Flag_Recv = 1;
}

void loop()
{
if(Flag_Recv)
{

    Flag_Recv = 0;
    ID1=CAN.getCanId();
    Serial.print("Ready");
    CAN.readMsgBuf(&ID1, buf);

    if (buf[0]==0){
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON ");
    }
    else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF ");

    }

}

}
[/code]

you can try it~ any problem please feel free to contract me!
cheers~ :laughing:

Actually , your code works fine also~ do you download the latest library?
image111.jpg

Sorry for the late reply…

Cool , it’s working …thanks Loovee…

INT8U buf[8]= {0, 0, 0, 0, 0, 0, 0, 0};         // you should init it first

Could you explain to me why must be {0, 0, 0, 0, 0, 0, 0, 0} ?

you can see here,

you use buf, but only initialize the first element, it may cause some problem~ :smiley:

hey~ it my pleasure~ :slight_smile:

I will post my project using CAN bus ,if it’s done…

Once again thanks Loovee …
:smiley:

wow~ I am looking forward to that~ I must be very funny~

anyway~ if you have any questions~please fell free to contract me~

Hi Loovee,

I am trying to trigger an action on the receiver side, but I cannot even get an LED to blink. I copied your receiver source code. Does it blink the LED for you? In my case, LED on pin 13 (or any other) remains dark.

Do you have any ideas? Thanks!

Hauke

I am correcting my last post. I am addressing pin 8 and can toggle with a digitalWrite.

However, the voltage is only 1.5 V and the LED without a resistor only blinks faintly. Is the shield consuming so much power that there is only very little left?