ILLIAS28
Joined: 11 Jan 2011 Posts: 42
|
Times calculating problem |
Posted: Sat Apr 25, 2020 10:38 am |
|
|
Hi everybody,
Could you please help me to resolve the following problem:
I have found on a site a program in c++ to calculate the islamic prayers times the program is this one:
Code: | #include <iostream>
#include <math.h>
/*
Prayers calculator start
*/
//convert Degree to Radian
double degToRad(double degree)
{
return ((3.1415926 / 180) * degree);
}
//convert Radian to Degree
double radToDeg(double radian)
{
return (radian * (180/3.1415926));
}
//make sure a value is between 0 and 360
double moreLess360(double value)
{
while(value > 360 || value < 0)
{
if(value > 360)
value -= 360;
else if (value <0)
value += 360;
}
return value;
}
//make sure a value is between 0 and 24
double moreLess24(double value)
{
while(value > 24 || value < 0)
{
if(value > 24)
value -= 24;
else if (value <0)
value += 24;
}
return value;
}
//convert the double number to Hours and Minutes
void doubleToHrMin(double number, int &hours, int &minutes)
{
hours = floor(moreLess24(number));
minutes = floor(moreLess24(number - hours) * 60);
}
void calcPrayerTimes(int year, int month, int day,
double longitude, double latitude, int timeZone,
double fajrTwilight, double ishaTwilight,
double &fajrTime, double &sunRiseTime, double &zuhrTime,
double &asrTime, double &maghribTime, double &ishaTime)
{
double D = (367 * year) - ((year + (int)((month + 9) / 12)) * 7 / 4) + (((int)(275 * month / 9)) + day - 730531.5);
double L = 280.461 + 0.9856474 * D;
L = moreLess360(L);
double M = 357.528 + (0.9856003) * D;
M = moreLess360(M);
double Lambda = L + 1.915 * sin(degToRad(M)) + 0.02 * sin(degToRad(2 * M));
Lambda = moreLess360(Lambda);
double Obliquity = 23.439 - 0.0000004 * D;
double Alpha = radToDeg(atan((cos(degToRad(Obliquity)) * tan(degToRad(Lambda)))));
Alpha = moreLess360(Alpha);
Alpha = Alpha - (360 * (int)(Alpha / 360));
Alpha = Alpha + 90 * (floor(Lambda / 90) - floor(Alpha / 90));
double ST = 100.46 + 0.985647352 * D;
double Dec = radToDeg(asin(sin(degToRad(Obliquity)) * sin(degToRad(Lambda))));
double Durinal_Arc = radToDeg(acos((sin(degToRad(-0.8333)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
double Noon = Alpha - ST;
Noon = moreLess360(Noon);
double UT_Noon = Noon - longitude;
////////////////////////////////////////////
// Calculating Prayer Times Arcs & Times //
//////////////////////////////////////////
// 2) Zuhr Time [Local noon]
zuhrTime = UT_Noon / 15 + timeZone;
// Asr Hanafi
//double Asr_Alt =radToDeg(atan(2+tan(degToRad(latitude - Dec))));
// Asr Shafii
double Asr_Alt = radToDeg(atan(1 + tan(degToRad(latitude - Dec))));
double Asr_Arc = radToDeg(acos((sin(degToRad(90 - Asr_Alt)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
Asr_Arc = Asr_Arc / 15;
// 3) Asr Time
asrTime = zuhrTime + Asr_Arc;
// 1) Shorouq Time
sunRiseTime = zuhrTime - (Durinal_Arc / 15);
// 4) Maghrib Time
maghribTime = zuhrTime + (Durinal_Arc / 15);
double Esha_Arc = radToDeg(acos((sin(degToRad(ishaTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
// 5) Isha Time
ishaTime = zuhrTime + (Esha_Arc / 15);
// 0) Fajr Time
double Fajr_Arc = radToDeg(acos((sin(degToRad(fajrTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
fajrTime = zuhrTime - (Fajr_Arc / 15);
return;
}
/*
Prayers calculator end
*/
/*
Personal code. Calculating for Cairo.
Date: 18-1-2012
Longitude: 30.2
Latitude: 30
Time Zone: +2
Fajr Twilight: -19.5
Esha Twilight: -17.5
*/
int main() {
double fajr, sunRise, zuhr, asr, maghrib, isha;
calcPrayerTimes(2012,1,18, 30.2, 30, 2, -19.5, -17.5,
fajr, sunRise, zuhr, asr, maghrib, isha);
int hours, minutes;
doubleToHrMin(fajr, hours, minutes);
std::cout << "Fajr: " << hours << ":" << minutes << std::endl;
doubleToHrMin(sunRise, hours, minutes);
std::cout << "Sunrise: " << hours << ":" << minutes << std::endl;
doubleToHrMin(zuhr, hours, minutes);
std::cout << "Zuhr: " << hours << ":" << minutes << std::endl;
doubleToHrMin(asr, hours, minutes);
std::cout << "Asr: " << hours << ":" << minutes << std::endl;
doubleToHrMin(maghrib, hours, minutes);
std::cout << "Maghrib: " << hours << ":" << minutes << std::endl;
doubleToHrMin(isha, hours, minutes);
std::cout << "Isha: " << hours << ":" << minutes << std::endl;
}
|
I have tested this program in an online compiler and it works good, it gives the correct times to the corresponding date and location.
my problem is as follows: I tried to use this program with a pic18f252 to display the prayer times on a 4x20 lcd display, but the problem is the times displayed are incorrect. I have adapted the program as follows to work with pic 18f252:
Code: | #include <18F252.h>
//#DEVICE HIGH_INTS=true
#device PASS_STRINGS=IN_RAM
#fuses XT,NOWDT,PUT,NOPROTECT,NOBROWNOUT,NOLVP,NOCPD,NODEBUG,NOCPB
#use delay(clock=4000000)
//#use rs232(baud=19200,UART1,BITS=8,XMIT=PIN_C6,RCV=PIN_C7,PARITY=N,STOP=1,ERRORS)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <LCD420.c>
/*
Prayers calculator start
*/
//convert Degree to Radian
double degToRad(double degree)
{
return ((3.1415926 / 180) * degree);
}
//convert Radian to Degree
double radToDeg(double radian)
{
return (radian * (180/3.1415926));
}
//make sure a value is between 0 and 360
double moreLess360(double value)
{
while(value > 360 || value < 0)
{
if(value > 360)
value -= 360;
else if (value <0)
value += 360;
}
return value;
}
//make sure a value is between 0 and 24
double moreLess24(double value)
{
while(value > 24 || value < 0)
{
if(value > 24)
value -= 24;
else if (value <0)
value += 24;
}
return value;
}
//convert the double number to Hours and Minutes
void doubleToHrMin(double number, int &hours, int &minutes)
{
hours = floor(moreLess24(number));
minutes = floor(moreLess24(number - hours) * 60);
}
void calcPrayerTimes(int year, int month, int day,
double longitude, double latitude, int timeZone,
double fajrTwilight, double ishaTwilight,
double &fajrTime, double &sunRiseTime, double &zuhrTime,
double &asrTime, double &maghribTime, double &ishaTime)
{
double D = (367 * year) - ((year + (int)((month + 9) / 12)) * 7 / 4) + (((int)(275 * month / 9)) + day - 730531.5);
double L = 280.461 + 0.9856474 * D;
L = moreLess360(L);
double M = 357.528 + (0.9856003) * D;
M = moreLess360(M);
double Lambda = L + 1.915 * sin(degToRad(M)) + 0.02 * sin(degToRad(2 * M));
Lambda = moreLess360(Lambda);
double Obliquity = 23.439 - 0.0000004 * D;
double Alpha = radToDeg(atan((cos(degToRad(Obliquity)) * tan(degToRad(Lambda)))));
Alpha = moreLess360(Alpha);
Alpha = Alpha - (360 * (int)(Alpha / 360));
Alpha = Alpha + 90 * (floor(Lambda / 90) - floor(Alpha / 90));
double ST = 100.46 + 0.985647352 * D;
double Dec = radToDeg(asin(sin(degToRad(Obliquity)) * sin(degToRad(Lambda))));
double Durinal_Arc = radToDeg(acos((sin(degToRad(-0.8333)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
double Noon = Alpha - ST;
Noon = moreLess360(Noon);
double UT_Noon = Noon - longitude;
////////////////////////////////////////////
// Calculating Prayer Times Arcs & Times //
//////////////////////////////////////////
// 2) Zuhr Time [Local noon]
zuhrTime = UT_Noon / 15 + timeZone;
// Asr Hanafi
// double Asr_Alt =radToDeg(atan(2+tan(degToRad(latitude - Dec))));
// Asr Shafii
double Asr_Alt = radToDeg(atan(1 + tan(degToRad(latitude - Dec))));
double Asr_Arc = radToDeg(acos((sin(degToRad(90 - Asr_Alt)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
Asr_Arc = Asr_Arc / 15;
// 3) Asr Time
asrTime = zuhrTime + Asr_Arc;
// 1) Shorouq Time
sunRiseTime = zuhrTime - (Durinal_Arc / 15);
// 4) Maghrib Time
maghribTime = zuhrTime + (Durinal_Arc / 15);
double Esha_Arc = radToDeg(acos((sin(degToRad(ishaTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
// 5) Isha Time
ishaTime = zuhrTime + (Esha_Arc / 15);
// 0) Fajr Time
double Fajr_Arc = radToDeg(acos((sin(degToRad(fajrTwilight)) - sin(degToRad(Dec)) * sin(degToRad(latitude))) / (cos(degToRad(Dec)) * cos(degToRad(latitude)))));
fajrTime = zuhrTime - (Fajr_Arc / 15);
return;
}
/*
Prayers calculator end
*/
/*
Personal code. Calculating for Cairo.
Date: 18-1-2012
Longitude: 30.2
Latitude: 30
Time Zone: +2
Fajr Twilight: -19.5
Esha Twilight: -17.5
*/
int main() {
double fajr, sunRise, zuhr, asr, maghrib, isha;
calcPrayerTimes(2020,4,25, -13.202, 27.1568, 0, -19, -17.25,
fajr, sunRise, zuhr, asr, maghrib, isha);
int hours, minutes;
SET_TRIS_B(0x00) ;
lcd_init();
lcd_putc('\f');
doubleToHrMin(fajr, hours, minutes);
lcd_putc('\f');
printf(lcd_putc,"fajr=%02u:%02u",hours,minutes);
doubleToHrMin(zuhr, hours, minutes);
lcd_putc('\n');
printf(lcd_putc,"zuhr= %02u:%02u",hours,minutes);
doubleToHrMin(asr, hours, minutes);
lcd_putc('\n');
printf(lcd_putc,"asr= %02u:%02u",hours,minutes);
doubleToHrMin(maghrib, hours, minutes);
lcd_putc('\n');
printf(lcd_putc,"mag=%02u:%02u",hours,minutes);
doubleToHrMin(isha, hours, minutes);
printf(lcd_putc,"ish=%02u:%02u",hours,minutes);
return 0;
}
|
I used Proteus (Isis) to make the simulation. Here is the design for Isis:
[img]https://zupimages.net/viewer.php?id=20/17/f4l4.jpg[/img]
I have tried to change the "double" variable with "float" but it didn't change anything, the times are always incorrect. _________________ i am newbe |
|