hey~ I got a canbus shield finally ~ and I had modified you code, just a little~ and it works~
[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~