Hi!
I'm trying to make a program that will perform something at every sunset. I have Chronodot RTC and I must say that I have used this program for quite some months without a problem. Then, all of a sudden, it didn't work anymore (it may be time related, as I read values from RTC and of course as time progresses these values change). It is as if Arduino goes crazy, Pin 13 LED is lit up although it shouldn't be and it prints a mess into serial output if I have many prints inside setup routine (with this example I provided it prints nothing) - from this mess I think I can recognize that it is resetting itself over and over again.
Now to the actual problem - I have determined (with commenting out various portions of code) that the error lies at the line accessing the array sunset:
#include <Wire.h>
#include "Chronodot.h"
Chronodot RTC;
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; // For calculating day of week.
static int sunset[ 12 ][ 31 ][ 2 ] = {{{16, 42}, {16, 42}, {16, 43}, {16, 44}, {16, 45}, {16, 46}, {16, 48}, {16, 49}, {16, 50}, {16, 51}, {16, 52}, {16, 53}, {16, 55}, {16, 56}, {16, 57}, {16, 58}, {17, 0}, {17, 1}, {17, 2}, {17, 4}, {17, 5}, {17, 7}, {17, 8}, {17, 9}, {17, 11}, {17, 12}, {17, 14}, {17, 15}, {17, 17}, {17, 18}, {17, 20}}, {{17, 21}, {17, 23}, {17, 24}, {17, 26}, {17, 27}, {17, 29}, {17, 30}, {17, 32}, {17, 33}, {17, 34}, {17, 36}, {17, 37}, {17, 39}, {17, 40}, {17, 42}, {17, 43}, {17, 45}, {17, 46}, {17, 48}, {17, 49}, {17, 51}, {17, 52}, {17, 54}, {17, 55}, {17, 57}, {17, 58}, {17, 59}, {18, 1}, {18, 2}, {0, 0}, {0, 0}}, {{18, 4}, {18, 5}, {18, 7}, {18, 8}, {18, 9}, {18, 11}, {18, 12}, {18, 13}, {18, 15}, {18, 16}, {18, 18}, {18, 19}, {18, 20}, {18, 22}, {18, 23}, {18, 24}, {18, 26}, {18, 27}, {18, 28}, {18, 30}, {18, 31}, {18, 33}, {18, 34}, {18, 35}, {19, 37}, {19, 38}, {19, 39}, {19, 41}, {19, 42}, {19, 43}, {19, 44}}, {{19, 46}, {19, 47}, {19, 48}, {19, 50}, {19, 51}, {19, 52}, {19, 54}, {19, 55}, {19, 56}, {19, 58}, {19, 59}, {20, 0}, {20, 2}, {20, 3}, {20, 4}, {20, 6}, {20, 7}, {20, 8}, {20, 10}, {20, 11}, {20, 12}, {20, 14}, {20, 15}, {20, 16}, {20, 18}, {20, 19}, {20, 20}, {20, 21}, {20, 23}, {20, 24}, {0, 0}}, {{20, 25}, {20, 27}, {20, 28}, {20, 29}, {20, 31}, {20, 32}, {20, 33}, {20, 34}, {20, 36}, {20, 37}, {20, 38}, {20, 39}, {20, 40}, {20, 42}, {20, 43}, {20, 44}, {20, 45}, {20, 46}, {20, 48}, {20, 49}, {20, 50}, {20, 51}, {20, 52}, {20, 53}, {20, 54}, {20, 55}, {20, 56}, {20, 57}, {20, 58}, {20, 59}, {21, 0}}, {{21, 1}, {21, 2}, {21, 2}, {21, 3}, {21, 4}, {21, 5}, {21, 5}, {21, 6}, {21, 7}, {21, 7}, {21, 8}, {21, 8}, {21, 9}, {21, 9}, {21, 10}, {21, 10}, {21, 11}, {21, 11}, {21, 11}, {21, 11}, {21, 12}, {21, 12}, {21, 12}, {21, 12}, {21, 12}, {21, 12}, {21, 12}, {21, 12}, {21, 12}, {21, 12}, {0, 0}}, {{21, 11}, {21, 11}, {21, 11}, {21, 11}, {21, 10}, {21, 10}, {21, 9}, {21, 9}, {21, 8}, {21, 8}, {21, 7}, {21, 7}, {21, 6}, {21, 5}, {21, 4}, {21, 4}, {21, 3}, {21, 2}, {21, 1}, {21, 0}, {20, 59}, {20, 58}, {20, 57}, {20, 56}, {20, 55}, {20, 54}, {20, 53}, {20, 51}, {20, 50}, {20, 49}, {20, 48}}, {{20, 46}, {20, 45}, {20, 44}, {20, 42}, {20, 41}, {20, 39}, {20, 38}, {20, 36}, {20, 35}, {20, 33}, {20, 32}, {20, 30}, {20, 28}, {20, 27}, {20, 25}, {20, 24}, {20, 22}, {20, 20}, {20, 18}, {20, 17}, {20, 15}, {20, 13}, {20, 11}, {20, 10}, {20, 8}, {20, 6}, {20, 4}, {20, 2}, {20, 0}, {19, 59}, {19, 57}}, {{19, 55}, {19, 53}, {19, 51}, {19, 49}, {19, 47}, {19, 45}, {19, 43}, {19, 41}, {19, 40}, {19, 38}, {19, 36}, {19, 34}, {19, 32}, {19, 30}, {19, 28}, {19, 26}, {19, 24}, {19, 22}, {19, 20}, {19, 18}, {19, 16}, {19, 14}, {19, 12}, {19, 10}, {19, 8}, {19, 6}, {19, 4}, {19, 2}, {19, 0}, {18, 58}, {0, 0}}, {{18, 57}, {18, 55}, {18, 53}, {18, 51}, {18, 49}, {18, 47}, {18, 45}, {18, 43}, {18, 41}, {18, 39}, {18, 38}, {18, 36}, {18, 34}, {18, 32}, {18, 30}, {18, 29}, {18, 27}, {18, 25}, {18, 23}, {18, 22}, {18, 20}, {18, 18}, {18, 17}, {18, 15}, {18, 13}, {18, 12}, {18, 10}, {17, 9}, {17, 7}, {17, 5}, {17, 4}}, {{17, 2}, {17, 1}, {17, 0}, {16, 58}, {16, 57}, {16, 56}, {16, 54}, {16, 53}, {16, 52}, {16, 50}, {16, 49}, {16, 48}, {16, 47}, {16, 46}, {16, 45}, {16, 44}, {16, 43}, {16, 42}, {16, 41}, {16, 40}, {16, 39}, {16, 38}, {16, 38}, {16, 37}, {16, 36}, {16, 36}, {16, 35}, {16, 35}, {16, 34}, {16, 34}, {0, 0}}, {{16, 33}, {16, 33}, {16, 32}, {16, 32}, {16, 32}, {16, 32}, {16, 32}, {16, 31}, {16, 31}, {16, 31}, {16, 31}, {16, 31}, {16, 32}, {16, 32}, {16, 32}, {16, 32}, {16, 33}, {16, 33}, {16, 33}, {16, 34}, {16, 34}, {16, 35}, {16, 35}, {16, 36}, {16, 37}, {16, 37}, {16, 38}, {16, 39}, {16, 40}, {16, 40}, {16, 41}}};
void setup ()
{
Serial.begin(9600);
Serial.print("Initialising...");
Wire.begin(); // Initialise connection to RTC via SCL/SDA.
RTC.begin(); // Initialise RTC.
if ( ! RTC.isrunning() )
{
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust( DateTime( __DATE__, __TIME__ ));
}
Serial.println("Initialisation complete.");
}
void loop ()
{
DateTime now = RTC.now(); // Get current time.
// Separate current time into hours, minutes, months, days and day of week.
int hrs24 = now.hour();
int mins = now.minute();
int month = now.month();
int sec = now.second();
int day = now.day();
int dayOfWeek = dow( now.year(), month, day );
Serial.print("Time: ");
Serial.print(hrs24, DEC);
Serial.print(":");
Serial.print(mins, DEC);
Serial.print(":");
Serial.print(sec, DEC);
Serial.print(" Temperature: ");
Serial.print(now.tempC(), 1);
Serial.println(" C");
checkSunset( hrs24, mins, dayOfWeek, month, day );
delay( 1000 );
}
void checkSunset ( int hrs, int mins, int dayOfWeek, int month, int day )
{
int sHr = sunset[ month-1 ][ day-1 ][ 0 ];
int sMin = sunset[ month-1 ][ day-1 ][ 1 ];
if ( hrs == sHr && mins == sMin )
{
Serial.println("Its sunset!");
}
}
/* Function that gets day of week from date. */
int dow ( int y, int m, int d )
{ // Week starts with sunday - index 0.
y -= m < 3;
return ( y + y/4 - y/100 + y/400 + t[ m-1 ] + d ) % 7;
}
These two lines:
int sHr = sunset[ month-1 ][ day-1 ][ 0 ];
int sMin = sunset[ month-1 ][ day-1 ][ 1 ];
The curious thing here is, though, that
-
This array is ok, I tested it in Java by copy pasting it and deleting the numbers in square brackets at its initialisation (as Java doesn't use those) and then printing out its contents.
-
The program works if I do this:
int sHr = sunset[ 0 ][ 21 ][ 0 ];
int sMin = sunset[ 0 ][ 21 ][ 1 ];
- This doesnt help:
int i = month - 1;
int j = day - 1;
int sHr = sunset[ i ][ j ][ 0 ];
int sMin = sunset[ i ][ j ][ 1 ];
- Printing out month and day via Serial works fine.
Could someone please explain to me what is going on? Thank you!