RTC time condition not respected by sketch program

even when I set it as a global variable, I get this error though:

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_515631 -warnings=none -build-cache C:\Users\Thecht\AppData\Local\Temp\arduino_cache_848073 -prefs=build.warn_data_percentage=75 -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 -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 -verbose C:\Users\Thecht\Documents\Arduino\clone_1_LED_of_Moduel_clock\clone_1_LED_of_Moduel_clock.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_515631 -warnings=none -build-cache C:\Users\Thecht\AppData\Local\Temp\arduino_cache_848073 -prefs=build.warn_data_percentage=75 -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 -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 -verbose C:\Users\Thecht\Documents\Arduino\clone_1_LED_of_Moduel_clock\clone_1_LED_of_Moduel_clock.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_515631\\sketch\\clone_1_LED_of_Moduel_clock.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_515631\\sketch\\clone_1_LED_of_Moduel_clock.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_515631\\sketch\\clone_1_LED_of_Moduel_clock.ino.cpp" -o nul

Using cached library dependencies for file: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src\Wire.cpp

Using cached library dependencies for file: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src\utility\twi.c

Using cached library dependencies for file: C:\Users\Thecht\Documents\Arduino\libraries\DS3231\DS3231.cpp

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_515631\\sketch\\clone_1_LED_of_Moduel_clock.ino.cpp" -o "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_515631\\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_515631\\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_515631\\sketch\\clone_1_LED_of_Moduel_clock.ino.cpp" -o "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_515631\\sketch\\clone_1_LED_of_Moduel_clock.ino.cpp.o"

clone_1_LED_of_Moduel_clock:12:1: error: 'secondsAfterMidnight' does not name a type

 secondsAfterMidnight = 3600UL * hour + 60 * minute + second;

 ^~~~~~~~~~~~~~~~~~~~

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

'secondsAfterMidnight' does not name a type

and with the variable hours, are you sure the rtc automatically correlates that with the current time in hours? Hear me out, I know this sounds very stupid but I'm truly considering whether there an error to it because even after I give the time variable (hour, minute, second) value and I use this if condition,

if hour >=5 

at 5:00PM (17:00) it doesn't go off; I know I asked this over 20 times and this is coming out annoying to you at this point because of my lack of knowledge on the subject of RTC's. However, this error ties into the seocondsAfterMidnight because if and when I set it as a global variable, the value of the function requires the value of other variables like hour and minute which is the original problem I had.

Please learn to post your updated sketch after you have made changes and (still) get errors.

It's the exact same thing but instead I declared

 unsigned long secondsAfterMidnight;
  secondsAfterMidnight = 3600UL * hour + 60 * minute + second;

in between the setup function and loop function:

#include <time.h>
 #include <Wire.h>
 #include <DS3231.h>
 DS3231 myRTC;
 float frequency;  
 float dutyCycle; 
 //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
byte second;
byte minute;
byte hour;
void setup() {
  frequency = 3;
  dutyCycle = 50;
  Wire.begin();
  Serial.begin(74880);
  delay(200);
  pinMode(LED_BUILTIN, INPUT);
  
}

int sec_prev = 0;  // Global Variable

  unsigned long secondsAfterMidnight;
secondsAfterMidnight = 3600HL * hour + 60 * minute + second;
void loop() {

  double period = 1000000 / frequency;
  double offFor = period - (period * (dutyCycle/100));
  double onFor = period - offFor;
  time_t total_second = time(0);
  struct tm* ct = localtime(&total_second);

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

   if (secondsAfterMidnight >= 32768) { 
     if( period > 16383 ) {
   Serial.print("INSERT NEEDLE");
      digitalWrite(LED_BUILTIN, HIGH);
       delay((long)onFor/1000);
       delay (60000);
       digitalWrite(LED_BUILTIN, LOW);
           delay (9999999999999);
     }
       else {
             Serial.print("INSERT NEEDLE");
             
           digitalWrite(LED_BUILTIN, HIGH);
           delayMicroseconds((long)onFor);
           delay (180000);
           digitalWrite(LED_BUILTIN, LOW);
           delay (9999999999999);
  }}
else {
 digitalWrite(LED_BUILTIN, LOW);

  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));
  }
}
}

Try

unsigned long secondsAfterMidnight = (3600UL * hour) + (60 * minute) + second;
//secondsAfterMidnight = (3600UL * hour) + (60 * minute) + second;

Please note the use of additional parenthesis in the calculation.

In your code, you use 3600HL, that should be 3600UL. The IDE will show you warnings on this if you set the warning level to ALL in file → preferences.

