hi i have project about car reverse and i need to do that with sensor hy-srf05 two can bus module mcp2515 and two arduino uno card but this emission code does not work
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication
struct can_frame canMsg1; //pot message
MCP2515 mcp2515(10); // port CLK carte Arduino UNO
const int TRIG_PIN = 13;
const int ECHO_PIN = 12;
void setup()
{
pinMode(TRIG_PIN,OUTPUT);
pinMode(ECHO_PIN,INPUT);
Serial.begin(9600);
SPI.begin(); //Begins SPI communication
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode();
canMsg1.can_id = 0xAA; //Id CAN : 0xAA
canMsg1.can_dlc = 2; //longueur données CAN : 2
}
void loop()
{
long duration, distanceCm, distanceIn;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN,HIGH);
// convert the time into a distance
distanceCm = duration / 29.1 / 2 ;
if (distanceCm <= 0)
{
Serial.println("Out of range");
}
else
{
Serial.print(distanceCm);
Serial.print("cm");
Serial.println();
}
int y = digitalRead(distanceCm);
Serial.print(canMsg1.can_id);
canMsg1.data[0] = y; //Valeur envoyée sur data[0]
canMsg1.data[1]= 987; // Valeur envoyée sur data [1]
mcp2515.sendMessage(&canMsg1); //Sends the CAN message
delay(200);
}