Invalid use of void expression

Hello all,

I've got a problem with using void function and i've got no idea where the problem comes from?

Here is the part of the code that is faulty:

#include <DS3231.h>
#include <SD.h> // for the SD card
#include <Wire.h>
DS3231  rtc(SDA, SCL);
#define CS_SD 2
File myFile;
void setup() {
  // put your setup code here, to run once:
rtc.begin();
while(true){
  digitalWrite(CS_SD,LOW); // enables the cs pin
 
  // If the file opened ok, write to it
    if (myFile) {
    Serial.println("Read:");
    // Reading the whole file
    while (myFile.available()) {
      Serial.write(myFile.read());
   }
    myFile.close();
  }
  else {
    Serial.println("error opening test.txt");
  } 
 
    digitalWrite(CS_SD,HIGH);
  }
}
void Rtc()
{
   rtc.getDOWStr();
  rtc.getDateStr();
rtc.getTimeStr();
}
void loop() {
   myFile = SD.open("opit7.txt", FILE_WRITE);
   myFile.print(Rtc());
   myFile.close();
}

Thanks for the help!

I've got a problem with using void function and i've got no idea where the problem comes from?

But I bet the compiler gave you a massive clue.
Care to share?

Also, you don't open your file in setup(), but you loop like you did.

which library is that?

#include <DS3231.h>

also copy and paste the error message you have

When there is an error in compiling, a button will appear in the lower right of the IDE window. it is labeled "copy error message". Copy the error message and paste into a new post. Paraphrasing error messages leaves out important information.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Okay, sorry guys i didnt think it was necessary to post the error message, but here it is:

Arduino: 1.8.9 (Windows 10), Board: "Arduino Uno"

In function 'void loop()':

sketch_feb02a:37:22: error: invalid use of void expression

myFile.print(Rtc());

^

exit status 1
invalid use of void expression

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

And the DS3231 is library for the RTC module which outputs day,time and date. My aim is to save that data into a file only 1 time.

Cheers.

Your RTC() function is declared void, therefor it does not return any value.

myFile.print(Rtc());

Is trying to write the non-existent return value of the RTC() function.

groundFungus:
Your RTC() function is declared void, therefor it does not return any value.

myFile.print(Rtc());

Is trying to write the non-existent return value of the RTC() function.

I've changed the name and still get the same error.

You've changed what name?

I've changed the name and still get the same error.

Can't help if we don't know what you did. Always post the new code so that we can keep up.

If you want to save output of rtc.getDOWStr(), rtc.getDateStr(), tc.getTimeStr() you need to write them separately. The RTC() function only calls those functions, it does not take on their values.

I've changed the name of the void function and i still get the same error.
I've also tried to leave only rtc.getDOWStr(); and i still get the same error.

Here is the part of the code where i changed the name of the void function.

void clocktime()
{
   rtc.getDOWStr();
  rtc.getDateStr();
rtc.getTimeStr();
}
void loop() {
   myFile = SD.open("opit7.txt", FILE_WRITE);
   myFile.print(clocktime());
   myFile.close();
}
1 Like

when you ask the compiler to do:  myFile.print(clocktime());
you are asking to print whatever is returned by the call to clocktime().. but since it does not return anything (its return type is 'void', ie nothing), there is nothing to print.

if you were to do a

Serial.print(rtc.getDOWStr());

then you would likely see something (and you would do the same with rtc.getDateStr() and rtc.getTimeStr())

Please do not post snippets. Post the whole program so that we do not have to assume that nothing else changed.

The clockTime() function is still not returning any value.

myFile.print(clocktime());

What, exactly, do you expect that line of code to do?

This makes more sense though I cannot test:

void clockTime()
{
   myFile.print(rtc.getDOWStr());
   myFile.print(rtc.getDateStr());
   myFile.print(rtc.getTimeStr());
}
void loop()
{
   myFile = SD.open("opit7.txt", FILE_WRITE);
   clockTime();
   myFile.close();
}

Similar to what groundFungus replied while I was writing it, but there are some differences so I'll post it anyway:

Changing the name of the function isn't going to help. The problem is that the clocktime function doesn't return anything, so there's nothing to print to myFile, thus the error.

I'll give a simplified example, here's a function named myFunction that returns a byte:

byte myFunction() {
  return 42;
}

Now I can use the function like this:

myFile.print(myFunction());

which is the equivalent of:

myFile.print(42);

But your clocktime function returns a void, so your code:

myFile.print(clocktime());

is the equivalent of:

myFile.print(void);

which is not valid code, and wouldn't be useful even if it were.

I guess you think your code is going to work something like this:

myFile.print(rtc.getDOWStr());
myFile.print(rtc.getDateStr());
myFile.print(rtc.getTimeStr());

but that's not how the Arduino programming language works.

Here is a tutorial on functions. that may help your understanding of functions and how they return values.

pert:
I guess you think your code is going to work something like this:

myFile.print(rtc.getDOWStr());

myFile.print(rtc.getDateStr());
myFile.print(rtc.getTimeStr());



but that's not how the Arduino programming language works.

This is actually the way it worked. I just deleted the void function and placed the quoted paragraphs in the void loop.

Thanks alot for the help!

OK, that's good, but I do hope you'll also take the time to learn how functions work. Even if you don't need the knowledge for this specific project, functions are incredibly useful.

pert:
OK, that's good, but I do hope you'll also take the time to learn how functions work. Even if you don't need the knowledge for this specific project, functions are incredibly useful.

Thanks for the tip. I will spend some time learning them. Thank you.