Data logger shield/timestamp

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
hginvt
 
Posts: 13
Joined: Wed Aug 29, 2018 9:42 pm

Re: Data logger shield/timestamp

Post by hginvt »

I've been struggling with and can't figure it out. The sketch/shield write data to the SD card if I comment out the IF Statement and allow control only by the delay command. It does not write to the SD card if I include the IF statement. Its does print to the serial port/screen as expected, but will only write to SD card without IF statement.

Code: Select all

// Work in Progress 27 Aug 2018
// Developing first data logging sketch
// read temperature sensor and log readings of deg C and deg F to SD card
// 28 August-- sucessfully write data to SD card, next add switch to control data logging
// 29 August-- switch added. Jumper Pin 3 to 3.3 volts to logg data, jumper to GRN to not log data
//               note: need to "re-set" Arduino after re-inserting SD card
            



#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define ONE_WIRE_BUS 7

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

 float Celcius=0;
 float Fahrenheit=0;
 
 int inputPin=3; // jumper Pin 3 to 3.3 volts to write data to SD card, to GRN to not write to SD

 int chipSelect = 4; // chipSlect pin for the SD card reader

 
 
 File myFirstData; //Data object you will write your sensor data to





 RTC_PCF8523 rtc;

 int x; //value of Mudulo divide
 int dlSwitch; // jumper to control logging of data 
 int dlSwitch2;
 int counter = 0;
 
 
void setup(void)
{

   // following line sets the RTC to the date & time this sketch was compiled
   rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

   // This following code would set the RTC with an explicit date & time, for example
   // January 21, 2014 at 3am you would call:
   // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
    
  Serial.begin(9600);
  sensors.begin();
  rtc.begin();
  
  pinMode(inputPin, INPUT);
 
  pinMode(10, OUTPUT); //Must declare 10 an output and reserve it
  SD.begin(4); //Initialize the SD card reader

    //Add for LCD 
    lcd.begin(20,4);
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.clear();
}

void loop(void)
{ 

 myFirstData=SD.open("My_Data.txt", FILE_WRITE);


  
  sensors.requestTemperatures(); 
  Celcius=sensors.getTempCByIndex(0);
  Fahrenheit=sensors.toFahrenheit(Celcius);


  DateTime now = rtc.now(); //get current date&time from rtc
 
//print time with leading zeros
  char printLineA[10]; // This will hold the Date
  char printLineB[8]; // This will hold the Time
  sprintf(printLineA,"%02d/%02d/%4d", now.month(), now.day(), now.year());
  sprintf(printLineB,"%02d:%02d:%02d", now.hour(), now.minute(), now.second());


// Display on LCD
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.setCursor(0,1); 
  lcd.print(printLineA);
  lcd.print(" ");
  lcd.print(printLineB);
  lcd.setCursor(13,2); 
  lcd.print(Celcius);
  lcd.print(" C");
  lcd.setCursor(13,3); 
  lcd.print(Fahrenheit);
  lcd.print(" F");

//create "mechcanical switch": possble way to control logging
dlSwitch = digitalRead(inputPin); // readcPin3 Hi or LOW

//create a counter: another possible way to control logging
counter = counter + 1;

//Use IF statement to control when to write to SD card
if ((now.second() == 00 || now.second() == 01))

{


//Print to Serial Port/screen TimeStamp and data
 //print timestamp
 Serial.print(printLineA);
 Serial.print(" ");
 Serial.print(printLineB);
 Serial.print(" ");

 // print data
 Serial.print(Celcius);
 Serial.print(", ");
 Serial.print(Fahrenheit);


//Mudulo Divide -- base it off Unix Time
// if resulant of Mudulo divide of Unix time by 60 is zero (or 1 accoumting for imprecision of timing)
// one minute has elapsed -- write to SD card
x = now.unixtime()%60;
Serial.print(", ");
Serial.print(x);
Serial.print(", ");
Serial.print(dlSwitch);
Serial.print(", ");
Serial.print(dlSwitch2);
Serial.print(", ");
Serial.print(counter);
// end Mudulo divide


Serial.println();


 
// Write to SD card



 
 Serial.print("  Log Record");


  
 myFirstData.print(printLineA);
 myFirstData.print(" ");
 myFirstData.print(printLineB);
   
 myFirstData.print(" ");
  myFirstData.print(Celcius);
  myFirstData.print(" ");
  myFirstData.println(Fahrenheit);
  myFirstData.close();


counter = 0;

}
// delay 304 for one second execution interval
  delay(1304);
}

