Pages: [1]   Go Down
Author Topic: SD card  (Read 861 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all.
There are several examples on how to log onto a SD card.
My problem is that I have my arduino, I mounted a MC SD card holder from sparkfun. When I startup the board empty, I cannot log.
It seams to not recognice the card.
If I have inserted a SD card, and then startup, It all works fine...

Is there a interupt to fix this?

Best,
Andy smiley
Logged

Offline Offline
God Member
*****
Karma: 19
Posts: 781
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure I follow.  You're confused because you can't log to an SD card if it isn't in the board?  That makes good sense doesn't it.  I've never seen any disk drive that could write to a disk that wasn't in the drive.

Maybe you could show us the code and explain the problem a little better.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there a interupt to fix this?
No. What is the problem? How can you expect to log to a card you haven't inserted?
Logged

Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys! Of course it cannot log when the card is not present...
But after startup, I insert a SD card. Then something is happening... I really dont know what...
To be more spesific, It seams to arduino freezes...

I want to start logging when "Card detection" is high. When SD card is present...

*******************************************************************************************************************
***********************   Code  **********************************************************************************
*******************************************************************************************************************
#include <math.h>
#include <Wire.h>
#include <SD.h>

const int ThermistorValue   = 10000;  // 10kohm resistor
const int chipSelect        = 10;     //
const int CD                = 9;      //  Card Detect

int ledPin                  = 8;
int CDState                 = 0;

void setup() {
  Serial.begin(9600);
 
  pinMode(CD, INPUT);
  pinMode(ledPin, OUTPUT);
 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {Serial.println("Card failed, or not present");}
}

void loop() {
 
  CDState = digitalRead(CD);
  int Temp = Thermister(analogRead(0));
 
  if(CDState==1){
   
    digitalWrite(ledPin, LOW);
    String dataString = String(Temp);
    File dataFile = SD.open("datalog3.txt", FILE_WRITE);
   
    if (dataFile) {
      dataFile.println(dataString);
      dataFile.close();
      // print to the serial port too:
      Serial.println(dataString);
    }
  }
  else
  {
    digitalWrite(ledPin, HIGH);
    Serial.println("Initializing SD card...");
  }
 
 
  delay(1000);
}

double Thermister(int RawADC) {
  double k;
  double c;
  k = log(((10240000/RawADC) - ThermistorValue));
 
  k = 1 / (0.001129148 + (0.000234125 * k) + (0.0000000876741 * k * k * k));
  c = k - 273.15;
  return c;
}


*******************************************************************************************************************
***********************   Code  **********************************************************************************
*******************************************************************************************************************
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But after startup, I insert a SD card. Then something is happening... I really dont know what...
To be more spesific, It seams to arduino freezes...
Some Serial.print() statements would tell you whether that was true, and if so, where the problem is.

But, look at your code. On startup, setup() is run, and fails in the call to SD.begin(), if there is no card.

Then, you insert a card, and (maybe) CDState is set to HIGH. If so, you try to open a file, without ever calling SD.begin() again.

You need to move the call to SD.begin() into loop(), in the CDState == HIGH block, but make sure to call it only once.
« Last Edit: October 08, 2012, 11:28:32 am by PaulS » Logged

Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS, Thanks smiley
I´ll try this now!
If it´s not working, I´ll post it here smiley
Logged

Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS smiley
Thanks! Now it works smiley-grin

-Andy
Logged

Pages: [1]   Go Up
Jump to: