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

Question for problem solvers!

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



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

Question for problem solvers!
PostPosted: Wed Sep 18, 2019 12:02 am     Reply with quote

Hi guys!

I am using a PIC24F16KA102 in a device which sprays perfume in predefined intervals. Sleeps, wakes, sprays, sets the next alarm, sleeps.

For example a spray program could be:

1) 08:30 - 14:30 every 5 minutes
2) 17:30 - 20:30 every 10 minutes

So before going into sleep the device must calculate the next alarm with rollover at midnight etc.

I have been burning my mind to figure out the logic that I should follow but it is a little complicated Sad

Any ideas welcome.
_________________
George.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Wed Sep 18, 2019 12:51 am     Reply with quote

It is not clear from your question, whether '1' and '2' are two separate
program examples, or both to be running at the same time in one unit?.
If the latter, what is to be done if two signals occur at the same time?.
Double output?. Ignore the second?. Your examples have the two times
not overlapping, but you need to work out how to prevent this being
done, or handle if it does happen... (always assume people will input
unexpected values....).

The standard way to handle this type of timing, is to ignore 'times', and
think instead of just 'counts'. Turn a time of day, into a counts past
midnight figure.
Then a program simply has a start count, an end count, and an interval.
If the current count is greater than the start count for a program, and
less than the end count, then program is active. The next time required,
is simply (using integer maths) (((current count)/interval)*interval)+
interval. So (for instance) if you are at 08:34, for your first program:

current count = ((8*60)+34)*60 = 30840 (seconds)
next alarm = 30840/(5*60) = 102. 102*300 = 30600. 30600+300 =
30900.

So the next alarm is in 30900-30840 seconds (60 seconds) at 8:35.

Obviously you can decide whether a program does include it's end time
or not by testing for < or <= and the same with the start time.
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Wed Sep 18, 2019 1:10 am     Reply with quote

Hello Ttelmah!

Thanks for taking the time to reply.

I already made 2 functions that turn hours-minutes-seconds to seconds and back to hours-minutes-seconds so that I will have to deal with one number only. ( the count you are talking about).
For simplicity we assume that the programs do not overlap. I will deal with this later.
The 1+2 programs are in the same machine and active at the same time.
Now let me read again your solution and try to understand.

...and keep in mind that finally it will have 8 sets of programs. Each set has day of the week setting and 3 start-finish / interval sub programs.

Thanks!!!
_________________
George.
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Wed Sep 18, 2019 1:35 am     Reply with quote

Also the trick is that you have to calculate the next alarm even if the actual "now" time is before the "active" time.


For example if now is 08:30 and the program starts at 12:00, before going to sleep I have to set alarm for 12:00.

Also there is not enough eeprom to store all intervals, I have to calculate them real time and compare before going to sleep.
_________________
George.
temtronic



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

View user's profile Send private message

PostPosted: Wed Sep 18, 2019 4:42 am     Reply with quote

My 30+ year old remote energy control system breaks 24 hrs into 15 minute slices. IE 1:00, 1:15,1:30,1:45. Really, there's no need to keep track of seconds, so you only need 7 bits for time, leaving you the 8th bit as a 'control' flag ( 0=5, 1=10). That means you only need ONE byte, not 7 or 8 bytes.
The 'control' flag tells the program the 'spray interval' of 5 or 10 minutes.
Done this way you only need 2 bytes per 'spray operation'.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Wed Sep 18, 2019 8:28 am     Reply with quote

Agree with Jay.

Also it is worth realising that it probably costs more in terms of power
and a lot more in terms of code/time complexity to reprogram alarms all the
time.
Just have the system wake once per minute (single 'alarm'), and have
code that checks if this time matches a required event. Much simpler
to code and the chip need only operate for a few hundred machine cycles
every minute.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Sep 18, 2019 7:57 pm     Reply with quote

I liked this thread. Awesome.
_________________
CCS PCM 5.078 & CCS PCH 5.093
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