Attachments

[The extension ino has been deactivated and can no longer be displayed.]

Last edited by adafruit_support_bill on Wed Sep 12, 2018 12:31 pm, edited 1 time in total.
Reason: added code in-line

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Data logger shield/timestamp

Post by adafruit_support_bill »

Please post a sample of the serial output with the IF statement enabled.

User avatar
hginvt
 
Posts: 13
Joined: Wed Aug 29, 2018 9:42 pm

Re: Data logger shield/timestamp

Post by hginvt »

I've uploaded a screen shot of the serial output with the IF statement operative and a copy/past of the output as well.

Thank you.

Code: Select all

09/12/2018 09:02:00 20.06, 68.11, 0, 1, 0, 12
  Log Record09/12/2018 09:03:00 20.12, 68.22, 0, 1, 0, 30
  Log Record09/12/2018 09:04:00 20.25, 68.45, 0, 1, 0, 30
  Log Record09/12/2018 09:05:00 20.25, 68.45, 0, 1, 0, 30
  Log Record09/12/2018 09:06:00 20.31, 68.56, 0, 1, 0, 30
  Log Record09/12/2018 09:07:00 20.31, 68.56, 0, 1, 0, 30
  Log Record09/12/2018 09:08:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:09:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:10:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:11:00 20.44, 68.79, 0, 1, 0, 30
  Log Record09/12/2018 09:12:00 20.44, 68.79, 0, 1, 0, 30
  Log Record09/12/2018 09:13:00 20.44, 68.79, 0, 1, 0, 30
  Log Record09/12/2018 09:14:00 20.44, 68.79, 0, 1, 0, 30
  Log Record09/12/2018 09:15:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:16:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:17:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:18:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:19:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:20:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:21:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:22:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:23:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:24:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:25:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:26:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:27:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:28:00 20.31, 68.56, 0, 1, 0, 30
  Log Record09/12/2018 09:29:00 20.31, 68.56, 0, 1, 0, 30
  Log Record09/12/2018 09:30:00 20.31, 68.56, 0, 1, 0, 30
  Log Record09/12/2018 09:31:00 20.31, 68.56, 0, 1, 0, 30
  Log Record09/12/2018 09:32:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:33:00 20.44, 68.79, 0, 1, 0, 30
  Log Record09/12/2018 09:34:00 20.50, 68.90, 0, 1, 0, 30
  Log Record09/12/2018 09:35:00 20.50, 68.90, 0, 1, 0, 30
  Log Record09/12/2018 09:36:00 20.50, 68.90, 0, 1, 0, 30
  Log Record09/12/2018 09:37:00 20.50, 68.90, 0, 1, 0, 30
  Log Record09/12/2018 09:38:00 20.50, 68.90, 0, 1, 0, 30
  Log Record09/12/2018 09:39:00 20.50, 68.90, 0, 1, 0, 30
  Log Record09/12/2018 09:40:00 20.44, 68.79, 0, 1, 0, 30
  Log Record09/12/2018 09:41:00 20.44, 68.79, 0, 1, 0, 30
  Log Record09/12/2018 09:42:00 20.44, 68.79, 0, 1, 0, 30
  Log Record09/12/2018 09:43:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:44:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:45:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:46:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:47:00 20.37, 68.68, 0, 1, 0, 30
  Log Record09/12/2018 09:48:00 20.31, 68.56, 0, 1, 0, 30
  Log Record
Attachments
SampleOfSerialPortWithIFStatement.txt
(2.71 KiB) Downloaded 26 times
SampleOfSerialOutWithIFStatement.JPG
SampleOfSerialOutWithIFStatement.JPG (273.29 KiB) Viewed 233 times
Last edited by adafruit_support_bill on Wed Sep 12, 2018 12:30 pm, edited 1 time in total.
Reason: posted output in-line

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Data logger shield/timestamp

