// Sketch uses 14324 bytes (44%) of program storage space.
// Maximum is 32256 bytes.
// Global variables use 515 bytes (25%) of dynamic memory,
// leaving 1533 bytes for local variables. Maximum is 2048 bytes.
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define CITY "Karachi"
#define LATITUDE 24.860966 // Latitude of Karachi.
#define LONGITUDE 66.990501 // Longitude of Karachi.
//#define CITY "Lahore"
//#define LATITUDE 31.582045 // Latitude of Lahore.
//#define LONGITUDE 74.329376 // Longitude of Lahore.
//#define CITY "Islamabad"
//#define LATITUDE 33.738045 // Latitude of Islamabad.
//#define LONGITUDE 73.084488 // Longitude of Islamabad.
#define degRad 57.296
#define TIMEZONE +5.0 // GMT timezone of Pakistan.
#define TAU 2.0 // Correction in minutes.
//#define MASLAK "Shaafai"
//#define maslak 1.0
#define MASLAK "Hanbali"
#define maslak 2.0
const String SALAH[6] = {"-", "Fajr", "Zuhar", "Asr", "Maghrb", "Isha"};
float TSalah[6];
float TRemaining;
int d; //# of days from Jan 1 of current year.
float delta; // inclination of sun in degrees.
float D; // Used in deltaT (EoT).
float deltaT; // equation of time EOT in minutes.
float TFajr, TSunrise, TZuhar, TAsr, TMaghrib, TIsha; // time of prayers in decimal.
int Salah; // Salah number, 1=Fajr, ... , 5=Isha.
float TNow; // Current time in decimal.
float TNow1; // Current time in decimal.
int DAY, MONTH, YEAR; // // Current date, month, year.
int loopCounter;
bool colon;
int n;
char s[25];
char ampm[] = "--";
bool s1=false;
bool s2=false;
bool s3=false;
bool s4=false;
bool s5=false;
const unsigned long onDuration = 5000;
const unsigned long onDuration1 = 8000;
const unsigned long onDuration2 = 10000;
const unsigned long onDuration3 = 13000;
const unsigned long onDuration4 = 18000;
//RTC_DS1307 rtc;
RTC_DS3231 rtc;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
if (!rtc.begin()) {
lcd.print("Couldn't find RTC.");
while (1);
}
//if (!rtc.isrunning()) { //DS1307
if (rtc.lostPower()) { // DS3231
lcd.print("Set date and time.");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set date and time of RTC at the compile time of the project.
}
//rtc.adjust(DateTime(2024, 3, 7, 8, 31, 0)); // YYYY, MM, DD, hh, mm, ss
}
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
if (now.isPM()) strcpy(ampm, "PM");
else strcpy(ampm, "AM");
if (colon == true) {
colon = !colon;
n = sprintf(s, " %s %d:%02d ", ampm, now.twelveHour(), now.minute());
}
else {
colon = !colon;
n = sprintf(s, " %s %d %02d ", ampm, now.twelveHour(), now.minute());
}
n = (20 - n) / 2;
lcd.setCursor(n, 0);
lcd.print(s);
char buf1[] = "DDD, DD MMM YYYY";
n = sprintf(s, " %s ", now.toString(buf1));
n = (20 - n) / 2;
lcd.setCursor(n, 1);
lcd.print(s);
if ((loopCounter % 10) == 0) {
float temperature = 23.1; //degree centigrade.
float humidity = 43.1; // % relative humidity.
float pressure = 996.1; // millibar.
if (pressure < 1000) n = 5; else n = 6;
char a[8], b[8], c[8];
n = sprintf(s, "%sC %s%c %smb", dtostrf(temperature, 4, 1, a), dtostrf(humidity, 4, 1, b), 37, dtostrf(pressure, n, 1, c));
n = (20 - n) / 2;
lcd.setCursor(n, 2);
lcd.print(s);
}
if ((loopCounter % 30) == 0) {
DAY = now.day();
MONTH = now.month();
YEAR = now.year();
TNow = now.hour() + (now.minute() / 60.0); // Current time in decimal.
TNow1 = now.hour() + now.minute() ;
d = DAY + 30.4 * (MONTH - 1);
delta = -23.45 * cos((360.0 / 365.0) * (d + 10.0) / degRad);
D = 6.24004077 + 0.017220197 * (365.25 * (YEAR - 2000) + d);
deltaT = 9.863 * sin((2 * D + 3.5932) / degRad) - 7.659 * sin(D / degRad);
TZuhar = 12.0 + (TIMEZONE - LONGITUDE / 15.0) - (deltaT / 60.0) + (TAU / 60.0); // time of zuhar in decimals.
TFajr = TZuhar - Talpha(-18.0, delta);
TSunrise = TZuhar - Talpha(-0.833, delta);
TAsr = TZuhar + Talpha(acot(maslak, delta), delta);
TMaghrib = TZuhar + Talpha(-0.833, delta);
TIsha = TZuhar + Talpha(-18.0, delta);
TSalah[1] = TFajr;
TSalah[2] = TZuhar;
TSalah[3] = TAsr;
TSalah[4] = TMaghrib;
TSalah[5] = TIsha;
Salah = 0;
for (int N = 5; N > 0; N--) if (TSalah[N] > TNow) Salah = N;
if (Salah == 0) {
Salah = 1;
TRemaining = 24.0 + TSalah[Salah] - TNow;
}
else TRemaining = TSalah[Salah] - TNow;
n = sprintf(s, "%s %s %s", timePrint(TSalah[Salah]).c_str(), SALAH[Salah].c_str(), timeRemaining(TRemaining).c_str());
n = (20 - n) / 2;
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(n, 3);
lcd.print(s);
}
Serial.println(TFajr);
delay(1000);
Serial.println(TNow);
loopCounter++;
//--------------1---------------------
if (TNow ==23.90) {
Serial.print("oki");
s1 = true;
}
if (millis() >= onDuration) {
// Turn the LED off
s1 = false;
}
if (s1 == true) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
//------------2-----------------------
if (TNow == TZuhar) {
Serial.print("oki");
s2 = true;
}
if (millis() >= onDuration1) {
// Turn the LED off
s2 = false;
}
if (s2 == true) {
digitalWrite(3, HIGH);
} else {
digitalWrite(3, LOW);
}
//-------------3----------------------
if (TNow == TAsr) {
Serial.print("oki");
s3 = true;
}
if (millis() >= onDuration2) {
// Turn the LED off
s3 = false;
}
if (s3 == true) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
//--------------4---------------------
if (TNow == TMaghrib) {
Serial.print("oki");
s4 = true;
}
if (millis() >= onDuration3) {
// Turn the LED off
s4 = false;
}
if (s4 == true) {
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
//--------------5---------------------
if (TNow == TIsha) {
Serial.print("oki");
s5 = true;
}
if (millis() >= onDuration4) {
// Turn the LED off
s5 = false;
}
if (s5 == true) {
digitalWrite(6, HIGH);
} else {
digitalWrite(6, LOW);
}
}
float Talpha(float alpha, float delTa) {
return acos((sin(alpha / degRad) - sin(delTa / degRad) * sin(LATITUDE / degRad)) / cos(delTa / degRad) / cos(LATITUDE / degRad)) * degRad / 15.0;
}
float acot(float n, float delTa) {
float theeta = n + abs(tan((LATITUDE - delTa) / degRad));
return degRad * atan(1 / theeta);
}
String timePrint(float T) {
String tR = "";
int hh = int(T);
int mm = (T - int(T)) * 60.0;
if (hh < 13) tR = String(hh);
else tR = String(hh - 12);
tR = tR + String(":");
if (mm < 10) tR = tR + String("0") + String(mm);
else tR = tR + String(mm);
if (hh < 12) tR = tR + String("A");
if (hh > 11 && hh < 24) tR = tR + String("P");
return tR;
}
/*
void printAll(void) {
Serial.print("Date: " + String(DAY) + "." + String(MONTH) + "." + String(YEAR));
Serial.println(", " + String(CITY));
Serial.print("TNow: " + String(TNow) + " = ");
timePrint(TNow);
Serial.print("TFajr: " + String(TFajr) + " = ");
timePrint(TFajr);
Serial.print("TSunrise: " + String(TSunrise) + " = ");
timePrint(TSunrise);
Serial.print("TZuhar: " + String(TZuhar) + " = ");
timePrint(TZuhar);
Serial.print("TAsr: " + String(TAsr) + " = ");
timePrint(TAsr);
Serial.print("TMaghrib: " + String(TMaghrib) + " = ");
timePrint(TMaghrib);
Serial.print("TIsha: " + String(TIsha) + " = ");
timePrint(TIsha);
}
*/
String timeRemaining(float tt) {
String tR = "";
int hh = int(tt);
int mm = (tt - hh) * 60;
if (hh != 0 && mm != 0) tR = String(hh) + String("h ") + String(mm) + String("m");
if (hh == 0 && mm != 0) tR = String(mm) + String("m");
if (hh != 0 && mm == 0) tR = String(hh) + String("h");
if (hh == 0 && mm == 0) tR = "Time";
return tR;
}