Go Down

Topic: PLEASE HELP - SD card file created but data does not store to it (Read 195 times) previous topic - next topic

agm.godar

Jul 26, 2017, 03:19 pm Last Edit: Jul 26, 2017, 04:50 pm by agm.godar Reason: Code improperly attached
I am using the Arduino UNO R3 and Adafruit Data Logger Shield to log temperature, light, and humidity on a FAT32 SD card. The code I have (attached below) is able to create a file, but sensor readings are not added to it. The sensors I am using are the DHT22 Sensor and the BH1750 sensor. I am new at coding and have no idea why this is not working.  :(

Code: [Select]

/* This is the initial project code for the IRES Arduino project using both the DHT22 sensor and BH1750 sensor*/

//Libraries
#include "RTClib.h";
#include "SD.h";
#include <DHT.h>
#include <Wire.h>

//Constants
#define LOG_INTERVAL  3000 // mills between entries
#define ECHO_TO_SERIAL   1 // echo data to serial por
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT22/AM2302
  DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
int chk;
  float hum;  //Stores humidity value
  float temp; //Stores temperature value
int BH1750_address = 0x23; // i2c Addresse
byte buff[2];
  
//Object and Error
  RTC_PCF8523 rtc;
    char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

const int chipSelect = 10; // for the data logging shield, we use digital pin 10 for the SD cs line

File logfile; // the logging file

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  while(1);
}

void setup(void)
{
  Serial.begin(9600);
  Serial.println("IRES");

  // initialize the SD card
  //Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.print("Card fails, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE);
      break;  // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);
  
//For DHT22 and BH1750
  dht.begin();  
  Wire.begin();
  BH1750_Init(BH1750_address);
  
//For RTC
  if (!rtc.begin()) {
    logfile.println("RTC failed");
#if ECHO_TO_SERIAL
    Serial.println("RTC failed");
#endif  //ECHO_TO_SERIAL
  }
  
  logfile.println("runtime,time,hum,temp,lux");    
    #if ECHO_TO_SERIAL
    Serial.println("runtime,time,hum,temp,lux");
    #endif

}
    
//Additional Coding for BH1750
void BH1750_Init(int address){
  
  Wire.beginTransmission(address);
  Wire.write(0x10); // 1 [lux] resolution
  Wire.endTransmission();
}

byte BH1750_Read(int address){
  
  byte i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()){
    buff[i] = Wire.read();
    i++;
  }
  Wire.endTransmission();  
  return i;
}

void loop()
{
  DateTime now;

// delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));

// runtime, log milliseconds since starting
  uint32_t runtime = millis();
  logfile.print(runtime);
  logfile.print(", ");    
#if ECHO_TO_SERIAL
  Serial.print(runtime);
  Serial.print(", ");  
#endif

// now, log actual time using RTC
  now = rtc.now();
      logfile.print(now.year(), DEC);
      logfile.print("/");
      logfile.print(now.month(), DEC);
      logfile.print("/");
      logfile.print(now.day(), DEC);
      logfile.print(" ");
      logfile.print(daysOfTheWeek[now.dayOfTheWeek()]);
      logfile.print(") ");
      logfile.print(now.hour()-6, DEC);
      logfile.print(":");
      logfile.print(now.minute(), DEC);
      logfile.print(":");
      logfile.print(now.second(), DEC);
    #if ECHO_TO_SERIAL
      Serial.print(now.year(), DEC);
      Serial.print('/');
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.day(), DEC);
      Serial.print(" (");
      Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
      Serial.print(") ");
      Serial.print(now.hour()-6, DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.println();
    #endif //ECHO_TO_SERIAL

//For DHT22
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp= dht.readTemperature();
    
    logfile.print(", ");    
    logfile.print(hum);
    logfile.print(", ");    
    logfile.println(temp);
    
    //Print temp and humidity values to serial monitor
    #if ECHO_TO_SERIAL
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
    #endif //ECHO_TO_SERIAL
    
//For BH1750
    delay(2000);
    float valf=0;
   if(BH1750_Read(BH1750_address)==2){
    
    valf=((buff[0]<<8)|buff[1])/1.2;
  
    logfile.print(", ");    
    logfile.print((int)valf,DEC);
    logfile.print("lux");
    
    #if ECHO_TO_SERIAL
    Serial.print("Light: ");
    if(valf<0)Serial.print("> 65535");
    else Serial.print((int)valf,DEC);
    Serial.println(" lux");
    #endif
  }

//Trying to get this stupid thing to log
 logfile = SD.open("filename", FILE_WRITE);
  if (logfile) {    
    logfile.print(runtime);
    logfile.print(",");    
    now = rtc.now();
      logfile.print(now.year(), DEC);
      logfile.print("/");
      logfile.print(now.month(), DEC);
      logfile.print("/");
      logfile.print(now.day(), DEC);
      logfile.print(" ");
      logfile.print(daysOfTheWeek[now.dayOfTheWeek()]);
      logfile.print(") ");
      logfile.print(now.hour()-6, DEC);
      logfile.print(":");
      logfile.print(now.minute(), DEC);
      logfile.print(":");
      logfile.print(now.second(), DEC);
    logfile.print(",");
    logfile.print(hum);
    logfile.print(",");
    logfile.print(temp);
    logfile.print(",");
    logfile.print((int)valf,DEC);
    logfile.print(",");
    logfile.close(); // close the file
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  
}
}

Nick_Pyner

Have you formatted the card in the proper manner?  There is a sticky about that at the head of the forum.
Post your code in the proper manner using

agm.godar

Hi Nick_Pyner,

I have properly attached my code now. The card I am using is formatted FAT32, is that the correct formatting?

Nick_Pyner

No, you haven't and, if you have not read the first item on this forum, now is the time to do so. Something slipped in my previous, I meant post your code using </> tags.

Go Up