Canbus filtering

hi

i have a problem with canbus filtering,

i have been writing a code for a dash display for an EV conversion using a donor car,
i have the canbus ids and what they do, and my code works so far but my next part is giving me some queries:

this is my code so far:

#include <SPI.h>
#include <mcp2515.h>
#include <MCUFRIEND_kbv.h>
struct can_frame canMsg;
MCP2515 mcp2515(10);
MCUFRIEND_kbv tft;

#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>
#include <FreeDefaultFonts.h>

#define BLACK       0x0000      /*   0,   0,   0 */
#define NAVY        0x000F      /*   0,   0, 128 */
#define DARKGREEN   0x03E0      /*   0, 128,   0 */
#define DARKCYAN    0x03EF      /*   0, 128, 128 */
#define MAROON      0x7800      /* 128,   0,   0 */
#define PURPLE      0x780F      /* 128,   0, 128 */
#define OLIVE       0x7BE0      /* 128, 128,   0 */
#define LIGHTGREY   0xC618      /* 192, 192, 192 */
#define DARKGREY    0x7BEF      /* 128, 128, 128 */
#define BLUE        0x001F      /*   0,   0, 255 */
#define GREEN       0x07E0      /*   0, 255,   0 */
#define CYAN        0x07FF      /*   0, 255, 255 */
#define RED         0xF800      /* 255,   0,   0 */
#define MAGENTA     0xF81F      /* 255,   0, 255 */
#define YELLOW      0xFFE0      /* 255, 255,   0 */
#define WHITE       0xFFFF      /* 255, 255, 255 */
#define ORANGE      0xFDA0      /* 255, 180,   0 */
#define GREENYELLOW 0xB7E0      /* 180, 255,   0 */
#define PINK        0xFC9F

void setup() {
  Serial.begin(115200);
  tft.begin(0x9486);
  tft.fillScreen(BLACK);
  tft.setRotation(1);
  tft.setTextColor(WHITE, BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(4);
  tft.setCursor(135, 245);
  tft.setTextColor(WHITE, BLACK);
  tft.print("  v      %");
  tft.fillRect(100, 290, 285, 30, BLACK);
  tft.fillRect(100, 290, 10, 30, RED);
  tft.fillRect(375, 290, 10, 30, GREEN);
  tft.fillRect(240, 290, 10, 30, WHITE);
  tft.fillRect(240, 290, 10, 30, ORANGE);
  tft.fillRect(20, 250, 50, 50, WHITE); //LEFT
  tft.fillRect(410, 250, 50, 50, WHITE); //RIGHT
  tft.drawRect(90, 47, 20, 70, WHITE); // tyre car
  tft.drawRect(370, 47, 20, 70, WHITE); // door car
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS);
  mcp2515.setNormalMode();

  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");

}

void loop() {

  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
    if (canMsg.can_id == 0x789 && canMsg.data[canMsg.can_dlc - 3] == 0x41) {                       //start of voltage meter
      int battvolt = canMsg.data[canMsg.can_dlc - 2] * 256 + canMsg.data[canMsg.can_dlc - 1] ; //commands for live car
      tft.setCursor(100, 245);
      tft.setTextColor(ORANGE, BLACK);
      tft.print(battvolt / 4);
      tft.fillRect(100, 290, 285, 30, BLACK);
      tft.fillRect(100, 290, 10, 30, RED);
      tft.fillRect(375, 290, 10, 30, GREEN);
      tft.fillRect(240, 290, 10, 30, ORANGE);
      if (battvolt >= 8 && battvolt < 1676) {
        Serial.println("3/4");
        tft.fillRect(110, 300, battvolt / 8, 10, WHITE);
      } else if (battvolt == 1676)
        tft.fillRect(110, 300, 265, 10, WHITE);            //end of voltage meter
    }
    if (canMsg.can_id == 0x789 && canMsg.data[canMsg.can_dlc - 3] == 0x46) {               //start of SoC
      int Soc = canMsg.data[canMsg.can_dlc - 2] * 256 + canMsg.data[canMsg.can_dlc - 1] ;
      tft.setCursor(270, 245);
      tft.setTextColor(ORANGE, BLACK);
      tft.print(Soc / 10);
    }
  }
  if (canMsg.can_id == 0x72C) {               //start of tyre pressure
    int RL = canMsg.data[canMsg.can_dlc - 2];
    int RR = canMsg.data[canMsg.can_dlc - 2];
    int FR = canMsg.data[canMsg.can_dlc - 2];
    int FL = canMsg.data[canMsg.can_dlc - 2];
    tft.setTextColor(BLUE, BLACK);
    tft.setTextSize(2);
    tft.setCursor(20, 98);
    tft.print(RL / 1.1);
    tft.setCursor(120, 98);
    tft.print(RR / 1.1);
    tft.setCursor(120, 52);
    tft.print(FR / 1.1);
    tft.setCursor(20, 52);
    tft.print(FL / 1.1);
  }
      if (canMsg.can_id == 0x768) {               //start of odometer
      int Odo = canMsg.data[canMsg.can_dlc - 3] + canMsg.data[canMsg.can_dlc - 2] +canMsg.data[canMsg.can_dlc - 1];
      tft.setCursor(30, 200);
      tft.setTextColor(ORANGE, BLACK);
      tft.print(Odo);
    }
    if (canMsg.can_id == 0x748) {
      //problem lies here
} 
}
void diag() {
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setCursor(0, 160);
  tft.setTextSize(2);
  delay(500);
  void();
}

