Compiler warnings

I have installed the TimeLord from GitHub and RTClib from the library manager.
This is my code to control the light in the staircase using these libraries. The relay will be connected to Pin 9.

#include <TimeLord.h>
#include <RTClib.h>
RTC_DS1307 RTC;
TimeLord aqua;

void setup() {
  // put your setup code here, to run once:
  //put your locations latlong for sunrise/sunset timimgs
  aqua.Position(28.60598, 77.33622);
  aqua.TimeZone(5.5 * 60);
  RTC.begin();
  //RTC.adjust(DateTime(__DATE__, __TIME__));
  pinMode(13, OUTPUT);
  pinMode(9, OUTPUT);
}
void loop() {
  // put your main code here, to run repeatedLy:
  DateTime now = RTC.now();
  byte sunRise[6] = {
    0, 0, 0, now.day(), now.month(), now.year()
  };
  byte sunSet[6] = {
    0, 0, 0, now.day(), now.month(), now.year()
  };
  aqua.SunRise(sunRise);
  aqua.SunSet(sunSet);
  if (now.hour() * 60 + now.minute() > sunRise[2] * 60 + sunRise[1] && now.hour() * 60 + now.minute() <= sunSet[2] * 60 + sunSet[1])
  {
    digitalWrite(13, 1);
    digitalWrite(9, 0);

  }
  else
  {
    digitalWrite(13, 0);
    digitalWrite(9, 1);

  }
  delay(90000);
}

The code compiles but the compiler comes up with some warnings. these are

Compiling library "TimeLord"
"C:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\cores\\arduino" "-IC:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\variants\\standard" "-ID:\\Arduino_1.8.9\\Portable\\Sketchbook\\libraries\\TimeLord" "-ID:\\Arduino_1.8.9\\Portable\\Sketchbook\\libraries\\RTClib" "-IC:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\libraries\\Wire\\src" "D:\\Arduino_1.8.9\\Portable\\Sketchbook\\libraries\\TimeLord\\TimeLord.cpp" -o "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_172345\\libraries\\TimeLord\\TimeLord.cpp.o"
D:\Arduino_1.8.9\Portable\Sketchbook\libraries\TimeLord\TimeLord.cpp: In member function 'bool TimeLord::ComputeSun(uint8_t*, bool)':

D:\Arduino_1.8.9\Portable\Sketchbook\libraries\TimeLord\TimeLord.cpp:299:37: warning: unused variable 'z' [-Wunused-variable]

   float y, decl, eqt, ha, lon, lat, z;

                                     ^

D:\Arduino_1.8.9\Portable\Sketchbook\libraries\TimeLord\TimeLord.cpp:301:7: warning: unused variable 'doy' [-Wunused-variable]

   int doy, minutes;

       ^

Compiling library "RTClib"
"C:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\cores\\arduino" "-IC:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\variants\\standard" "-ID:\\Arduino_1.8.9\\Portable\\Sketchbook\\libraries\\TimeLord" "-ID:\\Arduino_1.8.9\\Portable\\Sketchbook\\libraries\\RTClib" "-IC:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\libraries\\Wire\\src" "D:\\Arduino_1.8.9\\Portable\\Sketchbook\\libraries\\RTClib\\RTClib.cpp" -o "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_172345\\libraries\\RTClib\\RTClib.cpp.o"
D:\Arduino_1.8.9\Portable\Sketchbook\libraries\RTClib\RTClib.cpp: In constructor 'DateTime::DateTime(uint32_t)':

D:\Arduino_1.8.9\Portable\Sketchbook\libraries\RTClib\RTClib.cpp:86:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

         if (days < 365 + leap)

                  ^

D:\Arduino_1.8.9\Portable\Sketchbook\libraries\RTClib\RTClib.cpp: In constructor 'DateTime::DateTime(const char*, const char*)':

D:\Arduino_1.8.9\Portable\Sketchbook\libraries\RTClib\RTClib.cpp:136:70: warning: operation on '((DateTime*)this)->DateTime::m' may be undefined [-Wsequence-point]

         case 'J': m = date[1] == 'a' ? 1 : m = date[2] == 'n' ? 6 : 7; break;

                                                                      ^

D:\Arduino_1.8.9\Portable\Sketchbook\libraries\RTClib\RTClib.cpp: In constructor 'DateTime::DateTime(const __FlashStringHelper*, const __FlashStringHelper*)':

