Very first Arduino project so I need a lot of help.
Hardware
- Arduino Uno R4 Minima
- DS3231 (RTC)
- 270 Ohm resistors
- 3-3.2V, 20mA LED
Goal
I'm trying to turn on 1 LED during the day, another during golden hour (sunrise~20min after sunrise & 20min before sunset~sunset) for a traffic light type art piece I'm making.
So Far
I've uploaded the "ds3231" example code from RTClib...
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(57600);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
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");
and gotten the correct time output.
I plan
on using the "SunriseSunset" example code from the SolarCalculator library to get the times for sunrise and sunset.
#include <SolarCalculator.h>
void setup()
{
Serial.begin(9600);
// Date
int year = 2000;
int month = 1;
int day = 1;
// Location
double latitude = 42.36;
double longitude = -71.058;
int utc_offset = -5;
double transit, sunrise, sunset;
// Calculate the times of sunrise, transit, and sunset, in hours (UTC)
calcSunriseSunset(year, month, day, latitude, longitude, transit, sunrise, sunset);
// Get the approximate times (minimum program size) (iterations = 0)
//calcSunriseSunset(year, month, day, latitude, longitude, transit, sunrise, sunset, SUNRISESET_STD_ALTITUDE, 0);
// Print results
char str[6];
Serial.println(hoursToString(sunrise + utc_offset, str));
Serial.println(hoursToString(transit + utc_offset, str));
Serial.println(hoursToString(sunset + utc_offset, str));
}
void loop()
{
}
// Rounded HH:mm format
char* hoursToString(double h, char* str)
{
int m = int(round(h * 60));
int hr = (m / 60) % 24;
int mn = m % 60;
str[0] = (hr / 10) % 10 + '0';
str[1] = (hr % 10) + '0';
str[2] = ':';
str[3] = (mn / 10) % 10 + '0';
str[4] = (mn % 10) + '0';
str[5] = '\0';
return str;
}
My question's are...
-
How do I use the date I'm getting from my RTC for the SunriseSunset code? I see places in the code for manually inputting date but I'm not sure how to automatically use the date I have on my RTC for this part of the code.
-
I'm thinking from there the code would be something like
void loop(){
if(current time == sunrise + 20min){ // time from RTC = sunrise calculated by sunrisesunset code + 20 minutes
digitalWrite(13,HIGH); // first LED for daytime on
}
else{
digitalWrite(13,LOW);// turn the LED off
}
but I'm unsure of the specifics, like how to keep the LED on from the point it is sunrise+20min till it is Sunset-20min and not just turn on the LED for that 1 minute of time where sunrise+20 is true.
Any amount of guidance would be greatly appreciated. I know only how to carve wood.


