Go Down

Topic: The arduino stop storing data to SD card after 25 seconds, Please help (Read 78 times) previous topic - next topic

mehafelix

I am coding three CO2 sensors MG811. When running the code I noticed that the Arduino could not open the sd card and store data after 25 seconds of looping. Multiple try but same thing. Am I doing anything wrong??
Here is the code
Code: [Select]
#include <SD.h>



/************************Hardware Related Macros*********|***************************/
#define         MG_PIN                       (A0)     //define which analog input channel you are going to use
#define         BOOL_PIN                     (2)     //Arduino D2-CO2 sensor digital pinout, labled with "D" on PCB  
#define         MG_PIN2                       (A1)     //define which analog input channel you are going to use for sensor 2
#define         BOOL_PIN2                     (5)     //Arduino D2-CO2 sensor digital pinout, labled with "D" on PCB  for sensor 2
#define         MG_PIN3                       (A2)     //define which analog input channel you are going to use for sensor 3
#define         BOOL_PIN3                     (6)     //Arduino D2-CO2 sensor digital pinout, labled with "D" on PCB for sensor 3
#define         DC_GAIN                      (8.5)   //define the DC gain of amplifier

/***********************Software Related Macros************************************/
#define         READ_SAMPLE_TIMES            (10)     //define how many samples you are going to take in normal operation
#define         READ_SAMPLE_INTERVAL         (50)    //define the time interval(in milisecond) between each samples in
//normal operation

/**********************Application Related Macros**********************************/
//These values differ from sensor to sensor. User should derermine this value.
#define         ZERO_POINT_X                 (2.602) //lg400=2.602, the start point_on X_axis of the curve
#define         ZERO_POINT_VOLTAGE           (0.324) //define the output of the sensor in volts when the concentration of CO2 is 400PPM
#define         MAX_POINT_VOLTAGE            (0.265) //define the output of the sensor in volts when the concentration of CO2 is 10,000PPM
#define         REACTION_VOLTGAE             (0.059) //define the €œvoltage drop€ of the sensor when move the sensor from air into 1000ppm CO2

/*****************************Globals***********************************************/
float           CO2Curve[3]  =  {ZERO_POINT_X, ZERO_POINT_VOLTAGE, (REACTION_VOLTGAE / (2.602 - 4))};
//Two points are taken from the curve.With these two points, a line is formed which is
//"approximately equivalent" to the original curve. You could use other methods to get more accurate slope
File MyFile;