D:\Arduino_1.8.9\Portable\Sketchbook\libraries\RTClib\RTClib.cpp:161:70: warning: operation on '((DateTime*)this)->DateTime::m' may be undefined [-Wsequence-point]

         case 'J': m = buff[1] == 'a' ? 1 : m = buff[2] == 'n' ? 6 : 7; break;

                                                                      ^

These appear to be about code in the libraries.

How do we address these warnings.

Regards.

  1. Ignore them.

or

  1. Make changes to the libaries.
  • "Unused variable" warnings are harmless, but indicate sloppy writing by the library's author.

  • "Comparison between signed and unsigned" warning is probably harmless in this context. Hard to say without seeing the whole code.

  • "Operation may be undefined" warning is a potentially serious error in C++11, which in this particular case should normally work out as intended. But the author should get a good slap on the wrist for this nonsense. (It is no longer an error in C++17, but Arduino IDE compiles in C++11).

The only way to address those warnings is to modify the library's code. Maybe you should just refresh your library. The version I see here - GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC library - is free of at least some of these issues. The "Operation may be undefined" issue was fixed on April 9, 2017.

Thanks Montmorency, googled and found a topic on our forum here https://forum.arduino.cc/index.php?topic=129826.0

I cloned the TimeLord library from here

These lines of code are there in the TimeLord.h and TimeLord.cpp

extern "C" {

 #include <inttypes.h>
 #include <Math.h>
}

I changed it to

extern "C" {

 #include <inttypes.h>
 #include <math.h>
}

With this two warnings have gone out (the compiler is happy)
However this warning is coming up

Compiling sketch...
"C:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10809 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\cores\\arduino" "-IC:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\variants\\standard" "-ID:\\Arduino_1.8.9\\Portable\\Sketchbook\\libraries\\TimeLord" "-ID:\\Arduino_1.8.9\\Portable\\Sketchbook\\libraries\\RTClib" "-IC:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.6.21\\libraries\\Wire\\src" "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988\\sketch\\sketch_stairCaseLightControl.ino.cpp" -o "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988\\sketch\\sketch_stairCaseLightControl.ino.cpp.o"
D:\Arduino_1.8.9\Portable\Sketchbook\sketch_stairCaseLightControl\sketch_stairCaseLightControl.ino: In function 'void loop()':

D:\Arduino_1.8.9\Portable\Sketchbook\sketch_stairCaseLightControl\sketch_stairCaseLightControl.ino:19:65: warning: narrowing conversion of 'now.DateTime::year()' from 'uint16_t {aka unsigned int}' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]

   byte sunRise[6] = { 0, 0, 0, now.day(), now.month(), now.year()} ;

                                                                 ^

D:\Arduino_1.8.9\Portable\Sketchbook\sketch_stairCaseLightControl\sketch_stairCaseLightControl.ino:20:64: warning: narrowing conversion of 'now.DateTime::year()' from 'uint16_t {aka unsigned int}' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]

   byte sunSet[6] = { 0, 0, 0, now.day(), now.month(), now.year() };

                                                                ^

