Reading CD pin on SD card

Has anyone done anything with this pin on a card reader? What's a good way of detecting it, through an interrupt or a regular pin? What I'd like to do is detect that a card has been inserted, wait a few seconds, and then try to access it.

And then for the reverse, detect when it gets removed and move on to a different portion of the code. I think this last bit is going to be tricky because if I'm constantly reading from the card and it gets removed, what happens? Does the program crash, does the MCU lock up, or would there be a way to have a timeout of sorts and go to a different part of the code?

What's a good way of detecting it, through an interrupt or a regular pin?

What have you tried? How important is it to know the exact instant the card is inserted? If it is critical, then interrupts are the way to go. If next time through loop() is soon enough, then polling is the way to go.

What I'd like to do is detect that a card has been inserted, wait a few seconds, and then try to access it.

Why wait? If the card is there, you can access it.

Since you are willing to wait, interrupts are not needed.

I think this last bit is going to be tricky because if I'm constantly reading from the card and it gets removed, what happens?

Try it, and see. The read() will fail, obviously. You should be able to determine this.

Does the program crash, does the MCU lock up, or would there be a way to have a timeout of sorts and go to a different part of the code?

No, depends on your code, and yes.

PaulS:

What's a good way of detecting it, through an interrupt or a regular pin?

What have you tried? How important is it to know the exact instant the card is inserted? If it is critical, then interrupts are the way to go. If next time through loop() is soon enough, then polling is the way to go.

Haven't tried anything yet. That's this evening's project. I'm just preparing here ... This has to do with this POV project I'm working on. What I'm envisioning here is something like this (if I can get it to work):
Turn the unit on and it does a check for an SD card
If not present -> enter one while() loop that pulls static images stored in memory - let's call it the "static loop".
If present -> pull image data from SD card - let's call this the "sd loop".

If the system is already running with no SD card, I want to be able to detect when a card gets inserted, jump out of the static loop and go to the sd loop.
If the system is already running with an SD card and it gets removed, jump out of the sd loop and go back to the static loop.

PaulS:

What I'd like to do is detect that a card has been inserted, wait a few seconds, and then try to access it.

Why wait? If the card is there, you can access it.
Since you are willing to wait, interrupts are not needed.

Why wait, because that's an assumption I'm making. When does that switch trigger, when the card "clicks" into place" When it gets pushed in far enough, but not yet locked in place? So accounting for that possibility, I thought perhaps when it detects the pin change, wait a few seconds, in case it triggered too early and the user is still trying to shove the thing all the way in.

PaulS:

I think this last bit is going to be tricky because if I'm constantly reading from the card and it gets removed, what happens?

Try it, and see. The read() will fail, obviously. You should be able to determine this.

Does the program crash, does the MCU lock up, or would there be a way to have a timeout of sorts and go to a different part of the code?

No, depends on your code, and yes.

That's the part I need to figure out, hence me asking, thinking someone may have already encountered this and perhaps had suggestions of how to tackle it. Especially in my system where I'm constantly reading the SD card, as soon as it gets removed, I don't know what happens ... if the MCU or code locks up, I'm in trouble. But if it "gracefully" fails and allows me to determine that and move on, great.

I expect your read() request will return an error. It's easy enough to detect.

  result = file.read(buf, length);
  if (result != length)
  {
    // Handle error
  }

Here's what the documentation for read() has to say:

Returns:
For success read() returns the number of bytes read. A value less than nbyte, including zero, will be returned if end of file is reached. If an error occurs, read() returns -1. Possible errors include read() called before a file has been opened, corrupt file system or an I/O error occurred.

Usually file systems have a GetLastError() function or some way to find out which error occurred, but the SDFat documentation didn't say how that's done. There's getWriteError(), but...