Two CAN bus message compare

So i want to read from two different Can messages from different can bus, they both are sending the same message but at different times. I want to basically compare both the Can message bits and find the difference between the two and perform some action. The problem is that when i do that i see a difference the 2nd can message shows the difference as - value of 1 cause its reading the second one as 0.

here is my code

void loop()
{

unsigned char len1 = 0;
unsigned char buf1[8];
unsigned char len2 = 0;
unsigned char buf2[8];
float Voltage1=0;
float Voltage2=0;
float Vtemp1=0;
float Vtemp2=0;
CAN1.sendMsgBuf(0x700,0,8,data3); // Send pack 1 to open contactors

CAN2.sendMsgBuf(0x700,0,8,data3); // Send pack 2 to open contactors

if(CAN_MSGAVAIL == CAN1.checkReceive() || CAN_MSGAVAIL == CAN2.checkReceive())
{

CAN1.readMsgBuf(&len1, buf1);
CAN2.readMsgBuf(&len2, buf2); // read data, len: data length, buf: data buf

unsigned int canId1 = CAN1.getCanId();
unsigned int canId2 = CAN2.getCanId();

if (canId1==123)
{

unsigned int x1= buf1[2]; //Select bit
unsigned int y1= buf1[3];
unsigned int z1= x1256 + y1; //concatenate
Voltage1 = z1
0.002; //Convert obtained decimal value to voltage
if(Voltage1>0)
{
float Vtemp1=Voltage1;
Serial.println(Vtemp1);
}
}
if (canId2==123)
{

unsigned int x2= buf2[2]; //Select bit
unsigned int y2= buf2[3];
unsigned int z2= x2256 + y2; //concatenate
Voltage2 = z2
0.002; //Convert obtained decimal value to voltage
if(Voltage2>0)
{
float Vtemp2=Voltage2;
Serial.println(Vtemp2);

}
}

float difference = Vtemp2-Vtemp1;
if(difference>0 || difference<0)
{
Serial.println(difference);
}

yes i want to make sure i have received both messages and then compare.
how do i do it?

Sorry about that.

yes i tried doing what you said but it still reads just one can bus voltage and the other as 0 hence the difference is always negative of voltage 1 ie if v1=55 and v2=0 , difference = -55.

added code-

if (Voltage1!= 0 && Voltage2 != 0)
{
float difference = Voltage1-Voltage2;
Serial.println(difference);
}

Yes i will do that. Even if i change it to int. How do i fix the timing issue or retain the value of each can bus voltage when it receives a 0 on the other can bus so i can compare it later.

code#

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

const int SPI_CS_PIN1 = 9; // select pin 9
const int SPI_CS_PIN2 = 10 ; // select pin 10

MCP_CAN CAN1 (SPI_CS_PIN1);
MCP_CAN CAN2 (SPI_CS_PIN2); // Set CS pin

void setup()
{
Serial.begin(9600);

while (CAN_OK != CAN1.begin(CAN_500KBPS) && CAN_OK != CAN2.begin(CAN_500KBPS) ) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
Serial.println("CAN BUS Shield init ok!");

}

unsigned char data3[8] = {04,00,00,00,00,00,00,00}; // Wake up can
void loop()
{

unsigned char len1 = 0;// clear flag unsigned char len1 = 0;
unsigned char buf1[8];
unsigned char len2 = 0;
unsigned char buf2[8];
int Voltage1;
int Voltage2;

CAN1.sendMsgBuf(0x700,0,8,data3); // Send pack 1 to open contactors
CAN2.sendMsgBuf(0x700,0,8,data3); // Send pack 2 to open contactors
if(CAN_MSGAVAIL == CAN1.checkReceive()) // check if data coming
{
CAN1.readMsgBuf(&len1, buf1);
unsigned int canId1 = CAN1.getCanId();

if (canId1==768)
{
unsigned int x1= buf1[2]; //Select bit
unsigned int y1= buf1[3];
unsigned int z1= x1256 + y1; //concatenate
Voltage1 = z1
0.002; //Convert obtained decimal value to voltage
}

}
if(CAN_MSGAVAIL == CAN2.checkReceive())
{
CAN2.readMsgBuf(&len2, buf2);
unsigned int canId2 = CAN2.getCanId();

if (canId2==768)
{
unsigned int x2= buf2[2]; //Select bit
unsigned int y2= buf2[3];
unsigned int z2= x2256 + y2; //concatenate
Voltage2 = z2
0.002; //Convert obtained decimal value to voltage

}

}
if (Voltage1!= 0 && Voltage2 != 0)
{
int difference = Voltage1-Voltage2;
Serial.println(difference);
}
}

Delta_G:

unsigned char data3[8] = {04,00,00,00,00,00,00,00}; // Wake up can

Do you know what putting a leading 0 on a number means? Obviously not. You might want to investigate that too.

In my years of programming it means someone spent extra time typing.

On point.
OP: Do you what you say you want to.

  1. Wait to receive message #1
  2. Wait to receive message #2
  3. If both messages received, then loop through data and compare.

I'm failing to see how this wouldn't work.

I also see lots of programming in the loop() routine. Try using subroutines to break up the program and manage the thought process.

yes changing the voltages to static made the code work. Thank you.

Delta_G:
Then you should go look it up too.

Here's a hint:

int x = 015;

int y = 15;
if(x == y) {
    // whatever you put here will never happen
}

int a = 015;
int b = 13;

if(a == b) {
   // this one will be true
}




In the OP's case it will work out by coincidence. But as soon as he writes 08 or 09 for one of them he's got a compile error.

I will try it when I am near my arduino, in the meantime I'm not finding anything in Google. How about providing useful information, what number system is it causing the compiler to use?

Delta_G:
Come on adw. I know you're smarter than that. In what system is 15 equal to 13 decimal?

I didn't notice the scroll bar, only reading the program snippet first displayed. Even so, I'm not sure I would have followed what you were trying to say.

Interesting. I have lots of arguments with IT people when they add the leading zeros to the IP address lists. Same thing, leading zeros convert the IP address to octal.

Context? Documentation. Windows applications. DOS. Ping 192.168.1.010 is not the same as Ping 192.168.1.10.

adwsystems:
Context? Documentation. Windows applications. DOS. Ping 192.168.1.010 is not the same as Ping 192.168.1.10.

Interesting. I just tried "ping 010.010.010.010" in Linux and it pinged 8.8.8.8. You can also use hexadecimal with a "0x" prefix (also like C/C++).