Changing SS pin to use multiple SPI devices

I’m relatively new to Arduinos and I’ve encountered an issue with SPI.

I’m using an SD card module and a CAN Shield (v2.0). I have the SEEEDStudio CAN library, default Ardunio SD Card library and the default Arduino SPI Library. I only need to use the SD card during the setup(). The rest of the time, I’ll be using the CAN shield. I’ve had trouble using both on my Arduino Mega. I can get them to function independently when I only test them individually with SS pin 53 but I’m having issues when assigning an SS pin other than 53. I can’t seem to use a pin other than 53 for the SS pin. The SD card module and CAN shield both use this SS pin (53). As a test, I tried defining 49 as the SS pin for one of them but that didn’t work.

The following is an example of attempting to change the SS pin on the CAN device.

#include <SPI.h>          //SPI is used to talk to the CAN Controller
#include <SD.h>
#include <mcp_can.h>

MCP_CAN CAN(49);

void setup()
{
  Serial.begin(9600);   //to communicate with Serial monitor
  Serial.println("hello");
//tries to initialize, if failed --> it will loop here for ever
START_INIT:

    if(CAN_OK == CAN.begin(CAN_250KBPS))      //setting CAN baud rate to 250Kbps
    {
        Serial.println("CAN BUS Shield init ok!");
    }
    else
    {
        Serial.println("CAN BUS Shield init fail");
        Serial.println("Init CAN BUS Shield again");
        delay(100);
        goto START_INIT;
    }
}

The output is always “CAN BUS Shield init fail” because the SS pin is not 53.

How can I use two different SS pins in order to be able to use both my SD card module and CAN shield at the same time.

I appreciate any advice.

Use D4 for the SD module SS pin. Most shields that have a SD card on board use D4 for the SS.