Post by adafruit_support_bill »

When posting code or text-data, please post it in-line using the

Code: Select all

 button.  That is much easier to deal with than attachments.

Your code appears to be printing the data every minute as it should.  I don't see anywhere that you close or flush the file, so the directory entry for the file is probably not getting written or updated.

User avatar
hginvt
 
Posts: 13
Joined: Wed Aug 29, 2018 9:42 pm

Re: Data logger shield/timestamp

Post by hginvt »

Thank you for your support.

I have uploaded the file again with line numbers. I have had a close statement at line 170. I did try adding a flush statement, to no success.

What totally baffles me is that when I comment out the IF statement it works perfectly, logging the intended data every 2 seconds. What is it about an IF statement that can cause the sketch to not print the data to the SD card?

[The extension ino has been deactivated and can no longer be displayed.]


User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Data logger shield/timestamp

Post by adafruit_support_bill »

When posting code or text-data, please post it in-line using the

Code: Select all

 button. That is much easier to deal with than attachments.[/quote]

User avatar
hginvt
 
Posts: 13
Joined: Wed Aug 29, 2018 9:42 pm

Re: Data logger shield/timestamp

Post by hginvt »

I thought I had: My apologies.

I have got it to work. The command line defining the text file and opening the SD card to write ( myFirstData=SD.open("My_Data.txt", FILE_WRITE);) needed to be within the IF statement testing if it was a new minute. I don't understand why following the flow of the program, but when I moved the command from the general loop to within that IF statement the sketch writes the data to the file minutely.

Thanks for al your support

User avatar
bps
 
Posts: 45
Joined: Fri May 13, 2011 11:16 am

Re: Data logger shield/timestamp

Post by bps »

I'm having a time and date problem as well. I am using an 1141 datalogger shield with an actual UNO powered through a USB port. I am running the pfc8523 sketch which I have not changed in any way. I get a date that is about 11 months and some days ahead and, if I am reading things correctly, I get a return saying that today is Monday when it is actually Thursday. Could you suggest what the problem might be. I also unplugged the UNO and removed the coin cell for about 15 seconds before rebooting.

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Data logger shield/timestamp

Post by adafruit_support_bill »

I am running the pfc8523 sketch which I have not changed in any way.
You need remove & replace the coin cell and uncomment the rtc.adjust line in setup() to initialize the time.

Code: Select all

  if (! rtc.initialized()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

User avatar
bps
 
Posts: 45
Joined: Fri May 13, 2011 11:16 am

Re: Data logger shield/timestamp

Post by bps »

I have used the same coin cell on another datalogger (an older one) and the rtc on the datalogger gives the correct information. The data adjust line was already uncommented. I could not manual set the date and time using the other function.

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Data logger shield/timestamp

Post by adafruit_support_bill »

Please post photos showing your soldering and any connections to the shield.

User avatar
bps
 
Posts: 45
Joined: Fri May 13, 2011 11:16 am

Re: Data logger shield/timestamp

Post by bps »

I'll send you pix tomorrow

User avatar
bps
 
Posts: 45
Joined: Fri May 13, 2011 11:16 am

Re: Data logger shield/timestamp

Post by bps »

I attach a picture. I also attach the output from the Serial Monitor. BTW, I installed a new battery.

RTC is NOT running!
2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31

2165/165/165 (Thursday) 165:165:85
since midnight 1/1/1970 = 1580946385s = 18297d
now + 7d + 30s: 2020/2/13 12:16:31
Attachments
sypniewski-1141.jpg
sypniewski-1141.jpg (177.44 KiB) Viewed 185 times

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Data logger shield/timestamp

Post by adafruit_support_bill »

2165/165/165 (Thursday) 165:165:85
That indicates an i2c communication problem. Is the UNO you are using an R3, or an earlier version? If you are not sure, post a photo of your UNO.

User avatar
bps
 
Posts: 45
Joined: Fri May 13, 2011 11:16 am

Re: Data logger shield/timestamp

Post by bps »

I think it is an earlier version. I believe that I also have an R3. If it is any help help, I tried this with a sparkfun red board with the same results.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”