i have marked the area i am having trouble with.
the canbus sends the following data:

ID: 0x748 len: 8 data: 07 62 D1 12 80 00 00 00

the last 4 bytes change with which doors are open (to a maximum of 4) adding a new byte for each one.
eg:
80 00 00 00 = all closed.
01 00 00 00 = drivers door .
01 02 00 00 = drivers and passenger door.
01 02 04 00 = drivers , passgenger and LH rear.
01 02 04 05 = drivers , passgenger, LH rear and RH rear.
plus 10 and 20 for bonnet and boot.

i need to filter whats coming in and display something on my tft like the other parts of the code.
i know i can use if statements but im scratching my head on how to run them quickly going down the list.

would this work:

if (canMsg.can_id == 0x748)  {
if (canMsg.data[canMsg.can_dlc - 4] == 0x80  && canMsg.data[canMsg.can_dlc - 3] == 0x00 && canMsg.data[canMsg.can_dlc - 2] == 0x00 && canMsg.data[canMsg.can_dlc - 1] == 0x00) { 
// do something
}
if (canMsg.data[canMsg.can_dlc - 4] == 0x01  && canMsg.data[canMsg.can_dlc - 3] == 0x00 && canMsg.data[canMsg.can_dlc - 2] == 0x00 && canMsg.data[canMsg.can_dlc - 1] == 0x00) { 
// do something
}
if (canMsg.data[canMsg.can_dlc - 4] == 0x01  && canMsg.data[canMsg.can_dlc - 3] == 0x02 && canMsg.data[canMsg.can_dlc - 2] == 0x00 && canMsg.data[canMsg.can_dlc - 1] == 0x00) { 
// do something
}
if (canMsg.data[canMsg.can_dlc - 4] == 0x01  && canMsg.data[canMsg.can_dlc - 3] == 0x02 && canMsg.data[canMsg.can_dlc - 2] == 0x03 && canMsg.data[canMsg.can_dlc - 1] == 0x00) { 
// do something
}
if (canMsg.data[canMsg.can_dlc - 4] == 0x01  && canMsg.data[canMsg.can_dlc - 3] == 0x02 && canMsg.data[canMsg.can_dlc - 2] == 0x03 && canMsg.data[canMsg.can_dlc - 1] == 0x04) { 
// do something
}

or have i got my prgramming wrong?

To get top marks for Your post always use autoformat in the IDE and code tags, </>, when pasting. That makes the code more easy to read. Members loosing curly parentheses post now and then...

The snippet will not compile. Check by reading Your posting.

This line of code is repeated:

if (canMsg.data[canMsg.can_dlc - 4] == 0x80  &&

I suggest using one leading if like this:

if (canMsg.data[canMsg.can_dlc - 4] == 0x80) {
  next level if {
      code1;
  }
  else if {
     code2;
  }
.
.

}

That’s how I did it.
The if statements were just typed to show concept.

And 0x80 doesn’t repeat every time. It’s only sent if all doors are closed.

Make a kind of map for the thinkable messages.
It ought to be possible to run through a ladder of "if's" where each "if" sets variables, prints out, take action for each door.

If the first byte is not 0x80 there's a door being open.

if (!0x80){
  if (door1)

  if (door2)

  if (door3)

I think I get your meaning.
I get confused with whether I need to use { } or not. What’s best advice?

If You always use them You're safe. An if statement, when true, executes one line of code. Using {} makes all between them be handled as "one line".

} are my biggest hardship it’s like do I put them at the beginning of the ladder of statements or after each individual if line within the ladder.

Always use them in pairs.
Statements like "if", "while" and more just executes one line. When you need to execute more than just one line of code those curly brackets works like extenders allowing several lines of code to be executed.
Have a look at the programming bible: Arduino Reference - Arduino Reference.
Poke around there!

I use it frequently to get the syntax correct.

Does that include if within an if?

Yes. The first "if" executes just one line. That line can also be an "if".
Give every "if" the {} and work inside them.

If (a=1);
// do something

If (a=2); {
// do something
If (a=3);
// do something else
}

Like that?

Absolutely not.
A semikolon like in "If (a=1);" makes that line completely do nothing, only make some microseconds of a delay.

Use the IDE autoformat! That shows the effect of the curly brackets, the indentation.

So it’s not the brackets that’s breaking my code it’s the semicolon?

Please autoformat the the current code and post it in code tags.
Sorry for asking but any late change in the code might be significant.

Ok I will do it tomorrow. Are you around?

I'll be here tomorrow. Say some 16 hours later... 01.30 AM here, high time to get some sleep.

It’s 12:37 am here

On the other side of the globe then....

Uk Tuesday morning