Hi i have project about car reverse

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);
}


What does it do?

Same cut-and-paste laziness

1 Like

i copied how to calcul distance for the hy-srf05 code without mcp2515 in the void loop and i add what mcp2515 need . because i dont find any working code on the net

But what does the code do?

1 Like

987 contains 10 bits and you are putting it into a storage location that can only hold 8 bits.

Also check the return code from sendMessage to see if the message was successfully transmitted. If you are using a CAN bus analyzer then make sure it it configured to set the ACK bit.

I get the feeling he doe snot know what the code does.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.