Compiling libraries...
Compiling library "TimeLord"
Using previously compiled file: C:\Users\Karanbir\AppData\Local\Temp\arduino_build_369988\libraries\TimeLord\TimeLord.cpp.o
Compiling library "RTClib"
Using previously compiled file: C:\Users\Karanbir\AppData\Local\Temp\arduino_build_369988\libraries\RTClib\RTClib.cpp.o
Compiling library "Wire"
Using previously compiled file: C:\Users\Karanbir\AppData\Local\Temp\arduino_build_369988\libraries\Wire\Wire.cpp.o
Using previously compiled file: C:\Users\Karanbir\AppData\Local\Temp\arduino_build_369988\libraries\Wire\utility\twi.c.o
Compiling core...
Using precompiled core: C:\Users\Karanbir\AppData\Local\Temp\arduino_cache_437441\core\core_arduino_avr_uno_d6a851970069e21efb01b317b6e23b91.a
Linking everything together...
"C:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\4.9.2-atmel3.5.4-arduino2/bin/avr-gcc" -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=atmega328p -o "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988/sketch_stairCaseLightControl.ino.elf" "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988\\sketch\\sketch_stairCaseLightControl.ino.cpp.o" "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988\\libraries\\TimeLord\\TimeLord.cpp.o" "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988\\libraries\\RTClib\\RTClib.cpp.o" "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988\\libraries\\Wire\\Wire.cpp.o" "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988\\libraries\\Wire\\utility\\twi.c.o" "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988/..\\arduino_cache_437441\\core\\core_arduino_avr_uno_d6a851970069e21efb01b317b6e23b91.a" "-LC:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988" -lm
"C:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\4.9.2-atmel3.5.4-arduino2/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988/sketch_stairCaseLightControl.ino.elf" "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988/sketch_stairCaseLightControl.ino.eep"
"C:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\4.9.2-atmel3.5.4-arduino2/bin/avr-objcopy" -O ihex -R .eeprom "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988/sketch_stairCaseLightControl.ino.elf" "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988/sketch_stairCaseLightControl.ino.hex"
Using library TimeLord in folder: D:\Arduino_1.8.9\Portable\Sketchbook\libraries\TimeLord (legacy)
Using library RTClib at version 1.2.1 in folder: D:\Arduino_1.8.9\Portable\Sketchbook\libraries\RTClib 
Using library Wire at version 1.0 in folder: C:\Users\Karanbir\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\libraries\Wire 
"C:\\Users\\Karanbir\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\4.9.2-atmel3.5.4-arduino2/bin/avr-size" -A "C:\\Users\\Karanbir\\AppData\\Local\\Temp\\arduino_build_369988/sketch_stairCaseLightControl.ino.elf"
Sketch uses 6694 bytes (20%) of program storage space. Maximum is 32256 bytes.
Global variables use 234 bytes (11%) of dynamic memory, leaving 1814 bytes for local variables. Maximum is 2048 bytes.

What further change is required now ?

kssoin:
What further change is required now ?

Well, what are the values returned by those .year() functions? Do they return the full year number, like 2019?

If so, then how do you expect to store the result in a byte?

Montmorency:
Well, what are the values returned by those .year() functions? Do they return the full year number, like 2019?

If so, then how do you expect to store the result in a byte?

The code for the function DateTime from RTClib.cpp

DateTime::DateTime (uint32_t t) {
  t -= SECONDS_FROM_1970_TO_2000;    // bring to 2000 timestamp from 1970

    ss = t % 60;
    t /= 60;
    mm = t % 60;
    t /= 60;
    hh = t % 24;
    uint16_t days = t / 24;
    uint8_t leap;
    for (yOff = 0; ; ++yOff) {
        leap = yOff % 4 == 0;
        if (days < 365 + leap)
            break;
        days -= 365 + leap;
    }
    for (m = 1; ; ++m) {
        uint8_t daysPerMonth = pgm_read_byte(daysInMonth + m - 1);
        if (leap && m == 2)
            ++daysPerMonth;
        if (days < daysPerMonth)
            break;
        days -= daysPerMonth;
    }
    d = days + 1;
}

DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
    if (year >= 2000)
        year -= 2000;
    yOff = year;
    m = month;
    d = day;
    hh = hour;
    mm = min;
    ss = sec;
}

DateTime::DateTime (const DateTime& copy):
  yOff(copy.yOff),
  m(copy.m),
  d(copy.d),
  hh(copy.hh),
  mm(copy.mm),
  ss(copy.ss)

Where is the change required.

now.year() returns an uint16_t integer, aka an unsigned 16 bit integer, aka a two bytes integer.

in the byte array sunrise[6] you init the last byte (an 8 bit unsigned number) with a 16 bit number that requires 2 bytes: compiler is not happy.

if year is >= 2000 and is <= 2255, you can simply change now.year() with (byte)(now.year() - 2000) in

D:\Arduino_1.8.9\Portable\Sketchbook\sketch_stairCaseLightControl\sketch_stairCaseLightControl.ino

byte sunRise[6] = { 0, 0, 0, now.day(), now.month(), (byte)(now.year() - 2000)};

vlc0617:
if year is >= 2000 and is <= 2255, you can simply change now.year() with (byte)(now.year() - 2000) in

D:\Arduino_1.8.9\Portable\Sketchbook\sketch_stairCaseLightControl\sketch_stairCaseLightControl.ino

byte sunRise[6] = { 0, 0, 0, now.day(), now.month(), (byte)(now.year() - 2000)};

Thanks vlc0617 your method worked.
Why this condition

if year is >= 2000 and is <= 2255

when RTClib is for 2099.
For academic purose, can the libraries/library or sketch code be changed so that now.year() can be used as defined in the RTClib