Go Down

Topic: Sketch only records one set of readings to SD card (Read 154 times) previous topic - next topic

agm.godar

I am trying to log sensor readings from the BH1750 and DHT22 sensors using the Adafruit Data Logger Shield. The sketch I has properly writes the readings to the serial monitor, but only writes/prints one set of readings to the SD card. Is their an order to the delay and close commands I am messing up?

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>
#include <SPI.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...");
  pinMode(10, OUTPUT); // make sure that the default chip select pin is set to output, even if you don't use it:
 
// 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);
   
    #if ECHO_TO_SERIAL
    Serial.print("Light: ");
    if(valf<0)Serial.print("> 65535");
    else Serial.print((int)valf,DEC);
    Serial.print(" lux");
    #endif
  }
   
  //Time between readings
  delay(3000);  //wait x min before next read/write
 
  // close the file:
    logfile.close();
}

cattledog

Quote
only writes/prints one set of readings to the SD card. Is their an order to the delay and close commands I am messing up?
If you are going to call logfile.close() at the end of loop(), you will need to reopen the file at the beginning of loop().

Go Up