Go Down

Topic: Data does not store on SD CCard (Read 351 times) previous topic - next topic

DrkKpr

Aug 24, 2019, 05:31 am Last Edit: Aug 24, 2019, 03:53 pm by DrkKpr
Total amateur here needing some help. This is my first post so please be patient while I get the hang of things, and please let me know if I need to post this elsewhere or have started a duplicate post.

This code I've pieced together measures temp and turbidity for water quality testing. I'm trying to store the readings onto the data logger/sd card but the data either does not store or intermittently only stores the temperature to the sd card file. The code is working otherwise, just having a problem with the data storage into the SD Card. The headers display in the saved SD file, just not data. (I'm sure this code is laughable, again.. I'm new  :D . Thanks!

arduino uno r3

HiLetgo Data Logger shield - https://www.amazon.com/gp/product/B00PI6TQWO/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1

LCD keypad shield - https://www.amazon.com/gp/product/B06XDMRHWK/ref=ppx_yo_dt_b_asin_title_o02_s00?ie=UTF8&psc=1

Code: [Select]
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal.h>
//RTC and DL shield
#include <RTClib.h>
#include <SPI.h>
#include <SD.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//Keypad Test Begin


//Keypad Test End

#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempProbe = { 0x28, 0x03, 0xD0, 0x79, 0x97, 0x21, 0x03, 0x97 };
RTC_DS1307 RTC;                        // define the Real Time Clock object
const int chipSelect = 10;              //CS pin of your data logger shield.Maybe not yours!!
File Logfile;                          //Name of the file

//holds the latest temp reading
//turbidity id
int sensorPin = A1;
float volt;
float ntu;
//temp sensor hold latest temp reading
float tempF;

//------


void setup(void)
{

//pinMode(10 , OUTPUT);           //For some data logger shields.Uncomment if you need
 
SD.begin(chipSelect);            //Initialize the libraries
Wire.begin();
RTC.begin();
 
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();

Logfile=SD.open("Logfile.csv",FILE_WRITE);                //Open and write once, just for headers
Logfile.println(" Date/Time       NTU        Temp");    //Print headers(not saved yet)
Logfile.close();                                        //Print saved

// set the resolution to 10 bit (good enough?)
sensors.setResolution(tempProbe, 10);

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
//set the cursor to first row, first character
lcd.setCursor(0,0);
//show which version of software it is running
lcd.print("Temp");

}

void printTempSerial(float temp)
{//sends temp info to serial port
Serial.print("Probe temperature is: ");
if (temp < -100) {
Serial.print("Error getting temperature");
} else {
Serial.print("F: ");
Serial.print(temp);
}
Serial.print("\n\r\n\r");
}



void LCDTemp(float temp)
{//send temp info to LCD shield
//first character, second row
lcd.setCursor(0,1);

if (temp < -100) {
lcd.print("Error: check connection");
} else {
lcd.print (temp);
lcd.print ("F");
//lcd.setCursor(7,1);
//lcd.print(volt);
   // lcd.print(" V");

    lcd.setCursor(10,1);
    lcd.print(ntu);
    lcd.setCursor (10,0);
    lcd.print("NTU");
}
}

void loop(void)
{
// TEST FOR DATA LOGGER

DateTime now = RTC.now();        // Clock call


if(now.second()==00){          //Sample every minute

 Logfile=SD.open("Logfile.csv",FILE_WRITE);       

 now = RTC.now();                 // Print date and time

Logfile.print(now.year(), DEC);
Logfile.print("/");
Logfile.print(now.month(), DEC);
Logfile.print("/");
Logfile.print(now.day(), DEC);
Logfile.print(" ");
Logfile.print(now.hour(), DEC);
Logfile.print(":");
Logfile.print(now.minute(), DEC);
Logfile.print(":");
Logfile.print(now.second(), DEC);
Logfile.print("  ");              //Space beween date/time and temp
Logfile.close();                  //Save date and time

  int sensorValue = analogRead(A1);
  sensors.requestTemperatures(); // Command all devices on bus to read temperature
 
  Logfile=SD.open("Logfile.csv",FILE_WRITE); //Print temp
  //If you want fahrenheit, use(sensors.getTempFByIndex(X)
 Logfile.print(sensors.getTempFByIndex(0));
 Logfile.print("F");
 Logfile.println();
 Logfile.println(sensorValue);
 Logfile.println ( "ntu" );
 Logfile.println();
 
 Logfile.close();
}

delay(2000);                        //One data per second
//****}

// END TEST FOR DATA LOGGER

//****{
 

 int x;
 x = analogRead (0);
 lcd.setCursor(10,1);
 if (x < 60) {
   lcd.print ("Right ");
 }
 else if (x < 200) {
   lcd.print ("Up    ");
 }
 else if (x < 400){
   lcd.print ("Down  ");
 }
 else if (x < 600){
   lcd.print ("Left  ");
 }
 else if (x < 800){
   lcd.print ("Select");
 }


  int sensorValue = analogRead(A1);
  float voltage = sensorValue * (5.0 / 1024.0);

  Serial.println ("Sensor Output (V):");
  Serial.println (voltage);
  Serial.println();


  // read the input on analog pin 0:
  //int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  Serial.println ( "ntu" );
  Serial.println();
 
//gets all temp probes on bus (i.e. pin 3)
sensors.requestTemperatures();
//get the temp of a specific sensor on bus by unique probe Id
tempF = sensors.getTempF(tempProbe);
//send temp over serial debug
printTempSerial(tempF);
//send temp to LCD display
LCDTemp(tempF);
delay(2000);

//Test for Turbidity

 volt = 0;
    for(int i=0; i<800; i++)
   
        volt += ((float)analogRead(sensorPin)/1023)*5;
   
    volt = volt/800;
    volt = (volt,1);
    if(volt < 2.5){
      ntu = 3000;
    }else{
      ntu = -1120.4*square(volt)+5742.3*volt-4353.8;
    }
}
float round_to_dp( float in_value, int decimal_place )
{
float multiplier = powf( 10.0f, decimal_place );
  in_value = roundf( in_value * multiplier ) / multiplier;
  return in_value;

//End test for turbidity

//wait 2 seconds to next reading
delay(2000);
}

wildbill

I suspect that your delays are causing a problem such that seconds is not zero when you check it. You can prove it by removing that if and unconditionally write to the log on each iteration of loop.

If that starts writing to the file, adopt the blink without delay technique to control when you write data i.e. use millis to see if enough time has elapsed since your last write for it to be time to do it again.

DrkKpr

Sorry for the delay. I removed the delays as you suggested and it did not make a difference. I ran the sd card check sketch again to be sure the card and being found and it still shows ok. Scratching my head here..

wildbill

The only thing I can see in your code that could prevent writing to the file is this:
Code: [Select]

if(now.second()==00){          //Sample every minute


Get rid of it as a test.

sterretje

After opening the file, you have to check if SD.open() actually succeeded.

Not related to the problem, but what is below supposed to do?
Code: [Select]
volt = (volt,1);

If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up