CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Times calculating problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ILLIAS28



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

Times calculating problem
PostPosted: Sat Apr 25, 2020 10:38 am     Reply with quote

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
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Sat Apr 25, 2020 11:13 am     Reply with quote

One problem is you are using "int" which is 32 to 64 bits on an online compiler and 8bits on a PIC18.

You either need to change your ints to int32 or in your math cast them. For example, 367*year will always overflow when year is 8bits as an 8bit int can only hold -128 to 127.
ILLIAS28



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

TIMES CALCULATING PROBLEM
PostPosted: Sat Apr 25, 2020 1:22 pm     Reply with quote

thank you very much Jerimiah for your help;i did what you told me , i have changed all INT in INT32 and it works very good now.
many many thanks once again.
_________________
i am newbe
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group