RTC time condition not respected by sketch program

when I try and load this sketch the LED I set the time to 00:59 which is not 3:00 (when I coded the led to light up in the if statement) but the Arduino follows whatever time I make it and goes straight to the commands in the if statement which would make the led light up but that would make no sense since again it is not 3:00 and in this code I used 00:59 but it still lit up, any suggestions on this problem?
@sterretje


#include <time.h>
 #include <Wire.h>
 #include <DS3231.h>
 DS3231 myRTC;
 //just in case I want to set up an alarm of some kind with more complex variables other than hours than I would send it to myRTC
void setup() {
  Wire.begin();
  Serial.begin(115200);
  delay(200);
  pinMode(LED_BUILTIN, OUTPUT);
 
}
int sec_prev = 0;  // Global Variable
  int hour = 0, minute = 59, second = 0;

void loop() {
  time_t total_second = time(0);
  struct tm* ct = localtime(&total_second);
if (second > 59){
    time_t total_minutes = time(0);
  struct tm* ct = localtime(&total_minutes);
}
if (minute > 59){
   time_t total_hours = time(0);
  struct tm* ct = localtime(&total_hours);
}
else {
   time_t total_seconds = time(0);
  struct tm* ct = localtime(&total_seconds);
}
     if (hour = 15) {
 Serial.print('INSERT NEEDLE');
 delay (2000);
    digitalWrite(LED_BUILTIN, HIGH);
  }
else 
 digitalWrite(LED_BUILTIN, LOW);

  second = ct->tm_sec;
  minute = ct->tm_min;
  hour = ct->tm_hour;

  // Turn on the light from 3pm to 4pm

  if (second != sec_prev) {
    sec_prev = second;
    if (hour < 10)
      Serial.print('0');
    Serial.print(hour);
    Serial.print(':');
    if (minute < 10)
      Serial.print('0');
    Serial.print(minute);
    Serial.print(':');
    if (second < 10)
      Serial.print('0');
    Serial.println(second);
     if(second==sec_prev+1 || (sec_prev==59 && second==0));
  }
}

and I included the DS3231 library

I suggest that you contact a moderator by flagging post #23 and ask to move post #23 to a new topic in the programming section. It's not related to your original upload problem.

  1. Click the flag under the post
  2. Click "somethinge else"
  3. Type your request
  4. Click the Message button.

I have split out the unrelated part of the previous topic to a dedicated topic as requested.

Best wishes for success with your project @t-dog-69

These lines don't seem right? How can the same function call be used to represent seconds, minutes and hours?

I don't really understand your logic either. There is no time where seconds > 59 or minutes > 59?

If you just want the LED on between 3pm and 4pm then you could do somethign like:

void loop(){
	time_t time = time(0);
	struct tm* localtime = localtime(&time);
	if(localtime->tm_hour == 15){
		digitalWrite(LED_BUILTIN, HIGH);
	}else{
		digitalWrite(LED_BUILTIN, LOW);
	}
}

for these lines of the code:

time_t time = time(0);
struct tm* localtime = localtime(&time);

it says time and local time cannot be used as a function, getting this error message:

Arduino: 1.8.19 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Thecht\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Thecht\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Thecht\Documents\Arduino\libraries -fqbn=arduino:avr:nano:cpu=atmega328old -vid-pid=1A86_7523 -ide-version=10819 -build-path C:\Users\Thecht\AppData\Local\Temp\arduino_build_112499 -warnings=none -build-cache C:\Users\Thecht\AppData\Local\Temp\arduino_cache_63506 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Users\Thecht\Documents\Arduino\sketch_nov30a\sketch_nov30a.ino

C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Thecht\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Thecht\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Thecht\Documents\Arduino\libraries -fqbn=arduino:avr:nano:cpu=atmega328old -vid-pid=1A86_7523 -ide-version=10819 -build-path C:\Users\Thecht\AppData\Local\Temp\arduino_build_112499 -warnings=none -build-cache C:\Users\Thecht\AppData\Local\Temp\arduino_cache_63506 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=C:\Program Files (x86)\Arduino\hardware\tools\avr -verbose C:\Users\Thecht\Documents\Arduino\sketch_nov30a\sketch_nov30a.ino

Using board 'nano' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr

Using core 'arduino' from platform in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr

Detecting libraries used...

"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_112499\\sketch\\sketch_nov30a.ino.cpp" -o nul

Alternatives for Wire.h: [Wire@1.0]

ResolveLibrary(Wire.h)

  -> candidates: [Wire@1.0]

"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_112499\\sketch\\sketch_nov30a.ino.cpp" -o nul

Alternatives for DS3231.h: [DS3231_RTC@1.1.0 DS3231@1.1.0]

ResolveLibrary(DS3231.h)

  -> candidates: [DS3231_RTC@1.1.0 DS3231@1.1.0]

"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Thecht\\Documents\\Arduino\\libraries\\DS3231" "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_112499\\sketch\\sketch_nov30a.ino.cpp" -o nul

"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Thecht\\Documents\\Arduino\\libraries\\DS3231" "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src\\Wire.cpp" -o nul

