Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days. On MLK Day no orders will be shipped.
0

Need Help with RTC on data logger
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Need Help with RTC on data logger

by tsans99 on Tue Nov 30, 2021 2:53 pm

Error.jpg
Error.jpg (61.36 KiB) Viewed 283 times



I'm trying to use the Data Logger with an Arduino Uno to write data to an SD card along with the date.

I wrote a simple sketch to test out the code and I'm not all that good at it and I get an error as seen in the attached file. I also am including the entire sketch and would appreciate any help. I don't know if there are any other errors beyond what shown so if anyone sees any please let me know.

Thanks,

Tom

Code: Select all | TOGGLE FULL SIZE
//Initialise LCD display

#include <SPI.h>        // Include SPI library (needed for the SD card)
#include <SD.h>         // Include SD library
#include "RTClib.h"     // Include RTC Library

File myFile;

//Setup Variables

  int data =1;
  float DateTime;
  int chipSelect = 10;  // chipselect pin for the SD card reader


  void setup()
{
 
  Serial.begin(9600);  //Start the serial connection
 
// Check for SD card initialization

  while (!Serial) {; // wait for serial port to connect. Needed for native USB port only
{
  Serial.print("Initializing SD card...");

  if (!SD.begin()) {
    Serial.println("initialization failed!");
    while (1);
}
  Serial.println("initialization done.");
}

void loop()
{


 

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(") ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print(data);
 



  // SD Card Write

 
//open the file.
myFile = SD.open("RTCtest.txt", FILE_WRITE);
 
//write to file
if (myFile)
{
Serial.print("Writing to RTCtest.txt...");
  myFile.print(now.year(), DEC);
  myFile..print('/');
  myFile..print(now.month(), DEC);
  myFile..print('/');
  myFile..print(now.day(), DEC);
  myFile.l.print(" (");
  myFile..print(") ");
  myFile..print(now.hour(), DEC);
  myFile..print(':');
  myFile..print(now.minute(), DEC);
  myFile..print(':');
  myFile..print(now.second(), DEC);
  myFile.print("Data  ,");
  myFile.println(data);
//close the file
  myFile.close();
 Serial.println("done.");
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening Weather.txt file");
}

 

}
Last edited by adafruit_support_bill on Tue Nov 30, 2021 3:07 pm, edited 1 time in total.
Reason: Please use [code] tags when posting code to the forums.

tsans99
 
Posts: 12
Joined: Thu Nov 18, 2021 4:17 pm

Re: Need Help with RTC on data logger

by adafruit_support_bill on Tue Nov 30, 2021 3:13 pm

As the error message says, you can't declare your loop() function inside of your setup function.

Your setup function has more '{' than '}' so as far as the compiler is concerned, it never ends. It is a lot easier to catch problems like these if you use consistent indenting in your code:

Code: Select all | TOGGLE FULL SIZE
   void setup()
   {

      Serial.begin(9600);  //Start the serial connection

      // Check for SD card initialization

      while (!Serial) {; // wait for serial port to connect. Needed for native USB port only
      {
         Serial.print("Initializing SD card...");

         if (!SD.begin())
         {
            Serial.println("initialization failed!");
            while (1);
         }
         Serial.println("initialization done.");
      }

      ///**** missing '}' here ***
      
      void loop()
      {


adafruit_support_bill
 
Posts: 82875
Joined: Sat Feb 07, 2009 10:11 am

Re: Need Help with RTC on data logger

by rpiloverbd on Wed Dec 01, 2021 6:17 am

This error appears when you do not close a second bracket. In your setup function, I can see that no. of '{' and No. of '}' s are not equal. Please correct it. Hopefully your code will compile.

rpiloverbd
 
Posts: 96
Joined: Mon Nov 29, 2021 8:13 am

Re: Need Help with RTC on data logger

by dastels on Wed Dec 01, 2021 8:29 am

The problem is that here is an extra opening brace:
Code: Select all | TOGGLE FULL SIZE
while (!Serial) {;

Dave

dastels
 
Posts: 8468
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help with RTC on data logger

by dastels on Wed Dec 01, 2021 8:32 am

Note that in the Arduino editor you can press Control/Command-T (also Autoformat in the tools menu) to format your code (well, indent it) consistently.

Dave

dastels
 
Posts: 8468
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help with RTC on data logger

by tsans99 on Wed Dec 01, 2021 11:35 am

I do appreciate all the help and it should be obvious I'm VERY new to this. I did some online studying but it wasn't enough to get the things I want done so I'm here for all the help I can get.

I fixed the bracket problem and it's something I should have seen but missed and used the Auto Format tool (who knew). Now the next problem seems to be that 'rtc' and 'now' were not declared in this scope. I've attached the new sketch and if anyone can take a quick look at it and see any other errors it will stop me from having to ask for help again.

Thanks to all,

Tom

Code: Select all | TOGGLE FULL SIZE
/*
  TAS Design 11-28-21
*/

//Initialise LCD display

#include <SPI.h>        // Include SPI library (needed for the SD card)
#include <SD.h>         // Include SD library
#include "RTClib.h"     // Include RTC Library

File myFile;

//Setup Variables

//int data =1;
// float DateTime;
//int chipSelect = 10;  // chipselect pin for the SD card reader
//int now

void setup()
{

  Serial.begin(9600);  //Start the serial connection

  // Check for SD card initialization

  while (!Serial) ; // wait for serial port to connect. Needed for native USB port only
  {
    Serial.print("Initializing SD card...");

    if (!SD.begin())
    {
      Serial.println("initialization failed!");
    }
    while (1);
  }
  Serial.println("initialization done.");

}
void loop()
{

  DateTime now = rtc.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(") ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print(data);

  // SD Card Write

  //open the file.
  myFile = SD.open("RTCtest.txt", FILE_WRITE);

  //write to file
  if (myFile)
  {
    Serial.print("Writing to RTCtest.txt...");
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(" (");
    myFile.print(") ");
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print("Data  ,");
    myFile.println(data);
    //close the file
    myFile.close();
    Serial.println("done.");
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("error opening RTCtest.txt file");
  }

}
Attachments
Test_RTC.ino
(1.97 KiB) Downloaded 4 times
Last edited by adafruit_support_bill on Wed Dec 01, 2021 11:51 am, edited 1 time in total.
Reason: Added code in-line. Please use [code] tags when posting code to the forums.

tsans99
 
Posts: 12
Joined: Thu Nov 18, 2021 4:17 pm

Re: Need Help with RTC on data logger

by dastels on Wed Dec 01, 2021 11:51 am

Yes, that's because you haven't declared them.

I see the declaration (and assignment) of the variable now in the loop() function. I'm guessing that the now that's it's complaining about is the now function you are calling on rtc... guessing because you didn't include the compiler output (which is pretty much required when asking about compiler errors). rtc isn't declated anywhere. You will need to do that near the top of the file (after the includes). The exact declaration will depend on the RTC that you have, but will be something like
Code: Select all | TOGGLE FULL SIZE
RTC_DS3231 rtc;

Unrelated, but the
Code: Select all | TOGGLE FULL SIZE
while( 1);
should, I believe, be inside the
Code: Select all | TOGGLE FULL SIZE
if (!SD.begin())
loop. You also don't need the brace delineated block following
Code: Select all | TOGGLE FULL SIZE
while (!Serial) ;

dastels
 
Posts: 8468
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help with RTC on data logger

by tsans99 on Wed Dec 01, 2021 12:34 pm

I made the corrections you suggested and added RTC_PCF8523 rtc; so now I get all the way through Verify/Compile with no errors. I'll try to run and hopefully it's successful.

Thanks,

Tom

tsans99
 
Posts: 12
Joined: Thu Nov 18, 2021 4:17 pm

Re: Need Help with RTC on data logger

by tsans99 on Wed Dec 01, 2021 2:16 pm

Well, no errors but it doesn't run.

It seems to hang-up writing the first line to the serial monitor - see screen shot - and never completes the write. Not unexpectedly, the file is not created on the SD card.

Any Ideas?

Tom
Attachments
Screen Shot.jpg
Screen Shot.jpg (35.67 KiB) Viewed 247 times

tsans99
 
Posts: 12
Joined: Thu Nov 18, 2021 4:17 pm

Re: Need Help with RTC on data logger

by adafruit_support_bill on Wed Dec 01, 2021 2:58 pm

Please post your modified code. If your setup still has this, then the behavior you describe would be expected.

Code: Select all | TOGGLE FULL SIZE

    if (!SD.begin())
    {
      Serial.println("initialization failed!");
    }
    while (1);

adafruit_support_bill
 
Posts: 82875
Joined: Sat Feb 07, 2009 10:11 am

Re: Need Help with RTC on data logger

by tsans99 on Wed Dec 01, 2021 3:12 pm

Latest Code

Code: Select all | TOGGLE FULL SIZE
/*
  TAS Design 11-28-21
*/

//Initialise LCD display

#include <SPI.h>        // Include SPI library (needed for the SD card)
#include <SD.h>         // Include SD library
#include "RTClib.h"     // Include RTC Library

File myFile;
RTC_PCF8523 rtc;
//Setup Variables

int data =1;


void setup()
{

  Serial.begin(9600);  //Start the serial connection

  // Check for SD card initialization

  while (!Serial) ; // wait for serial port to connect. Needed for native USB port only
  {
    Serial.print("Initializing SD card...");

    if (!SD.begin(10))
    {
      Serial.println("initialization failed!");

        while (1);
    }
 
  }
  Serial.println("initialization done.");

}
void loop()
{

  DateTime now = rtc.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(") ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print(data);

  // SD Card Write

  //open the file.
  myFile = SD.open("RTCtest.txt", FILE_WRITE);

  //write to file
  if (myFile)
  {
    Serial.print("Writing to RTCtest.txt...");
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(" (");
    myFile.print(") ");
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print("Data  ,");
    myFile.println(data);
    //close the file
    myFile.close();
    Serial.println("done.");
  }
  else
  {
    // if the file didn't open, print an error:
    Serial.println("error opening RTCtest.txt file");
  }

}
Attachments
Test_RTC.ino
(1.9 KiB) Downloaded 2 times
Last edited by adafruit_support_bill on Wed Dec 01, 2021 3:34 pm, edited 1 time in total.
Reason: Added code in-line. Please use [code] tags when posting code to the forums.

tsans99
 
Posts: 12
Joined: Thu Nov 18, 2021 4:17 pm

Re: Need Help with RTC on data logger

by adafruit_support_bill on Wed Dec 01, 2021 3:40 pm

Please use [code] tags when posting code to the forums so that people don't have to download your files in order to help. Just press on the [code] button and paste your code between the [code] tags.

You code is probably hanging because you are not calling rtc.begin() to initialize the PCF8523. See the example code in the library:
https://github.com/adafruit/RTClib/blob ... cf8523.ino

adafruit_support_bill
 
Posts: 82875
Joined: Sat Feb 07, 2009 10:11 am

Re: Need Help with RTC on data logger

by dastels on Wed Dec 01, 2021 3:51 pm

As Bill said, you're missing the initializatio9n of the RTC via a begin call. Almost all (and possibly all) Adafruit sensor libraries use a begin method for initialization. Something to keep in mind.

Please have a look at the library examples, specifically https://github.com/adafruit/RTClib/blob/master/examples/ds3231/ds3231.ino.

Reading the tutorial guides would avoid a lot of erros and time spent debugging as well: https://learn.adafruit.com/adafruit-ds3231-precision-rtc-breakout.

Dave

dastels
 
Posts: 8468
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.