je vous donne les codes pour que vous puissiez mieux comprendre
emetteur :
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 10;
const int BP = 3; // bp1 en broche 3
const int BP2 = 4; //bp2 en broche 4
const int BP3 = 5; //bp3 en broche 5
const int BP4 = 6; // bp4 en broche 6
const int BP5 = 7; // bp5 en broche 7
const int BP6 = 8; //bp6 en broche 8
const int BP7 = 9; // bp7 en broche 9
const int BP8 = 10; // bp8 en broche 10
const int BP9 = 11; // bp9 en broche 11
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
pinMode(BP, INPUT); //bp en entrée
pinMode(BP2, INPUT); //bp2 en entrée
pinMode(BP3, INPUT); //bp3 en entrée
pinMode(BP4, INPUT); //bp4 en entrée
pinMode(BP5, INPUT); //bp5 en entrée
pinMode(BP6, INPUT); //bp6 en entrée
pinMode(BP7, INPUT); //bp7 en entrée
START_INIT:
if (CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
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;
}
}
unsigned char bp1[8] = {0, 1, 2, 3, 4, 5, 6, 7};
unsigned char bp2[8] = {0, 0, 0, 0, 0, 0, 0, 1};
unsigned char bp3[8] = {1, 1, 1, 1, 1, 1, 1, 0};
unsigned char bp4[8] = {0, 2, 2, 2, 2, 2, 2, 2};
unsigned char bp5[8] = {0, 0, 2, 2, 2, 2, 2, 2};
unsigned char bp6[8] = {0, 0, 0, 2, 2, 2, 2, 2};
unsigned char bp7[8] = {0, 3, 3, 3, 3, 3, 3, 3};
void loop() {
static int testPrecedent1 = LOW;
static int testPrecedent2 = LOW;
static int testPrecedent3 = LOW;
static int testPrecedent4 = LOW;
static int testPrecedent5 = LOW;
static int testPrecedent6 = LOW;
static int testPrecedent7 = LOW;
static int testPrecedent8 = LOW;
static int testPrecedent9 = LOW;
int test1 = digitalRead(BP);//test est égal à l'etat du bp1
if (test1 == HIGH && testPrecedent1 == LOW)
CAN.sendMsgBuf(0x00, 0, 8, bp1);// envoyé données: id = 0x00, tram standars car 0, longueur de la tram = 8, bp1: contenu du message
testPrecedent1 = test1;
int test2 = digitalRead(BP2);//test est égal à l'etat du bp2
if (test2 == HIGH && testPrecedent2 == LOW)
CAN.sendMsgBuf(0x01, 0, 8, bp2);// envoyé données: id = 0x01, tram standars car 0, longueur de la tram = 8, bp2: contenu du message
testPrecedent2 = test2;
int test3 = digitalRead(BP3);//test est égal à l'etat du bp3
if (test3 == HIGH && testPrecedent3 == LOW)
CAN.sendMsgBuf(0x02, 0, 8, bp3);// envoyé données: id = 0x02, tram standars car 0, longueur de la tram = 8, bp3: contenu du message
testPrecedent3 = test3;
int test4 = digitalRead(BP4);//test est égal à l'etat du bp4
if (test4 == HIGH && testPrecedent4 == LOW)
CAN.sendMsgBuf(0x03, 0, 8, bp4);// envoyé données: id = 0x03, tram standars car 0, longueur de la tram = 8, bp4: contenu du message
testPrecedent4 = test4;
int test5 = digitalRead(BP5);//test est égal à l'etat du bp5
if (test5 == HIGH && testPrecedent5 == LOW)
CAN.sendMsgBuf(0x04, 0, 8, bp5);// envoyé données: id = 0x04, tram standars car 0, longueur de la tram = 8, bp5: contenu du message
testPrecedent5 = test5;
int test6 = digitalRead(BP6);//test est égal à l'etat du bp6
if (test6 == HIGH && testPrecedent6 == LOW)
CAN.sendMsgBuf(0x05, 0, 8, bp6);// envoyé données: id = 0x05, tram standars car 0, longueur de la tram = 8, bp6: contenu du message
testPrecedent6 = test6;
int test7 = digitalRead(BP7);//test est égal à l'etat du bp7
if (test7 == HIGH && testPrecedent7 == LOW)
CAN.sendMsgBuf(0x06, 0, 8, bp7);// envoyé données: id = 0x06, tram standars car 0, longueur de la tram = 8, bp7: contenu du message
testPrecedent7 = test7;
delay(20);
}
[/co