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 support@ccsinfo.com

Multi-part logic......

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



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

Multi-part logic......
PostPosted: Mon Apr 16, 2018 4:22 pm     Reply with quote

Hi All,

I'm trying to code a test for dates that fall within a certain date range, but a simple & obvious method is defying me at the moment.

Let's suppose I want to find out if a specific date falls between two end point dates, say March 11th & Nov. 4th. I've been able to code a test that works for the beginning date or the end date separately, but I haven't been able to combine them into a working piece of code that tests both date limits! Argh!!!

Here is a test program that I'm trying to make work!

Code:

#include <18F44K22.h>
#fuses INTRC_IO, NOWDT, PUT, NOLVP, NOPBADEN, FCMEN
#use delay(clock=8000000) 

#define PWR_LED Pin_B1
#define Serial_TxD Pin_B4   // serial debug port

#use rs232(baud=9600, xmit=Serial_TxD, stream = Diag)

void main()
{
   int8 iIndex;

   signed int8 UTCOffset = 0;
   unsigned int16 iMonth = 0;
   unsigned int16 iDay = 0;
   unsigned int8 iDayMax = 0;

   // Here we turn off the Pwr LED
   output_low(PWR_LED);
   delay_ms(500);

   // Here we blip the Power LED at power-up to show that the Lighting interface is working
   for ( iIndex = 0 ; iIndex < 3 ; iIndex++ )
   {
      output_high(PWR_LED);
      delay_ms(250);
      output_low(PWR_LED);
      delay_ms(250);
   }
      
   // Here we leave the Power LED ON
   output_high(PWR_LED);

   for (iMonth = 1; iMonth <= 12; iMonth++)
   {

      switch (iMonth)
      {
         Case 1:
         Case 3:
         Case 5:
         Case 7:
         Case 8:
         Case 10:
         Case 12:
         iDayMax = 31;
           break;

         Case 2:
         iDayMax = 28;
         break;

         Case 4:
         Case 6:
         Case 9:
         Case 11:
         iDayMax = 30;
         break;
      }

      for (iDay = 1 ; iDay <= iDayMax ; iDay++)
      {

         //Here we do the UTC offset correction!
         if (((((iMonth >= 3) && (iDay >= 11)) || (iMonth > 3))) && ((((iMonth <= 11) && (iDay <= 4)) || (iMonth < 11))))
            UTCOffset = -4;
         else
            UTCOffset = -5;

         fprintf(Diag, "Month: %Lu  Day: %Lu  UTCOffset: %d\n\r", iMonth, iDay, UTCOffset);
        }      

   }

   while(1){}

}


Any input on the proper test I need to be performing to make the this code work as intended?

Thanks!

Jack


Last edited by JAM2014 on Tue Apr 17, 2018 2:01 pm; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9093
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 17, 2018 5:37 am     Reply with quote

Code:

//function to decide if DST or EST
void dst_calc() {
if((rtcdow==0) && (rtcmth==3)&&(rtcday>=8)&&(rtcday<=16)&&(rtchrs==0)&&(rtcmin==0)&&(rtcsec==0)&&(dstflg==0))
{
rtchrs=rtchrs+1;
//ds3231_set_date_time(rtcyer,rtcmth,rtcday,rtcdow,rtchrs,rtcmin,rtcsec);   //spring ahead !
dstflg=1;
lcd_gotoxy(15,2);
printf(lcd_putc,"DST");
//add rtc eeprom saving here !
}
else if ((rtcdow==0) && (rtcmth==11)&&(rtcday>=1)&&(rtcday<=8)&&(rtchrs==00)&&(rtcmin==0)&&(rtcsec==0)&&(dstflg==1))
{
rtchrs=rtchrs-1;
//ds3231_set_date_time(rtcyer,rtcmth,rtcday,rtcdow,rtchrs,rtcmin,rtcsec);   //spring ahead !dstflg=0;
dstflg=0;
lcd_gotoxy(15,3);
printf(lcd_putc,"noDST");
//add rtc eeprom storing here !
}


variables rtcxxx are the data from the rtc module,dtslfg is the daylight savings time flag.


OK, here's a quick cut and paste from my chickencoop project
It ain't pretty but I'll get back to it AFTER I get 'sunset' to be correct.
Got 18/4/13 to be correct( 20:00 instead of -4:00) by changing the 'loop' that put hrs within 0 to 24) but it fails around day 160ish..sunset at -3:00 !

Jay
JAM2014



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

PostPosted: Tue Apr 17, 2018 2:08 pm     Reply with quote

Hi All,

I needed to get away from this problem for a while to clear my head and be able to 'see the forest thru the trees'! Anyway, I've got the code working, and I've updated the original post with working code!

I haven't done much time/date arithmetic, but it always seems to be pretty gnarly!

Thanks,

Jack
temtronic



Joined: 01 Jul 2010
Posts: 9093
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 17, 2018 7:25 pm     Reply with quote

yes, you can lose a LOT of sleep over 'time math' ! Back when RAM and EPROMs were pricey, I figured out how to store time in 1 byte. As long as you're OK with 15 minute 'intervals', you can do it in 7 bits, leaving a bit to be the 'control' bit to turn a device on or off. Being one byte makes 'time math' a lot easier and faster,especially when using PICs !
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