Mystakes Arduino Mega with CANbus

Hello everyone i have a problem with my project.

I try to read data transmitted by a sensor with SPI link (MISO MOSI CS CLCK) on my Sparkfun board plugged on an Arduino MEGA. Also work in the library Seeed_Arduino_CAN.

I realized the attached code but after debugging and from what I observe in the console I am blocked at the level of my void loop :

void loop() {
  unsigned char len = 0;
  unsigned char buf[8];

  if (CAN_MSGAVAIL == CAN.checkReceive()) {    // check if data coming
    CAN.readMsgBuf(&len, buf);                 // read data,  len: data length, buf: data buf

    unsigned long canId = CAN.getCanId();

Apparently the problem is the : if (CAN_MSGAVAIL == CAN.checkReceive()) I tried to replace the == by != and my code works but I get false values... and i dont know what to do.

I give you my complete code here with which I get the following result on my console :

15:41:34.730 -> CAN init ok!
15:41:34.776 -> SD card is present & ready
15:41:34.776 -> File created! 
#include <SPI.h>
#include <SD.h>
#include <TimeLib.h>
#include "mcp2515_can.h"

//*********************CAN Sheild declarations*********************

const int SPI_CS_PIN = 53;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN);

//*********************SD Sheild declarations**********************

File myFile;
const int SD_CS_PIN = 9;

//*********************Record Excel declarations*******************

char filename[16];
unsigned long reads_recorded = 0;
uint16_t i = 0;

//*********************Switch and Led declarations*****************

const int buttonPin = 40;     // the number of the pushbutton pin
const int ledPin =  41;      // the number of the LED pin

//********************* Program SETUP *****************************

void setup() {

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  SERIAL_PORT_MONITOR.begin(115200);

  while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) { // init can bus : baudrate = 500k
    SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
    while (1); //wait here forever
  }
  SERIAL_PORT_MONITOR.println("CAN init ok!");

  if (SD.begin(SD_CS_PIN))
  {
    SERIAL_PORT_MONITOR.println("SD card is present & ready");

    //initialise filename
    sprintf(filename, "can_%.03d.csv", i);
    
    myFile = SD.open(filename, FILE_WRITE);
    if (myFile) // it opened OK
    {
      //add header to file
      myFile.println("Time,Pression,Temperature,co2_hydrogen,humidity");
      myFile.close();
      SERIAL_PORT_MONITOR.println("File created!");
    }
    else
      SERIAL_PORT_MONITOR.println("Error opening file");
  }
  else
  {
    SERIAL_PORT_MONITOR.println("SD card missing or failure");
    while (1); //wait here forever
  }
  pinMode(CAN_INT_PIN, INPUT); 
}
//********************* Fonction ecrire dans Excel ***********************

void writeToFile(float press_pascal, float temp_degC, float co2_true, float humi_pourcent)
{
  myFile = SD.open(filename, FILE_WRITE);
  if (myFile) // it opened OK
  {
    myFile.print(String(minute()));
    myFile.print(":");
    myFile.print(String(second()));
    myFile.print(";");
    myFile.print(String(press_pascal, 2));
    myFile.print(";");
    myFile.print(String(temp_degC, 2));
    myFile.print(";");
    myFile.print(String(co2_true, 2));
    myFile.print(";");
    myFile.println(String(humi_pourcent, 2));
    myFile.close();
  }
  else
    SERIAL_PORT_MONITOR.println("Error opening file");
}

//********************* Program LOOP ************************

void loop() {
  
  unsigned char len = 0;
  unsigned char buf[8];


 if (digitalRead(buttonPin) == HIGH) 
    {
  if(CAN_MSGAVAIL == CAN.checkReceive())
      {
      digitalWrite(ledPin, HIGH);  // turn LED on
      
      CAN.readMsgBuf(&len, buf); // read data,  len: data length, buf: data buf

    unsigned long canId = CAN.getCanId();

    SERIAL_PORT_MONITOR.println("_____Pression_____");
    uint16_t press_raw;
    memcpy(&press_raw, &buf[0], 2); //buf[0] is the first byte in buf, 2 because you want 2 bytes
    float press_pascal = ((press_raw * 0.0078125) - 250);
    SERIAL_PORT_MONITOR.println(press_pascal, 2);  // value to copy on excel

    SERIAL_PORT_MONITOR.println("_____Temperature_____");
    uint16_t temp_raw;
    memcpy(&temp_raw, &buf[2], 2); //buf[2] is the third byte in buf, 2 because you want 2 bytes
    float temp_degC = ((temp_raw * 0.3125) - 273) / 100;
    SERIAL_PORT_MONITOR.println(temp_degC, 2);

    SERIAL_PORT_MONITOR.println("_____CO2_Hydrogen_____");
    uint16_t co2_raw;
    memcpy(&co2_raw, &buf[4], 2); //buf[4] is the fifth byte in buf, 2 because you want 2 bytes
    float co2_true = ((co2_raw * 1) - 0) / 100;
    SERIAL_PORT_MONITOR.println(co2_true, 2);

    SERIAL_PORT_MONITOR.println("_____Humidity_%_____");
    uint16_t humi_raw;
    memcpy(&humi_raw, &buf[6], 2); //buf[6] is the six byte in buf, 1 because you want 1 bytes
    float humi_pourcent = ((humi_raw * 0.4) - 0);
    SERIAL_PORT_MONITOR.println(humi_pourcent, 2);

    
    writeToFile(press_pascal, temp_degC, co2_true, humi_pourcent);
    ++reads_recorded;

    }
    else 
    {
    digitalWrite(ledPin, LOW); // turn LED off
    }; 
  }
  
//********************* Creation new files excel ***********************
  
  //create new filenames for every 1800 lines written to file
  if(reads_recorded == 1800) {  //reset counter
    reads_recorded = 0;

    //increment file counter
    ++i;
    
    //update finename
    sprintf(filename, "can_%.03d.csv", i);

    myFile = SD.open(filename, FILE_WRITE);
    if (myFile) // it opened OK
    {
      //add header to new file
      myFile.println("Time,Pression,Temperature,co2_hydrogen,humidity");
      myFile.close();
      SERIAL_PORT_MONITOR.println("New File created!");
    }
    else
      SERIAL_PORT_MONITOR.println("Error opening file");
  }
}

Sorry the code is a bit long because the project is almost entirely coded but I still have this problem to solve I hope someone can help me

That sounds like a correctly wired hardware (CAN shield to Arduino) but no message on the CAN bus. So either the wiring on the CAN bus isn't working or no other node is sending any message.

You have changed your code to respond to a message that is not there. Post a schematic of how you have this wired, showing all nodes on the bus and the termination. This sounds like a hardware problem. What CAN library did you use. Does it have examples?

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