Trying to connect two SPI modules to arduino ( CAN-BUS module for Arduino - ElectroYA RC - Racing Drones and a SD module) in order to read from my car CAN and save a log on the SD card for emulating the information indoor, but having some problem in making both the modules work at the same time.
If I test the module individually, they work without a problem, if i connect both of them and try just to read from the CAN module, i receive no information, but the moment I disconect the SD card module, the data starts to pour. I have connected the SKC, MOSI, MISO and the INT (for the CAN module), and the CS to pins 10 for the CAN module and 4 for the SD module.
I have attached below the arduino code for checking the number of msg/sec from the CAN BUS.
#include <SPI.h>
#include <mcp2515.h>
struct can_frame canMsg;
MCP2515 mcp2515(10);
int cntr = 0;
unsigned long oldTime = 0;
void setup() {
Serial.begin(115200);
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("------- CAN Speedtest ----------");
}
void loop() {
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
cntr++;
}
if ((millis()-oldTime)>1000) {
oldTime = millis();
Serial.print(cntr);
Serial.println(" msg/sec");
cntr = 0;
}
}
Also, i will attach the code for reading and logging the data to the sd card.
#include <SPI.h>
#include <SD.h>
#include <mcp2515.h>
File myFile;
const int button = 7;
bool read_switch = false;
struct can_frame canMsg;
MCP2515 mcp2515(10);
void setup() {
Serial.begin(115200);
Serial.print("Initializing SD card...");
if (!SD.begin(4)){
Serial.println("initialization failed!");
}
if (!SD.begin(4)) Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
Serial.println("File opened");
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
mcp2515.setNormalMode();
Serial.println("------- CAN Read ----------");
Serial.println("ID DLC DATA");
}
void loop() {
if (digitalRead(button) == HIGH){
Serial.println("Button pressed");
myFile.println("Button pressed");
if (read_switch == true){
read_switch = false;
myFile.println("File closed");
myFile.close();
Serial.println("File closed");
} else read_switch = true;
delay(500);
}
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK && read_switch == true) {
myFile.print(millis());
myFile.print("-");
myFile.print(canMsg.can_id); // print ID
myFile.print("-");
myFile.print(canMsg.can_dlc); // print DLC
myFile.print("-");
for (int i = 0; i<canMsg.can_dlc; i++) { // print the data
myFile.print(canMsg.data[i]);
myFile.print(" ");
}
myFile.println();
Serial.println(".");
}
}
Also, i have tested the hardware setup with 2 different CAN modules, unfortunatelly, I only have one SD card module, but if tested independently, it works perfectly.
Can't seem to find any problem, I'm stuck.