"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Thecht\\Documents\\Arduino\\libraries\\DS3231" "C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src\\utility\\twi.c" -o nul

"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Thecht\\Documents\\Arduino\\libraries\\DS3231" "C:\\Users\\Thecht\\Documents\\Arduino\\libraries\\DS3231\\DS3231.cpp" -o nul

Generating function prototypes...

"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Thecht\\Documents\\Arduino\\libraries\\DS3231" "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_112499\\sketch\\sketch_nov30a.ino.cpp" -o "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_112499\\preproc\\ctags_target_for_gcc_minus_e.cpp"

"C:\\Program Files (x86)\\Arduino\\tools-builder\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_112499\\preproc\\ctags_target_for_gcc_minus_e.cpp"

Compiling sketch...

"C:\\Program Files (x86)\\Arduino\\hardware\\tools\\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO -DARDUINO_ARCH_AVR "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\cores\\arduino" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs" "-IC:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\libraries\\Wire\\src" "-IC:\\Users\\Thecht\\Documents\\Arduino\\libraries\\DS3231" "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_112499\\sketch\\sketch_nov30a.ino.cpp" -o "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_112499\\sketch\\sketch_nov30a.ino.cpp.o"

C:\Users\Thecht\Documents\Arduino\sketch_nov30a\sketch_nov30a.ino: In function 'void loop()':

sketch_nov30a:17:25: error: 'time' cannot be used as a function

     time_t time = time(0);

                         ^

sketch_nov30a:18:41: error: 'localtime' cannot be used as a function

   struct tm* localtime = localtime(&time);

                                         ^

sketch_nov30a:26:12: error: 'ct' was not declared in this scope

   second = ct->tm_sec;

            ^~

Multiple libraries were found for "DS3231.h"

 Used: C:\Users\Thecht\Documents\Arduino\libraries\DS3231

 Not used: C:\Users\Thecht\Documents\Arduino\libraries\DS3231_RTC

Using library Wire at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire 

Using library DS3231 at version 1.1.0 in folder: C:\Users\Thecht\Documents\Arduino\libraries\DS3231 

exit status 1

'time' cannot be used as a function


however, when I delete the useless code that you mentioned it compiles just fine without adding your part of the code. However, I am still facing the same problem with the Arduino not following the if condition and instead going straight to the command within the if condition so no matter what time I set it the LED lights up
@sterretje

The if statement doesn't work because of the single =

hour = 15 means assign hour the value 15
hour == 15 means does hour equal 15

Oh, I didn’t notice that

CONFIDENTIALITY NOTICE: The contents of this email message and any attachments are intended solely for the addressee(s) and may contain confidential and/or privileged information. If you are not the intended recipient of this message or their agent, or if this message has been addressed to you in error, please immediately alert the sender by reply email and then delete this message and any attachments. If you are not the intended recipient, you are hereby notified that any use, dissemination, copying, or storage of this message or its attachments is strictly prohibited. Email received by or sent to school district officials/employees is subject to the Open Public Records Act [OPRA]. Consider alternate avenues of communication should you have concerns about the contents of your email being read by someone other than the person(s) you are contacting.

:rofl: :rofl: :rofl:

the if statement still doesn't work even after I put == instead of =, now I'm really confused

Print the value that isn't working in the if statement, right before you use it in the if statement.

You may get a surprise.

    Serial.print(" hour = "); Serial.println(hour);
    if (hour == 15) {
       Serial.print('INSERT NEEDLE');

Also I see
  time_t total_second = time(0);

// and

  struct tm* ct = localtime(&total_minutes);

But I see nothing in the sketch that could know what time it is. What "time.h" library are you using exactly? Does it use an RTC, the one you make no direct use of?

a7

yes, it is connected to a ds 3231 and the time.h library is the DS 3231 library that I downloaded and included in my sketch. And btw your edit to the sketch did not work

print the hour value as said in post #11

Did you try to run examples from the Time library? Are them give you a correct time from RTC?

when I try to run the example sketches for my time library it doesn't work becuase it doesn't recognize some of the functions even though I have the library installed and I did print the hour value and it still didn't work

There is a C library <time.h>.

Do you intend to use that instead of the Arduino TimeLib.h?

Which library are you using for the ds3231. There are several with the same name.

In that case why are you surprised that your sketch doesn't work too?
If the examples from the library do not work for you, this may mean that your RTS is incorrectly connected or doesn't work.
You should first ensure that the examples work - and then transfer this code to your sketch

Please show the output in the forum

the 1.0.7 ds3231 by Andrew wickert

Hi, @t-dog-69
DID YOU TRY THE ADDED CODE IN POST # 11?
Please answer this question. (In a sentence, not just yes or no!)

Have you got the baud rate in the IDE Serial Monitor set to 115200 baud.
Please answer this question. (In a sentence, not just yes or no!)

Thanks.. Tom... :smiley: :+1: :coffee: :coffee: :coffee: :coffee: :australia:

I did try the added code in post #11 and it DID NOT work. I DID change the baud rate in the IDE Serial Monitor to 115200