I cant figure out how to print my date and time using RTC ds 3234 and Micro SD card adapter

I just cant seem to figure out how to print it i keep getting this error from my code:

C:\Users*my name*\Documents\code\send help 2\send help 2.ino: In function 'void loop()':
C:\Users*my name*\Documents\code\send help 2\send help 2.ino:104:29: error: invalid use of void expression
dataFile.print(printTime());
^

exit status 1

Compilation error: invalid use of void expression

Code(Send help 2):

#include <SPI.h>
#include <SparkFunDS3234RTC.h>


#define DS13074_CS_PIN 10 // DeadOn RTC Chip-select pin
#define INTERRUPT_PIN 2 // DeadOn RTC SQW/interrupt pin (optional)

#include <SD.h> 

File dataFile; 

const int chipSelect = 4; //SD card CS pin connected to pin 10 of Arduino

void setup() 
{
  // Use the serial monitor to view time/date output
  Serial.begin(9600);
#ifdef INTERRUPT_PIN // If using the SQW pin as an interrupt
  pinMode(INTERRUPT_PIN, INPUT_PULLUP);
#endif

  rtc.begin(DS13074_CS_PIN);

  rtc.autoTime();

  rtc.update();

  rtc.enableAlarmInterrupt();

  rtc.setAlarm1(30);

  rtc.setAlarm2(rtc.minute() + 1);

  // setup for the SD card
  Serial.print("Initializing SD card...");

  if(!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
    
  //open file
  dataFile = SD.open("LOGDATA.txt", FILE_WRITE);

  // if the file opened ok, write to it:
  if (dataFile) {
    Serial.println("File opened ok");
    // print the headings for our data
    dataFile.println("Date,Time");
  }
 dataFile.close();

  Serial.println("CLEARDATA"); //clears up any data left from previous projects
  Serial.println("LABEL,Date,Logging Time,Time,Temperature,Light Intensity %, POT %"); //always write LABEL, to indicate it as first line
  Serial.println("RESETTIMER");
}


void loop() 
{
  static int8_t lastSecond = -1;
  
  // Call rtc.update() to update all rtc.seconds(), rtc.minutes(),
  // etc. return functions.
  rtc.update();

  if (rtc.second() != lastSecond) // If the second has changed
  {
    printTime(); // Print the new time
    
    lastSecond = rtc.second(); // Update lastSecond value
  } 

  // Check for alarm interrupts
#ifdef INTERRUPT_PIN
  // Interrupt pin is active-low, if it's low, an alarm is triggered
  if (!digitalRead(INTERRUPT_PIN))
  {
#endif
    // Check rtc.alarm1() to see if alarm 1 triggered the interrupt
    if (rtc.alarm1())
    {
      Serial.println("ALARM 1!");
      // Re-set the alarm for when s=30:
      rtc.setAlarm1(30);
    }
    // Check rtc.alarm2() to see if alarm 2 triggered the interrupt
    if (rtc.alarm2())
    {
      Serial.println("ALARM 2!");
      // Re-set the alarm for when m increments by 1
      rtc.setAlarm2(rtc.minute() + 1, rtc.hour());
    }
#ifdef INTERRUPT_PIN
  }
#endif

  dataFile = SD.open("LOGDATA.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
  dataFile.print(printTime());
}
}
void printTime()
{
  Serial.print(String(rtc.hour()) + ":"); // Print hour
  if (rtc.minute() < 10){
    Serial.print('0'); // Print leading '0' for minute
  Serial.print(String(rtc.minute()) + ":"); // Print minute
  }
  if (rtc.second() < 10){
    Serial.print('0'); // Print leading '0' for second
  Serial.print(String(rtc.second())); // Print second
}
  if (rtc.is12Hour()) // If we're in 12-hour mode
  {
    // Use rtc.pm() to read the AM/PM state of the hour
    if (rtc.pm()) Serial.print(" PM"); // Returns true if PM
    else Serial.print(" AM");
  }
  
  Serial.print(" | ");

  // Few options for printing the day, pick one:
  Serial.print(rtc.dayStr()); // Print day string
  //Serial.print(rtc.dayC()); // Print day character
  //Serial.print(rtc.day()); // Print day integer (1-7, Sun-Sat)
  Serial.print(" - ");
#ifdef PRINT_USA_DATE
  Serial.print(String(rtc.month()) + "/" +   // Print month
                 String(rtc.date()) + "/");  // Print date
#else
  Serial.print(String(rtc.date()) + "/" +    // (or) print date
                 String(rtc.month()) + "/"); // Print month
#endif
  Serial.println(String(rtc.year()));        // Print year
}

You declared pirintTme() will return nothing (void). Did you want printTime() to return a value for dataFile.print()?

Ohhh so thats what void does... I just need to print date and time and I just thought well my function printTime() does that already so maybe a can just plug it here.

how else can I define a function?

Your function PrintTime() prints to the Serial interface by using function-calls like

Serial.print(String(rtc.hour()) + ":");

these function-calls

only

print to the Serial interface
How should your SD-card get any information from a function-call that is connected to IO-pins 0 and 1 and does bitbanging LOW/HIGH on the IO-pins 0 and 1???

This is like you call a friend on the phone talking to him and you expect the words spoken to be printed out with a printer on a sheet of paper

https://docs.arduino.cc/learn/programming/functions

Yes, your function printTime() do that, but it prints data to the Serial, not to file.

So like i cant print it to my txt file? I was just thinking maybe i could just data.print rtc.hour but it turns out as you said that dont work. Did I just waste money? Theres nothing i can follow in regards to my specific model so...

Why not? - you can, but not with this function.
You should to program a new function for it.
Something similar to your function printTime(), but now for printing to the file rather than Serial

This is a very quick but false conclusion without analysing what is really going on.

@rllybro0112
You really should learn more about how programming works and what the different names inside a code mean

Serial specifies serial interface
You don't have to delcare it expliciit because it is that one standard communication-channel that is used all the time

this line

File dataFile; 

declares:

(Me the writer of the code) want to have an "object" of type File
and the name for this object shall be "dataFile"
.
.
this line

File myAlarmDateTime_File; 

declares:

Me (the writer of the code) want to have an "object" of type File
and the name for this object shall be "myAlarmDateTime_File"

And for "printing" into the file you have to specifiy that you want to print into the file
by coding

dataFile.println("Date,Time");

The name "dataFile" specifies print into my "object" with name "dataFile"

Again: I want to emphasise:
You really, really really should learn the basics how objects and functions work

Your way of "figuring out" seems to be
I got the quick idea of modifying this, modifying that, modifying this, modifying that,etc., etc., etc., etc. etc, ......
and after hours of modifying almost randomly you go asking in the forum.

To be not misunderstood: It is really OK to ask in the forum. You should always ask in the forum if you ran out of ideas what might be the problem.

But there is a general advise of learning the basics. This will take some time but will save you much more time by proceeding faster because

  • you don't waste time by almost randomly modifying code
  • you don't have to wait for answers because you don't have to ask so many basic things any more.

best regards Stefan

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.