Extracting one CAN bus message

I've looked all over the place for CAN bus info and especially on extracting one message and using it as an output to power an LED. Can't really find anything on it. I'm not a complete newbie when comes to Arduino but anything CAN bus communication I slow way down and get confused pretty easily, I'm trying to get a grip on this. I can get my Arduino and CAN bus shield to communicate and show the values needed on my serial monitor. I'm just confused on extracting the specific info ex (headlights on = LED on). I'm using CAN-BUS shield library from seeedstudios: example "receive- monitor". If anyone has info or advice on a better way of communicating with CAN bus

ok... not much to go with so this is kind of a generic answer.

say you have a CAN message 8 bytes long with the following message structure with regards to the 'signals' defined in it:

now, depending on which signal you are after you need to then 'grab' the bits/bytes for that signal before you can read it.

for example, in the above CAN message map, if you want to read the value of 'SIG2', since it is only one bit long you can simply do bitRead(buf[6],4), where 'buf' is the array that contains you CAN message data.

hope that helps...

thank you, this helped out. I understand extracting the one piece of data now. I can see the info my car is sending me. How to I go about asking my Arduino to ignore all but one arbId/CanId and it's data?

have a look at this previous post of mine?

hope that helps...

this info has gotten me farther. What I'm confused about is the info on the left is using 11bits where on the right is a different layout. would I need to convert the "0x010F0000" on the right into binary? Also the "(0,0,...)" part of the code the first 0 represents the byte and second 0 represents how many bits. Am I correct in this?

If you have the library for your canbus shield there are examples showing the structure of a message and how to decode it .

If you google you find a list of canbus instructions , if you pick the one you want , you will only get a single response .

Something like headlights on may not be easily accessible and maybe on a different bus

So instead of headlights, I'm going to do "brake pedal switch" for the sake of ease. I found the ID where its stored and the value that changes to indicate that the pedal is pressed or not. how do I put when ID: 512 byte 4 has a value of 80 LED= on.
LED part isn't the issue. I just want my shield to recognize ID, byte, and bit.

Thank you for responding

something like this maybe:

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

#define LED 13 //LED PIN
#define Brake_Pedal 80 // value of Brake Pedal to look for

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];

MCP_CAN CAN0(10);                          // Set CS to pin 10

void setup()
{
  Serial.begin(115200);
  if (CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n");
  else Serial.print("MCP2515 Init Failed!!\r\n");
  pinMode(2, INPUT);                       // Setting pin 2 for /INT input
  pinMode(LED, OUTPUT);

  //setup filters to receive only CAN ID 0x512
  CAN0.init_Mask(0, 0, 0x07FF0000);              // Init first mask...
  CAN0.init_Filt(0, 0, 0x05120000);              // Init first filter...
  CAN0.init_Filt(1, 0, 0x05120000);              // Init second filter...

  CAN0.init_Mask(1, 0, 0x07FF0000);              // Init second mask...
  CAN0.init_Filt(2, 0, 0x05120000);              // Init third filter...
  CAN0.init_Filt(3, 0, 0x05120000);              // Init fouth filter...
  CAN0.init_Filt(4, 0, 0x05120000);              // Init fifth filter...
  CAN0.init_Filt(5, 0, 0x05120000);              // Init sixth filter...

  CAN0.setMode(MCP_NORMAL);                // Change to normal mode to allow messages to be transmitted
}

void loop()
{
  if (!digitalRead(2))                   // If pin 2 is low, read receive buffer
  {
    CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
    Serial.print("ID: ");
    Serial.print(rxId, HEX);
    Serial.print(" Data: ");
    for (int i = 0; i < len; i++)        // Print each byte of the data
    {
      if (rxBuf[i] < 0x10)               // If data byte is less than 0x10, add a leading zero
      {
        Serial.print("0");
      }
      Serial.print(rxBuf[i], HEX);
      Serial.print(" ");
    }
    Serial.println();

    //compare byte 4 of rx bufwith desired Brake pedal value (NOT SURE if by byte 4 you meet 4 or 3! using 3 here...
    if ((rxBuf[3] && Brake_Pedal)==Brake_Pedal) {
      digitalWrite(LED, HIGH); //LED ON
    }
    else {
      digitalWrite(LED, LOW); //LED OFF
    }
  }
}

hope that helps...

Man you are a beast! lol Thank you, I will try this sketch when I get a chance. If this works I was on the the right track when I was grinding away last night. Just got a bunch of reading material on CAN bus and communicating with Arduino.

I used your sketch and with a couple tweaks I can get it the CAN bus shield to initialize, baud rate and mask and filters good to go, but I'm not getting any messages from the car

sketch_aug05a.ino (2.1 KB)

erm... not use, are you sure CAN ID 0x512 is available on the CAN BUS?

Yeah, I'm positive it's available. I've been able to monitor its activity in relation to the brake pedal. I've been trying to find away to get the message structure of 0x512 but not sure how to ask for it. that way I can use the info you gave me on your first post.

Thanks for sticking with me on this btw

so I got CAN ID 0x512 reading and I don't have the CAN message structure down but I have what the car is sending out. At the bottom I tried rxbuf[3] and [4] and cycling throughout the rest of (0-7). No LED is lighting up. I've tested the LED and know its good. I know I'm right there in figuring this out. Attached my sketch, if anyone wants to look over and see if I made a mistake somewhere.

Standard_MaskFilter_for_brake_pedal.ino (4.2 KB)

Screenshot (10)|690x373

that's because '80' as you specified was the HEX value not decimal as I believed it to be.

so

#define Brake_Pedal 80

in the code I posted should actually be

#define Brake_Pedal 0x80

(and rxbuf[4] should be the correct byte to use! :wink: )

hope that helps...

So I tried switching the brake pedal from 80 to 0x80, I even tried converting it into DEC and it wasn't working. I also tried changing rxbuf[3] to [4]. Did a bit of trial and error with (bitRead(rxbuf[0-7], 0-7) going through the message structure again and still nothing. I'm lost now. ill attach my sketch and if anyone can check it out and see if anything looks out of place.

Standard_MaskFilter.ino (4.2 KB)

After combing over everything, and making some adjustments, I got everything to work as I wanted it to. Thank you for helping me out with this. Now on to more complex projects!