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 = z10.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 = z20.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);
}