Can addressing issue

I am trying to address particular on the can network i have created but i am unable to talk to a specific device address. i recieve messages from 1-8 but its from all addresses how would i target a particular address???

sorry im a noob to this and im just piecing together peoples existing code im not sure where to go from here

#include <SPI.h>              //Library for using SPI Communication 
#include <mcp2515.h>          //Library for using CAN Communication (https://github.com/autowp/arduino-mcp2515/)
#include <LiquidCrystal_I2C.h>
 
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x3F for a 16 chars and 2 line display
 
struct can_frame canMsg;
 
MCP2515 mcp2515(10);                 // SPI CS Pin 10
 
 
void setup()
{
  Serial.begin(9600);                //Begins Serial Communication at 9600 baudrate
  SPI.begin();                       //Begins SPI communication
  lcd.init();
  lcd.clear();         
  lcd.backlight();      // Make sure backlight is on
  lcd.setCursor(0, 0);
  lcd.print("CANBUS TUTORIAL");
  delay(3000);
  lcd.clear();
 
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
  mcp2515.setNormalMode();  
  
  
  pinMode(4, OUTPUT);  
  pinMode(5, OUTPUT);     
  pinMode(6, OUTPUT);   
  pinMode(7, OUTPUT); 
      

   //Serial.print("humi");
}
 
void loop()
{
  
  



  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) // To receive data (Poll Read)
  {
   
    int x = canMsg.data[0];
    int y = canMsg.data[1];
    int z = canMsg.data[2];
    float a =  z *(5.0/256);
 
   // lcd.setCursor(0, 0);         //Display Temp & Humidity value received at 16x2 LCD
   // lcd.print("Humi: ");
    //lcd.print(x);
   // lcd.setCursor(0, 1);
    //lcd.print("Temp: ");
    //lcd.print(y);
    //delay(1000);
    //lcd.clear();

    Serial.print("  Humidity  ");
    Serial.print(x);
   
    Serial.print("%  ");

    
    Serial.print("  Temp  ");
    Serial.print((y * 9/5) + 32);
    Serial.print("F   ");

   

    //CAN.read(0x700)

    //Serial.print("Temp  ");
    Serial.print(a );
    Serial.println(" V");
    //delay (100);



if (a>= 2)  {
   digitalWrite(4, HIGH);
 
  // sets the digital pin 13 on
 }else {
  digitalWrite(4, LOW);
  }  // sets the digital pin 13 off
  

  if (a>= 2.5)  {
   digitalWrite(5, HIGH);
 
  // sets the digital pin 13 on
 }else {
  digitalWrite(5, LOW);
  }  // sets the digital pin 13 off
  
  if (a>= 3.5)  {
   digitalWrite(6, HIGH);
 
  // sets the digital pin 13 on
 }else {
  digitalWrite(6, LOW);
  }

if (x>= 9)  {
   digitalWrite(7, HIGH);
 
  // sets the digital pin 13 on
 }else {
  digitalWrite(7, LOW);
  }  // sets the dig

  }
}


What do You mean by reading 1 to 8? Are there 8 devices sending frames?
Can You someway post what the CAN frame looks like, what it contains? I don't have that library in use and don't want to download it to this equipment.

Hi,
What are your devices, if other microcontrollers can you please post their codes?

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Try matching on canMsg.can_id, or if that doesn't compile look at the definition of the can_frame struct to see if there is an address field.

1 Like

When a message is sent it contains an ID number.
like this one from the CAN_write example from your library:
canMsg.can_id = 0x0F6;
This number should be the chosen ID number of the receiver.

After receiving the message you can do this:

if(canMsg.can_id == 0x0F6 )
 {// This Msg is for this receiver so do stuff here}

Thanks Hutkikz that worked

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