//CO2 Curve format:{ x, y, slope};point1: (lg400=2.602, 0.324), point2: (lg10000=4, 0.265)
//slope = (y1-y2)(i.e.reaction voltage)/ x1-x2 = (0.324-0.265)/(log400 - log10000)
//**********************************************Void setup of code******************************************************************
void setup() {
  Serial.begin(9600);                              //UART setup, baudrate = 9600bps
  pinMode(BOOL_PIN, INPUT);                        //set pin to input
  digitalWrite(BOOL_PIN, HIGH);                    //turn on pullup resistors

  pinMode(BOOL_PIN2, INPUT);                        //set pin to input
  digitalWrite(BOOL_PIN2, HIGH);                    //turn on pullup resistors

  pinMode(BOOL_PIN3, INPUT);                        //set pin to input
  digitalWrite(BOOL_PIN3, HIGH);                   //turn on pullup resistors
  pinMode(8, OUTPUT);

  Serial.print("MG-811 Demostration\n");\
                                                    //Setting up the sd card for writing
  Serial.print("Initializing SD card...");
  if (!SD.begin(8)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  MyFile = SD.open("datafile.txt", FILE_WRITE);
  if (MyFile){
  MyFile.print( "volt1:");
  MyFile.print("\t\t\t\t");
  MyFile.print("Volt2");
  MyFile.print("\t\t\t\t");
  MyFile.print("Volts3");
  MyFile.print("\t\t\t\t");
  MyFile.print("C02(1):");
  MyFile.print("\t\t\t\t");
  MyFile.print("C02(2):");
  MyFile.print("\t\t\t\t");
  MyFile.print("C02(3):");
  MyFile.print("\t\t\t\t");
  MyFile.println("TIME:");
  MyFile.close();
  }
}
//**********************************************************loop***********************************************************
void loop() {
 unsigned long currTime= (millis()/1000);
MyFile = SD.open("datafile.txt", FILE_WRITE);
if (!MyFile) {
    Serial.println ("ERROR OPEN SD CARD, DATA WOULD NOT BE STOREED!!");
  }
  int percentage1;
  int percentage2;
  int percentage3;

  float volts1;
  float volts2;
  float volts3;

  volts1 = MGRead(MG_PIN);
  volts2 = MGRead(MG_PIN2);
  volts3 = MGRead(MG_PIN3);
                                                    

  
    //////////////////////////////////////////////////////// print to sd card sens01 : the code from the top is repeat by changing Serial to MyFile.
   MyFile = SD.open("datafile.txt", FILE_WRITE);
  if (MyFile) {
  
 
  MyFile.print(volts1);
  MyFile.print("\t\t\t\t");
  MyFile.print(volts2);
  MyFile.print("\t\t\t\t");
  MyFile.print(volts3);
  MyFile.print("\t\t\t\t");
  
  percentage1 = MGGetPercentage(volts1, CO2Curve);
  if (percentage1 == -1) {
    MyFile.print(" Heating/bey range(400~10,000) ");
  } else {
    MyFile.print(percentage1);
  }
  MyFile.print("\t\t\t\t");
   percentage2 = MGGetPercentage(volts2, CO2Curve);
  if (percentage2 == -1) {
    MyFile.print("heating/bey range(400~10,000) ");
  } else {
    MyFile.print("                ");
    MyFile.print(percentage2);
  }
  
  MyFile.print("\t\t\t\t");
 percentage3 = MGGetPercentage(volts3, CO2Curve);
  if (percentage3 == -1) {
    MyFile.print(" heating/bey range(400~10,000) ");
  } else {
    
    MyFile.print(percentage3);
  }
 MyFile.print("\t\t\t\t");

  MyFile.println(currTime);

  MyFile.close();
  }
}
/*****************************  MQGetPercentage **********************************
  Input:   volts   - SEN-000007 output measured in volts
         pcurve  - pointer to the curve of the target gas
  Output:  ppm of the target gas
  Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm)
         of the line could be derived if y(MG-811 output) is provided. As it is a
         logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic
         value.
************************************************************************************/
int  MGGetPercentage(float volts1, float *pcurve) {
  volts1 = volts1 / DC_GAIN;
  if (volts1 > ZERO_POINT_VOLTAGE || volts1 < MAX_POINT_VOLTAGE ) {
    return -1;
  } else {
    return pow(10, (volts1 - pcurve[1]) / pcurve[2] + pcurve[0]);
    volts1 = 0;
  }

}
//***********************************************************************************/
int  MGGetPercentage2(float volts2, float *pcurve) {
  volts2 = volts2 / DC_GAIN;
  if (volts2 > ZERO_POINT_VOLTAGE || volts2 < MAX_POINT_VOLTAGE ) {
    return -1;
  } else {
    return pow(10, (volts2 - pcurve[1]) / pcurve[2] + pcurve[0]);
    volts2 = 0;
  }

}
//***********************************************************************************/
int  MGGetPercentage3(float volts3, float *pcurve) {
  volts3 = volts3 / DC_GAIN;
  if (volts3 > ZERO_POINT_VOLTAGE || volts3 < MAX_POINT_VOLTAGE ) {
    return -1;
  } else {
    return pow(10, (volts3 - pcurve[1]) / pcurve[2] + pcurve[0]);
    volts3 = 0;
  }

}




PaulS

Code: [Select]
MyFile = SD.open("datafile.txt", FILE_WRITE);
if (!MyFile) {
    Serial.println ("ERROR OPEN SD CARD, DATA WOULD NOT BE STOREED!!");
  }

   MyFile = SD.open("datafile.txt", FILE_WRITE);
  if (MyFile) {

Why are you opening the file twice without closing it in between?

The art of getting good answers lies in asking good questions.

mehafelix


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy