0% found this document useful (0 votes)
20 views5 pages

RLI TPs

The document contains code for an Arduino project that uses a CAN bus shield to communicate sensor data and switch states between multiple Arduino boards. It includes code for an emitter board that sends temperature, light, and switch status data, and receiver boards that display the received data on an LCD screen. The emitter codes send messages with IDs like 0x04, 0x00, 0x1E, 0x316, and the receiver codes initialize filters to receive messages from specific IDs like 0x316, 0x317.

Uploaded by

Mohamed TalhiOui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

RLI TPs

The document contains code for an Arduino project that uses a CAN bus shield to communicate sensor data and switch states between multiple Arduino boards. It includes code for an emitter board that sends temperature, light, and switch status data, and receiver boards that display the received data on an LCD screen. The emitter codes send messages with IDs like 0x04, 0x00, 0x1E, 0x316, and the receiver codes initialize filters to receive messages from specific IDs like 0x316, 0x317.

Uploaded by

Mohamed TalhiOui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TP1 BP EMETTEUR

#include <mcp_can.h>
#include<SPI.h>

const int SPI_CS_PIN = 53;

MCP_CAN CAN(SPI_CS_PIN);

unsigned char stmp[8] ={0,1,2,3,5,6,7,8};


unsigned char stmp1[8] ={1,1,2,3,5,6,7,8}; /// creation d'un tableau

void setup()
{
Serial.begin(115200);
pinMode(9 ,INPUT);

while(CAN_OK != CAN.begin(CAN_500KBPS))
{
Serial.println("CAN BUS Shield init fail");
Serial.println("init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");
}

void loop()
{
int Etat=digitalRead(9);
if(Etat == 1){
//send data: id = 0x00, standard frame ,data len=8,stmp:data buf
CAN.sendMsgBuf(0x04,0,1,stmp);
delay(100); //send data per 100ms
Serial.println(" CAN BUS sendMsgBuf ok!");
}
else {
//send data: id = 0x00, standard frame ,data len=8,stmp:data buf
CAN.sendMsgBuf(0x00,0,1,stmp1);
delay(100); //send data per 100ms
Serial.println(" CAN BUS sendMsgBuf ok!");
}

}
TP2 LM35 LDR EMETEUR
#include <SPI.h>
#include <mcp_can.h>

const int SPI_CS_PIN= 53;


MCP_CAN CAN(SPI_CS_PIN);
int LM35 = A1, LDR = A4;
int Nlm35,Val2,NLDR,Val1,LUMH,LUMl;
unsigned char stmp[8]={0,0,0,0,0,0,0,0};

void setup() {
pinMode(LM35,INPUT);
pinMode(LDR,INPUT);
Serial.begin(115200);
START_INIT:
while(CAN_OK != CAN.begin(CAN_500KBPS)){
Serial.println("CAN BUS Shield init fail,Retry");
delay(100);
goto START_INIT;
}
Serial.println("CAN BUS Shield init ok!,go");
}

void loop() {
Nlm35 = analogRead(LM35);
Val1 = Nlm35*0.488 ;
NLDR = analogRead(LDR);
Val2 = NLDR ;
LUMH=Val2/256;
LUMl=Val2%256;
stmp[0]=Val1;
stmp[1]=LUMH;
stmp[2]=LUMl;
CAN.sendMsgBuf(0x1E,0,3,stmp);
Serial.println("La valeur du capteur LM35 est: ");
Serial.println(Val1);
Serial.println("La valeur du capteur LDR est: ");
Serial.println(Val2);
delay(1000);
Serial.println("message envoyée");
}
TP3 2 BP EMMETEUR
#include <SPI.h>
#include "mcp_can.h"

const int SPI_CS_PIN = 53;


MCP_CAN CAN(SPI_CS_PIN);

int BP1 = 2, Etat_BP1;


int BP2 = 4, Etat_BP2;

unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};