The parameter 9999999999999 in your delay() exceeds the maximum number that can be used (see uint32_t max value - Google Search); the maximum is 4294967295. Again, the IDE will show warnings on this if you set the warning level to ALL in file → preferences.

This wont work. hour, minute and second dont have any values yet - you can't use them in a calculation before you assign values to them.
You need to put the calculation somewhere after the lines where you set the values of hour, minute and second.

@t-dog-69 you don't seem like you have read much code or studied/learned/appreciated the actual pesky details of coding.

Assignment statements should must be within functions.

To @touch1337's point I would add:

You need to put the calculation somewhere after the lines where you set (or get) the values of hour, minute and second, and before you try to use that value in any way, like an if statement.

Simply having an equation that states a relationship between some variables and another, like in maths or something, you have to think of a program as something that is alive and active.

To get a new value into a variable, the typical manner in which this happens in a simple computer program is that… wait for it… an assignment statement is executed. That is, a line of code through which the flow of you running program takes it gets done. Performed.

So just one second after calculating secondsAftreMidnight, the value in that variable is wrong, isn't the number of seconds since midnight, not until you do the caculation again. With the very latest values of hour, minute and second, wherever they got their new values from which.

I have only gone into more detail in this the fourth or fifth post trying to get at what will turn out to be your very simple problem.

Did you read about variable scope?

a7

even with the use of parenthesis, the value of secondsAfterMidnight is equal to 0. Wouldn't I need to give value to the variable's hour, second, and minute before that statement?

Of course and that was explained several times already:
Here
And Here
And Here Again

Your programming technique seems to be throwing sh*t at the wall, seeing what sticks, and asking forum members to clean up your mess.

Time to put your project aside, go back to basics, and learn to code. Read a book, take an online tutorial, take a class at a community college, watch teaching videos, study all the examples built in to the Arduino IDE .....

Continuing on your current path will lead to nothing but frustration for all involved.

See the comments by @touch1337 and @alto777 in the previous posts.

Did you read about variable scope?

yes, and I did use an assignment statement for the hours, minutes, and seconds variables:

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

and when I attempt to set it as a global variable outside any function, it gets the error message that seconds, minutes, and hours does not name a type. idk why my variables don't have value when there's an assignment statement, perhaps it needs to be a global variable where there's an error to that:

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_467835 -warnings=none -build-cache C:\Users\Thecht\AppData\Local\Temp\arduino_cache_848073 -prefs=build.warn_data_percentage=75 -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 -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 -verbose C:\Users\Thecht\Documents\Arduino\clone_1_LED_of_Moduel_clock\clone_1_LED_of_Moduel_clock.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_467835 -warnings=none -build-cache C:\Users\Thecht\AppData\Local\Temp\arduino_cache_848073 -prefs=build.warn_data_percentage=75 -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 -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 -verbose C:\Users\Thecht\Documents\Arduino\clone_1_LED_of_Moduel_clock\clone_1_LED_of_Moduel_clock.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_467835\\sketch\\clone_1_LED_of_Moduel_clock.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_467835\\sketch\\clone_1_LED_of_Moduel_clock.ino.cpp" -o nul

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

ResolveLibrary(DS3231.h)

  -> candidates: [DS3231@1.1.0 DS3231_RTC@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_467835\\sketch\\clone_1_LED_of_Moduel_clock.ino.cpp" -o nul

Using cached library dependencies for file: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src\Wire.cpp

Using cached library dependencies for file: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src\utility\twi.c

Using cached library dependencies for file: C:\Users\Thecht\Documents\Arduino\libraries\DS3231\DS3231.cpp

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_467835\\sketch\\clone_1_LED_of_Moduel_clock.ino.cpp" -o "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_467835\\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_467835\\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_467835\\sketch\\clone_1_LED_of_Moduel_clock.ino.cpp" -o "C:\\Users\\Thecht\\AppData\\Local\\Temp\\arduino_build_467835\\sketch\\clone_1_LED_of_Moduel_clock.ino.cpp.o"

clone_1_LED_of_Moduel_clock:11:3: error: 'minute' does not name a type

   minute = ct->tm_min;

   ^~~~~~

clone_1_LED_of_Moduel_clock:12:3: error: 'hour' does not name a type

   hour = ct->tm_hour;

   ^~~~

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

'minute' does not name a type

Maybe my assignment statement is being ignored for some reason, also I apologize for inconveniencing your time, I'm a beginner Arduino programmer (as you can tell by my lack of detail knowledge). So... without trying to make it seem like I'm not listening to you or am too arrogant or forcing all my problems on you, explain what is wrong with my assignment statement.

Without seeing them in context, I can place a bet that you have assignment statements that are…

outside the { braces } that mark the beginning and end of code, like the code in a function.

Assignment statements must be within the body of a function.

This, placed outside any function, is a global declaration and initialisation of a variable:

unsigned long someVariableMadeUpJustNow = 42;

it may look like an assignment statement. It is not.

Declare your variables (and their initial values if you need) as global or local and maybe static as needed.

If you don't know what any of those five words above mean and why they seem to be at the top cause of your pain, please google them in the context of "variable scope C/C++" or "variable scope Arduino".

And again without seeing the latest code, it appears that you still think one assignment-like expression, placed somewhere, is going to magically bind a variable to a value continuously informed and refreshed when any changes to the variables in the expression change. Not true. Please come to grips with that.

a7

here's my entire sketch and as you can see it is within a function:

#include <time.h>
 #include <Wire.h>
 #include <DS3231.h>
 DS3231 myRTC;
 float frequency;  
 float dutyCycle; 
 //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() {
  frequency = 3;
  dutyCycle = 50;
  Wire.begin();
  Serial.begin(74880);
  delay(200);
  pinMode(LED_BUILTIN, INPUT);
     
}

