Skip to content

Instantly share code, notes, and snippets.

@fcalderan
Last active February 11, 2020 10:32
Show Gist options
  • Save fcalderan/9f79e37a046537e2cf1dfee7f52e34d8 to your computer and use it in GitHub Desktop.
Save fcalderan/9f79e37a046537e2cf1dfee7f52e34d8 to your computer and use it in GitHub Desktop.
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <Wire.h>
#include <RtcDS3231.h>
#define DELAY_ONPLAY 1000
#define DELAY_ONPAUSE 20000
// RTC Library (I2C)
RtcDS3231<TwoWire> rtcObject(Wire);
// MP3 Serial communication
SoftwareSerial mySoftwareSerial(14, 12);
DFRobotDFPlayerMini myDFPlayer;
// Variables
int volume, mth, day, h, m;
// State of the playback
int playbackCount = 0;
bool isPlaying = false;
void setup() {
mySoftwareSerial.begin(9600);
// Start serial connection
Serial.begin(115200);
// Start I2C
rtcObject.Begin();
/* Date and time first configuration
*
* Define a date and time object that accepts the format
* yyyy, m, d, H, M, S. Then configure the RTC with the
* object defined.
*/
// RtcDateTime currentTime = RtcDateTime(2020, 2, 15, 16, 0, 0);
// rtcObject.SetDateTime(currentTime);
// Use softwareSerial to begin communication with the MP3 module
if (!myDFPlayer.begin(mySoftwareSerial)) {
while(true){
delay(10);
}
}
/* Get the value of potentiometer in the range of [3..30] and
* set the volume
*/
setVolume();
// Play the first MP3 for 5 seconds, as a diagnostic routine
myDFPlayer.play(1);
delay(5000);
myDFPlayer.pause();
}
void loop() {
setVolume();
RtcDateTime currentTime = rtcObject.GetDateTime(); //get the time from the RTC
mth = currentTime.Month();
day = currentTime.Day();
h = currentTime.Hour();
m = currentTime.Minute();
/* The caller is not playing: check if date and time should
* start the playback
*/
if (isPlaying == false) {
if (startSunrise() || startSunset()) {
isPlaying = true;
}
else {
// Nothing to do for the next 20 seconds
delay(DELAY_ONPAUSE)
}
}
else {
// Currently playing
if (playbackCount < 5) {
if (/* MP3mini_is_idle */) {
myDFPlayer.play(1);
playbackCount = playbackCount + 1;
Serial.print("Playback #");
Serial.println(String(playbackCount));
}
}
else {
playbackCount = 0;
isPlaying = false;
Serial.print("End of playback");
}
delay(DELAY_ONPLAY);
}
}
void setVolume() {
// Get the value of potentiometer in the range of [3..30]
volume = map(analogRead(A0), 0, 1023, 2, 30);
volume = constrain(volume, 3, 30);
// Set the player volume
myDFPlayer.volume(volume);
Serial.println(String(volume));
}
void printDateTime() {
char str[20];
sprintf(str, "%d/%d/%d %d:%d:%d",
currentTime.Year(),
currentTime.Month(),
currentTime.Day(),
currentTime.Hour(),
currentTime.Minute(),
currentTime.Second()
);
Serial.println(str);
}
bool startSunrise() {
/* at the sunrise the call has to be played for 2 hours */
if (mth == 2) {
if ((day == 20 && h == 7 && m == 8) ||
(day == 21 && h == 7 && m == 6) ||
(day == 22 && h == 7 && m == 4) ||
(day == 23 && h == 7 && m == 3) ||
(day == 24 && h == 6 && m == 1) ||
(day == 25 && h == 6 && m == 59) ||
(day == 26 && h == 6 && m == 57) ||
(day == 27 && h == 6 && m == 56) ||
(day == 28 && h == 6 && m == 54) ||
(day == 29 && h == 6 && m == 52)) { return true; }
}
if (mth == 3) {
if ((day == 1 && h == 6 && m >= 49) ||
(day == 2 && h == 6 && m >= 47) ||
(day == 3 && h == 6 && m >= 45 ) ||
(day == 4 && h == 6 && m >= 43) ||
(day == 5 && h == 6 && m >= 41) ||
(day == 6 && h == 6 && m >= 40) ||
(day == 7 && h == 6 && m >= 38) ||
(day == 8 && h == 6 && m >= 36) ||
(day == 9 && h == 6 && m >= 34) ||
(day == 10 && h == 6 && m >= 32) ||
(day == 11 && h == 6 && m >= 30) ||
(day == 12 && h == 6 && m >= 28) ||
(day == 13 && h == 6 && m >= 27) ||
(day == 14 && h == 6 && m >= 25) ||
(day == 15 && h == 6 && m >= 23) ||
(day == 16 && h == 6 && m >= 21) ||
(day == 17 && h == 6 && m >= 19) ||
(day == 18 && h == 6 && m >= 17) ||
(day == 19 && h == 6 && m >= 15) ||
(day == 20 && h == 6 && m >= 13) ||
(day == 21 && h == 6 && m >= 11) ||
(day == 22 && h == 6 && m >= 9) ||
(day == 23 && h == 6 && m >= 7) ||
(day == 24 && h == 6 && m >= 6) ||
(day == 25 && h == 6 && m >= 4) ||
(day == 26 && h == 6 && m >= 2) ||
(day == 27 && h == 6 && m >= 0) ||
(day == 28 && h == 5 && m >= 58)) { return true; }
}
return false;
}
bool startSunset() {
/* 2 hours before sunset the call has to be played for 2 hours */
if (mth == 2) {
if ((day == 20 && h == 15 && m >= 41) ||
(day == 21 && h == 15 && m >= 42) ||
(day == 22 && h == 15 && m >= 44) ||
(day == 23 && h == 15 && m >= 45) ||
(day == 24 && h == 15 && m >= 47) ||
(day == 25 && h == 15 && m >= 48) ||
(day == 26 && h == 15 && m >= 49) ||
(day == 27 && h == 15 && m >= 51) ||
(day == 28 && h == 15 && m >= 52) ||
(day == 29 && h == 15 && m >= 54)) { return true; }
}
if (mth == 3) {
if ((day == 1 && h == 15 && m >= 56) ||
(day == 2 && h == 15 && m >= 58) ||
(day == 3 && h == 15 && m >= 59) ||
(day == 4 && h == 16 && m >= 1) ||
(day == 5 && h == 16 && m >= 2) ||
(day == 6 && h == 16 && m >= 3) ||
(day == 7 && h == 16 && m >= 5) ||
(day == 8 && h == 16 && m >= 6) ||
(day == 9 && h == 16 && m >= 8) ||
(day == 10 && h == 16 && m >= 9) ||
(day == 11 && h == 16 && m >= 10) ||
(day == 12 && h == 16 && m >= 12) ||
(day == 13 && h == 16 && m >= 13) ||
(day == 14 && h == 16 && m >= 14) ||
(day == 15 && h == 16 && m >= 16) ||
(day == 16 && h == 16 && m >= 17) ||
(day == 17 && h == 16 && m >= 18) ||
(day == 18 && h == 16 && m >= 20) ||
(day == 19 && h == 16 && m >= 21) ||
(day == 20 && h == 16 && m >= 22) ||
(day == 21 && h == 16 && m >= 24) ||
(day == 22 && h == 16 && m >= 25) ||
(day == 23 && h == 16 && m >= 26) ||
(day == 24 && h == 16 && m >= 28) ||
(day == 25 && h == 16 && m >= 29) ||
(day == 26 && h == 16 && m >= 30) ||
(day == 27 && h == 16 && m >= 31) ||
(day == 28 && h == 16 && m >= 33)) { return true; }
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment