#include "RTClib.h" // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <SPI.h>
#include <DMD2.h>
#include <fonts/SystemFont5x7.h>
const char imsak[] PROGMEM = " - IMSAK Bray.. udud abisin dolo.. ";
const char subuh[] PROGMEM = " - SUBUH Bre.. yokk molor nya pending dolo.. ";
const char dzuhur[] PROGMEM = " - DZUHUR Kisanak.. istirohat lah sejenak.. ";
const char ashar[] PROGMEM = " - ASHAR Brayyy.. Let's go.. ";
const char magrib[] PROGMEM = " - MAGHRIB Bro.. jangan lupa mandi dolo.. ";
const char isya[] PROGMEM = " - ISYA Gaess.. lebih cepat lebih baik.. ";
PGM_P const jws[] PROGMEM = {imsak, subuh, dzuhur, ashar, magrib, isya};
//array 1= imsak, subuh, dzuhur, ashar, magrib, isya
//array 2= jam, menit, durasi
const byte jadwal[6][3] PROGMEM = {{3,50,10}, {4,10,55}, {11,50,90}, {15,15,100}, {18,5,40}, {19,20,200}};
const char mg[] PROGMEM = "Minggu";
const char sn[] PROGMEM = "Senin";
const char sl[] PROGMEM = "Selasa";
const char rb[] PROGMEM = "Rabu";
const char km[] PROGMEM = "Kamis";
const char jm[] PROGMEM = "Jumat";
const char sb[] PROGMEM = "Sabtu";
PGM_P const daftarhari[] PROGMEM = {mg, sn, sl, rb, km, jm, sb};
SoftDMD dmd(1,1);
DMD_TextBox box(dmd, 0, 8); // left, top, width, height
RTC_DS1307 rtc;
char buffer[50], jamenit[7];
unsigned long jadul = 0, lawas = 0, kudet = 0; //millis
String scrolltext;
int cetak = 0, spd = 300; //spd= scrolltext speed
void setup () {
Serial.begin(57600);
dmd.setBrightness(255);
dmd.selectFont(SystemFont5x7);
dmd.begin();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
Serial.println("send commands to fix incorrect date n time = year, month, day, hour, minute, second");
Serial.println("eg: 2021, 4, 27, 23, 45, 55");
}
void loop () {
DateTime now = rtc.now();
unsigned long gaul = millis();
//5 secs interval for schedules n current time checking
//interval 5 detik untuk cek waktu dgn jadwal
if(kudet == 0 || gaul - kudet >= 5000){
spd = 200;
scrolltext = " *-* ";
for (int z = 0; z < strlen_P(pgm_read_word(&(daftarhari[now.dayOfTheWeek()]))); z++) {
scrolltext += (char)pgm_read_byte_near(pgm_read_word(&(daftarhari[now.dayOfTheWeek()])) + z);
}
delay(1);
kudet = gaul;
scrolltext += (String)" " + now.year() + "-" + now.month() + "-" + now.day();
for(byte i = 0; i < sizeof(jadwal)/3; i++){
int totmnt = (now.hour() * 60) + now.minute();
int minjad = (pgm_read_byte(&(jadwal[i][0])) * 60) + pgm_read_byte(&(jadwal[i][1]));
int maxjad = minjad + pgm_read_byte(&(jadwal[i][2]));
if (totmnt >= minjad && totmnt <= maxjad){
strcpy_P(buffer, (char *)pgm_read_word(&(jws[i])));
scrolltext += (String)buffer;
spd = 150;
break;
}
}
}
//top line row
//1 seconds interval timer, clock is ticking
//interval 1 detik untuk animasi detik ':' kelap kelip
if (gaul - jadul >= 1000) {
jadul = gaul;
(strchr(jamenit,':') > 0) ? strcpy(jamenit, "hh mm") : strcpy(jamenit, "hh:mm");
dmd.drawString(1,0,(String)now.toString(jamenit) + " ");
}
//bottom line row
if (gaul - lawas >= spd) {
lawas = gaul;
box.print(scrolltext.charAt(cetak));
cetak++;
if(cetak > scrolltext.length()) cetak = 0;
}
// adjust RTC datetime by sending typed commands via serial monitor eg: 2021, 4, 27, 23, 45, 55
// pengaturan setting waktu
while (Serial.available() > 0) {
uint16_t thn = Serial.parseInt();
uint8_t bln = Serial.parseInt();
uint8_t tgl = Serial.parseInt();
uint8_t jam = Serial.parseInt();
uint8_t mnt = Serial.parseInt();
uint8_t dtk = Serial.parseInt();
delay(5);
if (Serial.read() == '\n') {
rtc.adjust(DateTime(thn, bln, tgl, jam, mnt, dtk));
Serial.println();
Serial.println("RTC has been adjusted!");
}
}
}