Estimate for Sketch

I have a MKR Zero with a CAN Shield. What would it cost to build a sketch that takes CAN Data in from the Shield and saves it to the SD Card on the MKR Zero?

calculate with ~ 150/h, multiply that with a decent amount of time for explaining in detail what you really want to do. Multioly that number by 10 and you have an estimate.

2 Likes

pretty cheap if you ask chatGPT

of course your mileage may vary if there is a bug... (here the file is never closed)

#include <SPI.h>
#include <SD.h>
#include <mcp_can.h>

// Pin definitions
const int chipSelect = SDCARD_SS_PIN;
const int CAN_CS_PIN = 3;  // Adjust this if your CAN shield uses a different CS pin

// Create CAN and SD objects
MCP_CAN CAN(CAN_CS_PIN);
File dataFile;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  // Initialize the CAN bus at 500 kbps
  if (CAN.begin(MCP_ANY, 500000, MCP_8MHZ) == CAN_OK) { // adjust to your CAN
    Serial.println("CAN Init OK");
  } else {
    Serial.println("CAN Init Failed");
    while (true) yield();
  }

  // Set CAN mode to normal
  CAN.setMode(MCP_NORMAL);

  // Initialize SD card
  if (!SD.begin(chipSelect)) {
    Serial.println("SD card initialization failed!");
    while (true) yield();
  }
  Serial.println("SD card initialized.");

  // Create or open a file for writing
  dataFile = SD.open("canlog.txt", FILE_WRITE);
  if (!dataFile) {
    Serial.println("Failed to open file for writing");
    while (true) yield();
  }

  Serial.println("Setup complete.");
}

void loop() {
  // Check if data is available from the CAN bus
  if (CAN_MSGAVAIL == CAN.checkReceive()) {
    // Read the message
    long unsigned int rxId;
    unsigned char len = 0;
    unsigned char rxBuf[8];

    CAN.readMsgBuf(&rxId, &len, rxBuf);

    // Print the received data to the serial monitor
    Serial.print("ID: ");
    Serial.print(rxId, HEX);
    Serial.print(" Data: ");
    for (int i = 0; i < len; i++) {
      Serial.print(rxBuf[i], HEX);
      Serial.print(" ");
    }
    Serial.println();

    // Save the received data to the SD card
    if (dataFile) {
      dataFile.print("ID: ");
      dataFile.print(rxId, HEX);
      dataFile.print(" Data: ");
      for (int i = 0; i < len; i++) {
        dataFile.print(rxBuf[i], HEX);
        dataFile.print(" ");
      }
      dataFile.println();
      dataFile.flush();  // Ensure data is written to the file
    }
  }
}

I'm of two minds with ChatGPT.

On the one hand, I asked it to write some Arduino code and it did it correctly, to my amazement.

On the other hand, for a "real" task, to save me some time, I asked it to setup a chip's registers to perform a certain function and it failed miserably.

On the gripping hand, I needed to triple check that water was heavier than gasoline and it assured me that it was not. Not only did it say that, but it said in one sentence that water was not heavier than gasoline, and in the next, that water was lighter than gasoline. At least it's internally consistent!

1 Like

This looks A LOT better then I was trying ..I'll give it a shot and Thank you!