Error with declaring something in the scope

in this code, I am having the error 'time_t was not declared in the scope" for context this is an Arduino nano, and I am connected to a DS 3231 through analog pins A4 and A5, I downloaded the DS 3231 library too so there's no problems with that, so I don't know why this error is happening.
heres my code:

void setup() {
  Serial.begin(115200);
  delay(200);
  pinMode(5, INPUT);
}

int sec_prev = 0;  // Global Variable

void loop() {
  int seconds, minutes, hours;
  time_t total_seconds = time(0);
  struct tm* ct = localtime(&total_seconds);

  seconds = ct->tm_sec;
  minutes = ct->tm_min;
  hours = ct->tm_hour;
>
  // Turn on the light from 3pm to 4pm
  if (hours == 15) {
    digitalWrite(5, HIGH));
    delay(180000);
  }
else
 digitalWrite(5, LOW));

  if (seconds != sec_prev) {
    sec_prev = seconds;

    if (hours < 10)
      Serial.print('0');
    Serial.print(hours);
    Serial.print(':');
    if (minutes < 10)
      Serial.print('0');
    Serial.print(minutes);
    Serial.print(':');
    if (seconds < 10)
      Serial.print('0');
    Serial.println(seconds);
  }
}

time_t structure declared in external time library.

You have to include in the code

#include <Time.h>

or

#include <TimeLib.h>

Perhaps you will need to install this libraries first.

I added the #include which I forgot to add and since it didn't work, I confirmed it has to do with an external library I don't have installed, do you have any suggestions on where to look for such a library, because the only places I've gone is through the DS 3231 or other RTC models libraries and I can't find one that fix's the problem.

Which library did you include? The sketch will compile (after correcting a few typing errors) using the standard time library ( #include <time.h> ).

Best thing is to look at the examples from the DS3231 library that you have installed, that will show the functions specific to that library.

Personally I like to use the Time library (by Paul Stoffregen) and the DS3232RTC library (by J Christensen), and yes that is DS3232, it also works with the DS3231.

time_t is not a standard type: so, potentially a #include is missing (to declare time_t).

Any non-standard typedef (like "time_t") needs a declaration (via #include). Any "new" type has to be declared before first use.

Just check: where is time_t declared and if you include the needed the file.

thank you for the solution, that error is fixed however when I try to define the DS 3231 as an object it doesn't work even though I downloaded the proper library for it, for example when I try:

#include <DS3231.h>
I know for a fact this should work because I have a DS 3231 and I have the library that goes with this example which supports the DS 3231 (obviously) and I'm including it before void setup so it should work, but it's not...
the error message I'm getting is "exit status 1, DS3231.h: No such file or directory"

Which specific DS3231 library did you download?

it's called "DS3231_RTC" by Andrew Wickert

Not sure why it does not work for you, I installed that library from the IDE's library manager and have no problems with the include statement.

to make sure I'm not doing anything wrong can you share the code you applied the include statement in or the example from the library. This will help me tell whether it's my computer to blame if it doesn't work or if it does work then I must be doing something wrong.

Your topic has been moved to a more suitable location on the forum as this has nothing to do with Avrdude, stk500 or Bootloader.

I just added the include line to the Blink Without Delay example sketch, the DS3231_RTC library has no example sketches. That library is the only library I have installed with the file DS3231.h

  1. List item
#include <DS3231.h>

I got it to work, I did a little error before that I fixed, thanks.

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