void setup()
{
Serial.begin(115200);
pinMode(2, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
START_INIT:
while (CAN_OK != CAN.begin(CAN_500KBPS))
{
Serial.println("CAN BUS Shield init fail,Retry");
delay(100);
goto START_INIT;
}
Serial.println("CAN BUS Shield init ok!,go");
}
void loop()
{
Etat_BP1 = digitalRead(BP1);
Etat_BP2 = digitalRead(BP2);
stmp[0] = Etat_BP1;
CAN.sendMsgBuf(0x316, 0, 1, stmp);
delay(2000);
Serial.println("CAN BUS sendMsg 0x316 ok!");
CAN.sendMsgBuf(0x317, 0, 1, stmp);
delay(2000);
Serial.println("CAN BUS sendMsg 0x317 ok!");
stmp[0] = Etat_BP2;
CAN.sendMsgBuf(0x318, 0, 1, stmp);
delay(2000);
Serial.println("CAN BUS sendMsg 0x318 ok!");
}
TP2 RECEIVE
#include <mcp_can.h>
#include <SPI.h>
#include <LiquidCrystal_I2C.h>
int lum;
LiquidCrystal_I2C lcd(0x27,20,4);
const int SPI_CS_PIN =53;
MCP_CAN CAN(SPI_CS_PIN);
unsigned char stmp[8]= {0,0,0,0,0,0,0,0};
void setup(){
lcd.init();
lcd.backlight();
Serial.begin(115200);
START_INIT:
while(CAN_OK!=CAN.begin(CAN_500KBPS)){
Serial.println("CAN BUS Shield inti fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT; }
Serial.println("CAN BUS Shield init OK!!!!!"); }
void loop(){
unsigned char len=2;
unsigned char buf[8];
lum =buf[1]*256+buf[2];
if (CAN_MSGAVAIL==CAN.checkReceive()) {
CAN.readMsgBuf(&len,buf);
unsigned long canId = CAN.getCanId();
Serial.println(".................................");
Serial.println("Get data from ID:0x");
Serial.println(canId,HEX);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature:");
lcd.setCursor(13, 0);
lcd.print(buf[0]);
lcd.setCursor(0, 1);
lcd.print("Lumiere:");
lcd.setCursor(11, 1);
lcd.print(lum);
Serial.println("La valeur du capteur LM35 est: ");
Serial.println(buf[0]);
delay(100);
Serial.println("La valeur du capteur LDR est: ");
Serial.println(lum);
delay(100);
}
}
TP3 RECEIVE
#include <mcp_can.h>
#include <SPI.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
const int SPI_CS_PIN =53;
MCP_CAN CAN(SPI_CS_PIN);
unsigned char len = 0;
unsigned char buf[8];
void setup() {
Serial.begin(115200);
lcd.init();
lcd.init();
lcd.backlight();
lcd.print("TEST BUS CAN");
delay(3000);
lcd.clear();
START_INIT:
while (CAN_OK != CAN.begin(CAN_500KBPS)) {
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
goto START_INIT; }
Serial.println("CAN BUS Shield init ok!");
CAN.init_Mask(0, 0, 0x7FF);
CAN.init_Mask(1, 0, 0x7FF);
CAN.init_Filt(0, 0, 0x318);
CAN.init_Filt(1, 0, 0x317);
CAN.init_Filt(2, 0, 0x316);
CAN.init_Filt(3, 0, 0x317);
CAN.init_Filt(4, 0, 0x316);
CAN.init_Filt(5, 0, 0x317);}
void loop() {
if (CAN_MSGAVAIL == CAN.checkReceive()) {
CAN.readMsgBuf(&len, buf);
unsigned long canId = CAN.getCanId();
Serial.print("Get data from ID: 0x");
Serial.println(canId, HEX);
lcd.setCursor(0,0);
lcd.print("ID : ");
lcd.setCursor(5,0);
lcd.print(canId);
lcd.setCursor(0,1);
lcd.print("Etat BP : ");
lcd.setCursor(11,1);
lcd.print(buf[0]);
Serial.println(); }}

You might also like