int sec_prev = 0;  // Global Variable
 
unsigned long secondsAfterMidnight = (3600UL * hour) + (60 * minute) + second;


void loop() {

  double period = 1000000 / frequency;
  double offFor = period - (period * (dutyCycle/100));
  double onFor = period - offFor;
  time_t total_second = time(0);
  struct tm* ct = localtime(&total_second);
 second = ct->tm_sec;
  minute = ct->tm_min;
  hour = ct->tm_hour;

   if (secondsAfterMidnight >= 32768) { 
     if( period > 16383 ) {
   Serial.print("INSERT NEEDLE");
      digitalWrite(LED_BUILTIN, HIGH);
       delay((long)onFor/1000);
       delay (60000);
       digitalWrite(LED_BUILTIN, LOW);
           delay ( 4294967295);
     }
       else {
             Serial.print("INSERT NEEDLE");
             
           digitalWrite(LED_BUILTIN, HIGH);
           delayMicroseconds((long)onFor);
           delay (180000);
           digitalWrite(LED_BUILTIN, LOW);
           delay (4294967295);
  }}
else {
 digitalWrite(LED_BUILTIN, LOW);

  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 appreciate you being patient with me.

I have infinite patience. But...

The code you just posted does not compile. Therefor it cannot be the source of any new information you are finding.

The symbol secondsAfterMidnght appears in your sketch exactly twice.

Once, a declaration of a global variable. Good!

unsigned long secondsAfterMidnight = (3600UL * hour) + (60 * minute) + second;

But lose the "= (3600UL * hour) + (60 * minute) + second" part there. It is unnecssary and also will not compile, somewhere along the way you lost the declarations for hour, minute and second.

The second appearance is in you loop() function

  if (secondsAfterMidnight >= 32768) {

where you use it without calculating it using the current values of hour, minute and second.

Just now playing with your code, I wonder, as I did somewhere up there earlier on this thread, how the RTC is being called upon to fill in the values you are using. Until I added lines of code to explicitly read the RTC registers, all I got from you code was 0 for time in all variables.

Have you used the RTC in a test sketch to simply read from it the time of day and print that to the serial monitor?

I have a feeling that you are never getting any actual time information, and everything you observe is due to the mistakes you keep making.

Find an example program that tests and demonstrates the RTC. There should be suitable examples that came with the library you are using.

BTW which exact DS3231 library is it? There are a few.

a7

BTW which exact DS3231 library is it? 

The name for it is literally "DS3231" By Andrew Wickard and I have the 1.1.0 version installed.

Find an example program that tests and demonstrates the RTC. There should be suitable examples that came with the library you are using.

yeah, I found an example sketch that prints the current date and time:

#include <Wire.h>
#include <DS3231.h>

RTClib myRTC;

void setup () {
    Serial.begin(57600);
    Wire.begin();
    delay(500);
    Serial.println("Nano Ready!");
}

void loop () {
    
    delay(1000);
    
    DateTime now = myRTC.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
}

however, the result I get in the serial monitor is this:

18:50:45.791 -> ⸮⸮⸮⸮⸮⸮

(Its 6:50 PM by me)
which is what it identifies the date and time as and I'm pretty sure this doesn't represent any time and when it has 18:50:45 my laptop gave it that time, so you would be right about this assumption:

I have a feeling that you are never getting any actual time information, and everything you observe is due to the mistakes you keep making.

however, when you say:

where you use it without calculating it using the current values of hour, minute and second.

earlier I did print the value of hour using your code line:

   Serial.print(" hour = "); Serial.println(hour);

where I got the same misinformation as using the example sketch just now:
``
19:43:30.037 -> image

`
so...how am I supposed to corollate the value of time with a variable that can't have a valid value if that makes sense. And ik that the time is changing which would make sense but then why would it also apply to printing the value for hours which doesn't constantly change.

Then you say it doesn't.

Did you mean the code is supposed to print the date and time, but instead prints that line of junk?

And the time on that line is from the serial monitor time stamp?

Do you have the correct setting in the serial monitor window baud rate selection?

If you can't get a library example to work, there is either something wrong with the example (probably not) or you are doing something incorrect.

All I can think of is the baud setting. I notice you used

  Serial.begin(74880);

which is not on short lists of standard rates. Use 57600 or 115200, even better.

Elsewise I got nothing. Sry.

added: I ran the exact code you posted in #59 and it worked as expected.

a7

I think I got it to work becuase when the time was 8:37 AM I got

hour = 8hour=8hour=8

and when it was 9:00 I got:

hour =9INSERT NEEDLE

and the LED turned on, heres my new sketch:

 #include <time.h>
 #include <Wire.h>
 #include <DS3231.h>



 float frequency;  
 float dutyCycle; 


 //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
RTClib myRTC;
void setup() {
  frequency = 3;
  dutyCycle = 50;
  Wire.begin();
  Serial.begin(115200);
  delay(200);
  pinMode(LED_BUILTIN, INPUT);


}

int sec_prev = 0;  // Global Variable

void loop() {
    DateTime now = myRTC.now();
int hour = now.hour() + 8 ;
//I added the +8 to represent the hours after midnight becuase the now.hours starts from 0 but counts an hour
  double period = 1000000 / frequency;
  double offFor = period - (period * (dutyCycle/100));
  double onFor = period - offFor;
  time_t total_second = time(0);
  struct tm* ct = localtime(&total_second);




;
// Turn on the light from 3pm to 4pm
  Serial.print ("hour =");
Serial.print (hour);
  if (hour >= 9 ) {
     if( period > 16383 ) {
   Serial.print("INSERT NEEDLE");
   delay(2000);
      digitalWrite(LED_BUILTIN, HIGH);
       delay((long)onFor/1000);
       delay (180000);
       digitalWrite(LED_BUILTIN, LOW);
           delay (4294967295);
     }
       else {
             Serial.print("INSERT NEEDLE");
             
           digitalWrite(LED_BUILTIN, HIGH);
           delayMicroseconds((long)onFor);
           delay (180000);
           digitalWrite(LED_BUILTIN, LOW);
           delay (4294967295);
  }}
else {
 digitalWrite(5, LOW);



   if (hour>=24){
    int hour = 0;
   }
   
}
}

some problems I see arising are that when you turn off the clock module the time leaves off where you left off and doesnt automatically connect but thats not really a problem becuase thats how normal clocks work. Would you say this is a success?

Only you can decide.

RTCs usually have a backup battery to hold the time, so it should not forget what time is is, it should keep track and always be able to report the date and time, as accurately as it can, DS3231 is quite good.

thats how normal clocks work

Not the clocks I use. They keep ticking whether you looking at them or not…

This

delay (4294967295);

is very amusing. A delay of 49.7 days.

Do you really intend to have your program come out of that delay() and continue functioning somehow?

If what you really want is to kill the program such that a power on/off/on cycle or board reset is required to revitalize it, use this idiomatic "forever" statement, your meaning will be clear(er):

    while (1) {}    // infinite "delay"

a statement that happily sits there doing nothing very fast.

You might want to remove

int sec_prev = 0;  // Global Variable

which was leftover nonsense from your earlier attempts. There may be other dead stuff that could be remobed.

This

       digitalWrite(LED_BUILTIN, HIGH);
       delay((long)onFor/1000);
       delay (180000);
       digitalWrite(LED_BUILTIN, LOW);

will… light the LED, delay some carefully caculated time, delay a further three minutes, then extinguish the LED.

Is that what you mean to do?

Also, please know about and start using, if you can't do it yourself, the Autoformat tool in the IDE. That will put your code into a standard layout format, and make it easier to see errors and certainly easier for those of us reading it to make sense of it.

Before you start adding and enhancing this start that you have made, please I beg you to reread this entire thread and glean what knowledge you can about tricks, traps, dumb mistakes, pitfalls and problems and solutions.

If you don't, you'll be back here asking for help… and ppl isn't think you should have learnt something from this little matter.

a7

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