CAN Signals getting skipped

Hello,

I am sending signals (8 signals) over CAN bus to Arduino Uno and trying to use these signals in a function but as soon as I am calling a function in loop, I am getting only half of the signals over CAN and when function is not called CAN bus communication is working perfectly fine.

I am struggling to find the problem, can anyone please provide some insights to solve this issue.

Thanking you in anticipation

Regards

/****************************************************************************

*************************************************************************/

#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
#include <stdlib.h>
#include <math.h>
int M1, M2, M3, M4, M5, M6, M7, M8;


//********************************Setup Loop*********************************//

void setup() {
  Serial.begin(9600); // For debug use
  Serial.println("CAN Read - Testing receival of CAN Bus message");
  delay(1000);

  if (Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
    Serial.println("CAN Init ok");
  else
    Serial.println("Can't init CAN");

  delay(1000);
}

//********************************Main Loop*********************************//

void loop()
{
  can_lesen();
  Test_call(M1, M2, M3, M4, M5, M6);
}

//********************************CAN Read*********************************//

void can_lesen() {

  tCAN message;
  if (mcp2515_check_message())
  {
    if (mcp2515_get_message(&message))
    {
      if (message.id == 0xA) //Botschaft 1
      {
        M1 = String(message.data[0] + message.data[1] * 256, DEC).toInt();
        M2 = String(message.data[2] + message.data[3] * 256, DEC).toInt();
        M3 = String(message.data[4] + message.data[5] * 256, DEC).toInt();
        M4 = String(message.data[6] + message.data[7] * 256, DEC).toInt();
        Serial.println("Executed_1");  //for Debugging
      }

      if (message.id == 0xB) //Botschaft 2
      {
        M5 = String(message.data[0] + message.data[1] * 256, DEC).toInt();
        M6 = String(message.data[2] + message.data[3] * 256, DEC).toInt();
        M7 = String(message.data[4] + message.data[5] * 256, DEC).toInt();
        M8 = String(message.data[6] + message.data[7] * 256, DEC).toInt();
        Serial.println("Executed_2"); //for Debugging
      }

    }
  }

}

//********************************Test Function*********************************//

void Test_call(int red, int green, int blue, int SpeedDelay, int ReturnDelay, int Batterieladung) {

  Serial.print(red);
  Serial.print(" ");
  Serial.print(green);
  Serial.print(" ");
  Serial.print(blue);
  Serial.print(" ");
  Serial.print(SpeedDelay);
  Serial.print(" ");
  Serial.print(ReturnDelay);
  Serial.print(" ");
  Serial.print(Batterieladung);
  Serial.println(" ");

}

2 things:

if you using an simple arduino avoid using String functions.

secondly you should call ‘Test_call’ AFTER you received 0xA AND 0xB to ensure that you received all your 8 data.

btw is you CAN ID 0x00A, ox00B OR 0xA00 and 0xB00?

I’ve modified your code (not tested as I dont have the hardware handy! :slight_smile: )

hope that helps

/****************************************************************************

*************************************************************************/

#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

union{
	int val;
	uint8_t bytes[2];
}M1, M2, M3, M4, M5, M6, M7, M8;

uint8_t ready=0;



//********************************Setup Loop*********************************//

void setup() {
  Serial.begin(9600); // For debug use
  Serial.println("CAN Read - Testing receival of CAN Bus message");
  delay(1000);

  if (Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
    Serial.println("CAN Init ok");
  else
    Serial.println("Can't init CAN");

  delay(1000);
}

//********************************Main Loop*********************************//

void loop()
{
  can_lesen();
  if(ready==0xAB){
    Test_call(M1.val, M2.val, M3.val, M4.val, M5.val, M6.val);
    ready=0;
  }
}

//********************************CAN Read*********************************//

void can_lesen() {

  tCAN message;
  if (mcp2515_check_message())
  {
    if (mcp2515_get_message(&message))
    {
      if (message.id == 0xA) //Botschaft 1
      {
        M1.bytes[0] = message.data[0]; 
		M1.bytes[1] = message.data[1];
		M2.bytes[0] = message.data[2]; 
		M2.bytes[1] = message.data[3];
		M3.bytes[0] = message.data[4]; 
		M3.bytes[1] = message.data[5];
		M4.bytes[0] = message.data[6]; 
		M4.bytes[1] = message.data[7];
        Serial.println("Executed_1");  //for Debugging
		ready|=0xA0;
      }

      if (message.id == 0xB) //Botschaft 2
      {
	    M5.bytes[0] = message.data[0]; 
		M5.bytes[1] = message.data[1];
		M6.bytes[0] = message.data[2]; 
		M6.bytes[1] = message.data[3];
		M7.bytes[0] = message.data[4]; 
		M7.bytes[1] = message.data[5];
		M8.bytes[0] = message.data[6]; 
		M8.bytes[1] = message.data[7];
        Serial.println("Executed_2"); //for Debugging
		read|=0x0B;
      }

    }
  }

}

//********************************Test Function*********************************//

void Test_call(int red, int green, int blue, int SpeedDelay, int ReturnDelay, int Batterieladung) {

  Serial.print(red);
  Serial.print(" ");
  Serial.print(green);
  Serial.print(" ");
  Serial.print(blue);
  Serial.print(" ");
  Serial.print(SpeedDelay);
  Serial.print(" ");
  Serial.print(ReturnDelay);
  Serial.print(" ");
  Serial.print(Batterieladung);
  Serial.println(" ");

}

Thanks a lot Sherzaad, your code helped me to resolve some issues and finally I am able to solve my problem.