Need Help with RTC on data logger

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tsans99
 
Posts: 12
Joined: Thu Nov 18, 2021 4:17 pm

Need Help with RTC on data logger

Post by tsans99 »

Error.jpg
Error.jpg (61.36 KiB) Viewed 436 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

//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.

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

Re: Need Help with RTC on data logger

Post by adafruit_support_bill »

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

	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()
		{


User avatar
rpiloverbd
 
Posts: 198
Joined: Mon Nov 29, 2021 8:13 am

Re: Need Help with RTC on data logger

Post by rpiloverbd »

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.

User avatar
dastels
 
Posts: 15655
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help with RTC on data logger

Post by dastels »

The problem is that here is an extra opening brace:

Code: Select all

while (!Serial) {;
Dave

User avatar
dastels
 
Posts: 15655
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help with RTC on data logger

Post by dastels »

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

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

Re: Need Help with RTC on data logger

Post by tsans99 »

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

/*
  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

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

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.

User avatar
dastels
 
Posts: 15655
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help with RTC on data logger

Post by dastels »

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

RTC_DS3231 rtc;
Unrelated, but the

Code: Select all

while( 1);
should, I believe, be inside the

Code: Select all

if (!SD.begin())
loop. You also don't need the brace delineated block following

Code: Select all

while (!Serial) ;

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

Re: Need Help with RTC on data logger

Post by tsans99 »

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

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

Re: Need Help with RTC on data logger

Post by tsans99 »

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 400 times

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

Re: Need Help with RTC on data logger

Post by adafruit_support_bill »

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

Code: Select all


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

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

Re: Need Help with RTC on data logger

Post by tsans99 »

Latest Code

Code: Select all

/*
  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

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

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.

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

Re: Need Help with RTC on data logger

Post by adafruit_support_bill »

Please use

Code: Select all

 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/master/examples/pcf8523/pcf8523.ino

User avatar
dastels
 
Posts: 15655
Joined: Tue Oct 20, 2015 3:22 pm

Re: Need Help with RTC on data logger

Post by dastels »

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 ... ds3231.ino.

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

Dave

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

Return to “Arduino”