Bel
Bel
Bel
h>
#include <DMD3asis.h>
#include <font/Angka6x16.h>
#include <font/Droid_Sans_12.h>
#include <font/huruf6x7.h>
#include <font/Mono5x7.h>
#include <avr/pgmspace.h>
#include <DS3231.h>
#include <Wire.h>
float LA = -6.943;
float LO = 113.573;
int AL = 0;
int TZ = 7;
int BL = 100;
typedef struct {
uint8_t hD;
uint8_t hM;
uint16_t hY;
} hijir_date;
hijir_date nowH;
DateTime now;
void setup() {
Wire.begin();
Serial.begin(9600);
now = RTC.now();
update_All_data();
Disp_init();
pinMode (pinBel, OUTPUT);
pinMode (pinLamp1, OUTPUT);
pinMode (pinLamp2, OUTPUT);
pinMode (pinLamp3, OUTPUT);
digitalWrite(pinBel, HIGH);
digitalWrite(pinLamp1, HIGH);
digitalWrite(pinLamp2, HIGH);
digitalWrite(pinLamp3, HIGH);
}
void loop() {
now = RTC.now();
update_All_data();
SerCom();
DoSwap = false;
Disp.clear();
AnimSquence();
}
void Disp_init() {
Disp.setDoubleBuffer(true);
Timer1.initialize(2000);
Timer1.attachInterrupt(scan);
Timer1.pwm(9, BL);
Disp.clear();
Disp.swapBuffers();
}
void scan() {
Disp.refresh();
}
void update_All_data() {
sholatCal();
hijryCal();
check_azzan();
cekaktif();
jadwalbel();
kegiatan();
cekLamp();
}
void cekLamp() {
if ((jamnow() >= ShT[6] - (float) 10/60) || (jamnow() < ShT[2] - (float) 30/60))
{
digitalWrite(pinLamp1, LOW);
}
else {
digitalWrite(pinLamp1, HIGH);
}
}
void SerCom() {
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
input.trim();
if (input.startsWith("NTG")) {
Clock.setDate(input.substring(3).toInt());
} else if (input.startsWith("NBL")) {
Clock.setMonth(input.substring(3).toInt());
} else if (input.startsWith("NTH")) {
Clock.setYear(input.substring(3).toInt());
} else if (input.startsWith("NJM")) {
Clock.setHour(input.substring(3).toInt());
} else if (input.startsWith("NMT")) {
Clock.setMinute(input.substring(3).toInt());
} else if (input.startsWith("NDT")) {
Clock.setSecond(input.substring(3).toInt());
} else if (input.startsWith("NHR")) {
Clock.setDoW(input.substring(3).toInt());
} else {
Serial.println("ID tidak dikenal");
}
}
}
int daynow() {
int d = Clock.getDoW();
return d;
}
float jamnow() {
float T = (float) now.hour() + (float) now.minute() / 60.0 + (float)
now.second() / 3600.0;
return T;
}
void sholatCal() {
float EJD = E_Julian_date(now.year(), now.month(), now.day(), LO);
float Decl = Dql(EJD);
float EqOfTime = EqT(EJD);
Pray_Time(TZ, LA, LO, AL, Decl, EqOfTime);
}
float E_Julian_date(int Year, int Month, int Days, float Longitude) {
if (Month <= 2) {
Year -= 1;
Month += 12;
}
float A = floor(((float)Year / 100.0));
float B = 2 - A + floor(A / 4.0);
float CLong = Longitude / (float)(15 * 24);
float JD = floor(365.25 * (float)(Year + 4716))
- 2451545
+ floor(30.6001 * (float)(Month + 1))
+ (float)Days + B
- 1524.5
- CLong;
return JD;
}
float fix_hour(float a) {
a = a - (float)24.0 * floor(a / 24.0);
a = a < 0.0 ? a + 24.0 : a;
return a;
}
float fix_angle(float a) {
a = a - (float)360.0 * floor(a / 360.0);
a = a < 0.0 ? a + 360.0 : a;
return a;
}
void check_azzan() {
SholatNow = -1;
for(int i=0; i <=7; i++) {
if(jamnow() >= ShT[i]){
SholatNow = i;
if(!azzan and (jamnow() > ShT[i]) and (jamnow() < (ShT[i]+0.03))) {
azzan =true;
RunS = 100;
}
}
}
}
void hijryCal(){
uint8_t date_cor = 0;
if (jamnow() > ShT[6]) {
date_cor = 1;
}
nowH = toHijri(now.year(), now.month(), now.day(), date_cor);
}
void AnimSquence() {
AnimJAM(30, 2000, 1);
dwMrq(drawDayDate(), 40, 1, 2);
AnimJAM(30, 2000, 3);
drawKBM(40, 4);
dwMrqMapel(40, 5);
AnimJAM(30, 2000, 6);
drawSholat(7);
AnimJAM(30, 2000, 8);
drawLokal(40, 9);
drawAzzan(100);
drawBell(50,101);
if (RunF == 1) {
RunS = 2;
RunF = 0;
}
if (RunF == 2) {
RunS = 3;
RunF = 0;
}
if (RunF == 3) {
if (hAktif) {
RunS = 4;
RunF = 0;
}
else {
RunS = 7;
RunF = 0;
}
}
if (RunF == 4) {
if (kamjum) {
RunS = 6;
RunF = 0;
}
else {
RunS = 5;
RunF = 0;
}
}
if (RunF == 5) {
RunS = 6;
RunF = 0;
}
if (RunF == 6) {
RunS = 7;
RunF = 0;
}
if (RunF == 7) {
if (hAktif) {
RunS = 8;
RunF = 0;
}
else {
RunS = 1;
RunF = 0;
}
}
if (RunF == 8) {
RunS = 9;
RunF = 0;
}
if (RunF == 9) {
RunS = 1;
RunF = 0;
}
if(RunF==100){
reset_x=1;
RunS = 1;
RunF =0;
}
if(RunF==101){
reset_x=1;
RunS = 1;
RunF =0;
}
if (DoSwap) {
Disp.swapBuffers();
}
}
void AnimJAM(int speed, int delay, int DrawAdd) {
if (!dwDo(DrawAdd)) return;
static uint8_t y;
static uint8_t s;
static uint16_t lsRn;
uint16_t Tmr = millis();
if ((Tmr - lsRn) > 30) {
if (s == 0 && y < 17) {
lsRn = Tmr;
y++;
}
if (s == 1 && y > 0) {
lsRn = Tmr;
y--;
}
}
if ((Tmr - lsRn) > 3000 && y == 17) {
s = 1;
}
if (y == 7) {
}
if (y == 0 && s == 1) {
dwDone(DrawAdd);
s = 0;
}
JAM_B(17 - y);
}
void JAM_B(uint16_t y) {
static uint16_t lsRn;
uint16_t Tmr = millis();
char BuffJ[6];
char BuffM[6];
char BuffD[6];
sprintf(BuffJ, "%02d", now.hour());
sprintf(BuffM, "%02d", now.minute());
sprintf(BuffD, "%s", ":");
Disp.setFont(fontAbesar);
Disp.drawText(0, y, BuffJ);
Disp.drawText(19, y, BuffM);
Disp.setFont(fontUmum);
if (Tmr - lsRn < 500) Disp.drawText(13, y + 5, BuffD);
if (Tmr - lsRn > 1000) lsRn = Tmr;
DoSwap = true;
}
void JAM_K(uint16_t y) {
static uint16_t lsRn;
uint16_t Tmr = millis();
char BuffJ[6];
char BuffM[6];
char BuffD[6];
sprintf(BuffJ, "%02d", now.hour());
sprintf(BuffM, "%02d", now.minute());
sprintf(BuffD, "%s", ":");
Disp.setFont(fontUmum);
Disp.drawText(0, y, BuffJ);
Disp.drawText(19, y, BuffM);
if (Tmr - lsRn < 500) Disp.drawText(13, y, BuffD);
if (Tmr - lsRn > 1000) lsRn = Tmr;
DoSwap = true;
}
void dwMrq(const char * msg, int Speed, int dDT, int DrawAdd) {
static uint16_t x;
if (!dwDo(DrawAdd)) return;
if (reset_x != 0) {
x = 0;
reset_x = 0;
}
static uint16_t lsRn;
int fullScroll = Disp.textWidth(msg) + Disp.width();
uint16_t Tmr = millis();
if ((Tmr - lsRn) > Speed) {
lsRn = Tmr;
if (x < fullScroll) {
++x;
} else {
dwDone(DrawAdd);
x = 0;
return;
}
if (dDT == 1) {
if (x <= 6) {
JAM_K(x - 6);
} else if (x >= (fullScroll - 6)) {
JAM_K((fullScroll - x) - 6);
} else {
JAM_K(0);
}
Disp.setFont(fontUmum);
Disp.drawText(Disp.width() - x, 9, msg);
} else {
Disp.setFont(fontHbesar);
Disp.drawLine(1, 2, 62, 2);
Disp.drawLine(1, 13, 62, 13);
Disp.drawText(Disp.width() - x, 4, msg);
}
DoSwap = true;
}
}
if (!dwDo(DrawAdd)) return;
if (reset_x != 0) {
x = 0;
reset_x = 0;
}
if (formal) {
int fullScroll = Disp.textWidth(MAPELN(KegNow)) + Disp.width();
if ((Tmr - lsRn) > Speed) {
lsRn = Tmr;
if (x < fullScroll) {
++x;
} else {
dwDone(DrawAdd);
x = 0;
return;
}
Disp.setFont(fontHbesar);
Disp.drawLine(0, 0, 32, 0);
Disp.drawLine(0, 15, 32, 15);
Disp.drawText(Disp.width() - x, 3, MAPELN(KegNow));
DoSwap = true;
}
}
else {
int fullScroll = Disp.textWidth(MAPELN(jpa)) + Disp.width();
if ((Tmr - lsRn) > Speed) {
lsRn = Tmr;
if (x < fullScroll) {
++x;
} else {
x = 0;
jpa=jpa+1;
return;
}
if (jpa > 3) {
dwDone(DrawAdd);
jpa = 1;
}
Disp.setFont(fontUmum);
char J [2];
sprintf(J,"%0d", jpa);
Disp.drawText(0, 0, "JAM");
Disp.drawText(26, 0, J);
Disp.drawText(Disp.width() - x, 9, MAPELN(jpa));
DoSwap = true;
}
}
}
Disp.setFont(fontUmum);
Disp.drawText(0, 9, BuffJ);
Disp.drawText(13, 9, BuffD);
Disp.drawText(19, 9, BuffM);
Disp.drawText(Disp.width() - x, 0, getKegiatan(keg, daynow()));
DoSwap = true;
}
}
Disp.setFont(fontUmum);
Disp.drawText(0, 9, BuffJ);
Disp.drawText(13, 9, BuffD);
Disp.drawText(19, 9, BuffM);
Disp.drawText(Disp.width() - x, 0, getKegiatan(keg, daynow()));
DoSwap = true;
}
}
void cekaktif(){
if ((daynow() == 7 && jamnow() > ShT[2]) || (daynow() == 1 && jamnow() < ShT[2]))
{
hAktif = false;
formal = false;
ekskul = false;
klokal = false;
kamjum = false;
} else {
hAktif = true;
if ((jamnow() > 7 + (float)30/60 && jamnow() < 9 + (float)50/60) || (jamnow() >
10 + (float)20/60 && jamnow() < 11 + (float)30/60 )) {
formal = true;
} else {
formal = false;
}
if (jamnow() > ShT[5] && jamnow() < ShT[2]) {
klokal = true;
} else {
klokal = false;
}
if (daynow() == 6) {
ekskul = true;
} else {
ekskul = false;
}
if (daynow() == 6 && jamnow() >= jadnow(11,30)) {
kamjum = true;
} else {
kamjum = false;
}
}
}
void kegiatan() {
KegNow = 0;
tKeg[ 1] = jadnow( 7,30);
tKeg[ 2] = jadnow( 8,40);
tKeg[ 3] = jadnow( 9,50);
tKeg[ 4] = jadnow(10,20);
tKeg[ 5] = jadnow(11,30);
tKeg[ 6] = ShT[5] + (float) 10/60;
tKeg[ 7] = ShT[6] + (float) 10/60;
tKeg[ 8] = ShT[7] + (float) 10/60;
tKeg[ 9] = ShT[7] + 3 + (float) 30/60;
tKeg[10] = ShT[0] - (float) 20/60;
tKeg[11] = ShT[1] + (float) 10/60;
for (int i = 1; i < 12; i++) {
if (jamnow() >= tKeg[i] && jamnow() < tKeg[i + 1]) {
KegNow = i;
break;
}
}
}
void jadwalbel() {
if (hAktif == 1) {
jadbell( 1, 2, 1, 1);
jadbell( 2, 2, 1, 1);
jadbell( 3, 2, 1, 1);
jadbell( 4, 2, 1, 1);
jadbell( 5, 2, 1, 1);
jadbell( 9, 2, 1, 1);
jadbell(10, 2, 1, 1);
}
blink();
}
void blink() {
if (isBlinking) {
unsigned long currentMillisBel = millis();
if (belState == LOW && (currentMillisBel - prevMillisBel >= onDuration)) {
prevMillisBel = currentMillisBel;
belState = HIGH;
digitalWrite(pinBel, belState);
Serial.println("LED OFF");
} else if (belState == HIGH && (currentMillisBel - prevMillisBel >=
offDuration)) {
prevMillisBel = currentMillisBel;
belState = LOW;
digitalWrite(pinBel, belState);
Serial.println("LED ON");
blinkCount++;
}
if (blinkCount >= blinkTarget) {
isBlinking = false;
digitalWrite(pinBel, HIGH);
Serial.println("LED OFF");
Serial.println("====");
}
}
}