I am attempting to interface an Arduino Mega 2560 with a system of four Black Jaguar motor controllers connected in CAN. I'm using the CAN shield from SeeedStudios to accomplish this.

The way Jaguars in CAN mode are configured, you must send a message every 100 ms to the network to keep the link active. However, I'm also getting input from Serial to another Arduino as well as sending other commands to the Jaguars, but not regularly enough to specifically time it out.

Is it possible to have the link message sent asynchronously in the background while I perform other code? I have looked into timers and interrupts, however I am unsure if this would truly solve the issue considering I have other processes that take more than 100 ms to complete.

Here's my loop statement:

loop() {
  CAN.sendMsgBuf(HEARTBEAT,1,0,volModeEnable); //HEARTBEAT keeps CAN link active
  setVoltage(3,.5); //Sets voltage of Motor by percent
  //GrabFromSerial(); Takes longer than 100ms
  //Control Statement for Additional Commands goes here... (also necessary to be > 100ms)
  • Just curious why does it take you more than 100ms to read the serial port?
    – Eric E
    Jul 26 '15 at 16:46
  • Im using this library: github.com/madsci1016/Arduino-EasyTransfer to save me the trouble of writing my own serial protocol. However i cant always garauntee it's time to execute because of the other arduinos timing. Jul 27 '15 at 17:03
  • I havnt looked at the library but perhaps it would be more simple to write your own protocol. Then you can use methods such as serial.available() that will tell you when data ia ready to be received without blocking the execution of your program
    – Eric E
    Jul 27 '15 at 17:49

I see several options:

  1. Change your code to achieve lower delays on the other functions
  2. Use interrupts at a set frequency to trigger the sending (and maybe the automatic transmission trigger pins, see MCP2515 datasheet, depending on pin use & schematic of your specific shield)
  3. Use a basic scheduler/rtos

It is hard to tell what is going on without the rest of your code, if it is a good idea to try to achieve faster functions.

I am not 100% sure the CAN-lib would will work well in the context of an interrupt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.