CAN bus send 2 messages with a set up period - arduino Uno mcp2515

Hello,

I am working on a project that involves using an Arduino Uno and an MCP2515 CAN shield to send messages on a CAN bus at a speed of 1 Mbps. I am using the MCP2515 library available on Github at this address GitHub - autowp/arduino-mcp2515: Arduino MCP2515 CAN interface library.

My problem is that when I try to send two different messages, one every 100ms and the other every 200ms, the MCP2515 module sends the messages continuously with no delay between them.

Here is the code I use:

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);

unsigned long lastTimeMsg1 = 0;
unsigned long lastTimeMsg2 = 0;

void setup() {
  canMsg1.can_id  = 0x0F6;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = 0x8E;
  canMsg1.data[1] = 0x87;
  canMsg1.data[2] = 0x32;
  canMsg1.data[3] = 0xFA;
  canMsg1.data[4] = 0x26;
  canMsg1.data[5] = 0x8E;
  canMsg1.data[6] = 0xBE;
  canMsg1.data[7] = 0x86;

  canMsg2.can_id  = 0x036;
  canMsg2.can_dlc = 8;
  canMsg2.data[0] = 0x0E;
  canMsg2.data[1] = 0x00;
  canMsg2.data[2] = 0x00;
  canMsg2.data[3] = 0x08;
  canMsg2.data[4] = 0x01;
  canMsg2.data[5] = 0x00;
  canMsg2.data[6] = 0x00;
  canMsg2.data[7] = 0xA0;

  while (!Serial);
  Serial.begin(115200);
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  
  Serial.println("Example: Write to CAN");
}



void loop() {


  if (millis() - lastTimeMsg1 >= 100) {
    mcp2515.sendMessage(&canMsg1);
    lastTimeMsg1 = millis();
    while (mcp2515.sendMessage(&canMsg1) != MCP2515::ERROR_OK) {
      // wait 
    }
  } 

  if (millis() - lastTimeMsg2 >= 200) {
    mcp2515.sendMessage(&canMsg2);
    lastTimeMsg2 = millis();
    while (mcp2515.sendMessage(&canMsg2) != MCP2515::ERROR_OK) {
      // wait 
    }
  }

  delay(100);
}

I tried to solve this problem by adding a while loop to wait for the TXREQ bit to reset, but that did not solve the problem. When I use this loop, the program crashes after the second pass through the while loop.

If anyone has encountered a similar problem or has any ideas on how to solve this problem, I would love to hear your suggestions.

I thank you in advance for your help.


time between 2 messages = 58µs
Only message 1 is sent = 0xF6

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);

unsigned long lastTimeMsg1 = 0;
unsigned long lastTimeMsg2 = 0;

void setup() {
  canMsg1.can_id  = 0x0F6;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = 0x8E;
  canMsg1.data[1] = 0x87;
  canMsg1.data[2] = 0x32;
  canMsg1.data[3] = 0xFA;
  canMsg1.data[4] = 0x26;
  canMsg1.data[5] = 0x8E;
  canMsg1.data[6] = 0xBE;
  canMsg1.data[7] = 0x86;

  canMsg2.can_id  = 0x036;
  canMsg2.can_dlc = 8;
  canMsg2.data[0] = 0x0E;
  canMsg2.data[1] = 0x00;
  canMsg2.data[2] = 0x00;
  canMsg2.data[3] = 0x08;
  canMsg2.data[4] = 0x01;
  canMsg2.data[5] = 0x00;
  canMsg2.data[6] = 0x00;
  canMsg2.data[7] = 0xA0;

  while (!Serial);
  Serial.begin(115200);
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  
  Serial.println("Example: Write to CAN");
}



void loop() {


  if (millis() - lastTimeMsg1 >= 100) {
    lastTimeMsg1 += 100;
    while (mcp2515.sendMessage(&canMsg1) != MCP2515::ERROR_OK) {    }
  } 

  if (millis() - lastTimeMsg2 >= 200) {
    lastTimeMsg2 += 200;
    while (mcp2515.sendMessage(&canMsg2) != MCP2515::ERROR_OK) {    }
  }
}

@mathoch
You shouldn't mix a millis() and delay() in the same code. If you message timings controled by millis() - why the delay() at end of loop?

Another option - because the time interval2 is a half of interval1 - you can use only delays to manage the sending messages every 100 and 200ms

@b707 Thanks for your answer

I tried many things, indeed delay(100) isn't useful, but the problem persists.

The problem is that I fixed out how to send Serial messages every 100 ms and 200 ms but the mcp2515 module seems to send continously messages. It doesn't change the message it shoud send.

TXREQ bit never resets...

I tried to send only once message 1 in the Setup() but it doesn't send only once the message. I don't know how to send only once at time, then change datas and send other one.

I don't know if it is possible.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.