PDA

View Full Version : Help on a project I'm working on



Sagid
04-14-2010, 04:14 AM
Hi,

I'm in need of a bit of help on a project that I'm working on and was wondering if anyone here can help. I'm trying to make a pill reminder. You know, something connected to a 7-day pill box and an alarm system that would sound and flash LEDs [possibly display a message on an LCD too] if the corresponding day's box is not opened on time. I'm using a parallax BS2p24 chip and all of the other parts that I'm using came from parallax as well. I've already written the code for the speaker and LEDs [I've determined the frequencies and "pause" times]. Currently I'm trying to get the timer portion set up. I'm using a DS1302 chip and I found some threads about it, but I was wondering if anyone knows if I am able to use the chip as an input for the controller. My idea overall is to have switches attached to each day's box and have the clock running so that for example, if the person was supposed to take the pill at 10:30am each day, then when 10:30 comes around on Sunday and the box isn't opened [switch would still be closed] then a "warning" alarm would sound but if the box was opened correctly [switch opened] then a "good job" alarm would sound. So you see, I need the clock to be an input for the controller so that the it would "know" what time it is and appropriately signal the alarm.

Please, any help that you can provide would be greatly appreciated.

rixter
04-14-2010, 04:30 AM
Another way to approach it would be to be cycling around with your code, monitoring the time and date from the DS1302 and comparing to a set of parameters and seeing what position the appropriate switches are in. This method also allows you to constantly monitor compartments that shouldn't be opened at all on the improper day.

Rick

stamptrol
04-14-2010, 06:14 AM
Sagid,

The ds1302 is a pretty good time keeper and Chris Savage has a demo program which will exercise every aspect of the chip. I think the demo program is on the Resources section of the Parallax site.

One technique for a job such as yours is to just count minutes, starting at, say, Sunday at 12:01 AM. Seven days of minutes is 10080 minutes, so is an easy number to work with in a Word variable.

As time passes, you know that 10:30 each day will occur at 630 minutes on Sunday, 2070 minutes on Mon, 3510 minutes on Weds, etc, etc. Reset to zero on Saturday at 12 midnight and repeat.

You could also use the clock's ability to tell the day of the week and just watch for the appropriate time to roll around.

Cheers,

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tom Sisk

http://www.siskconsult.com

Zoot
04-14-2010, 07:07 AM
If you use an RTC then I would use it for time, day of week, everything -- it will be easy AND you will have a battery or supercap (large capacitor) backup on the RTC -- so even if batteries die, or power goes out, or it resets, or whatever, the pill keeper will still be on track.


The logic for checking if the pill has been taken isn't too rough. It might look something like this in pseudo-code:





pillsTaken VAR Byte ' each bit position is a flag for whether or not the pill was taken that day...

Main:

GOSUB GetTimeAndDate ' not shown, but returns hour, minute, dayOfWeek (0-6 with 0 = sunday), etc., from DS1302 or similar

IF pillsTaken.BIT0(dayOfWeek) = 0 THEN ' flag for this day not set!!!!!! better check it
IF ( hour > 7 AND minute > 30 ) OR ( hour >= 8 ) THEN ' if 7:30 or later, take the pill!
' sound alarm!!!!
GOSUB Make_Loud_Beep
IF switch = 0 THEN ' active low switch, check it and see if they did it!
pillsTaken.BIT0(dayOfWeek) = 1 ' they did, so set flag
ENDIF
ENDIF
ENDIF

IF dayOfWeek = 0 AND hour = 0 AND minute <= 1 THEN ' at first minute of day on sunday, reset the flags
pillsTaken = 0
ENDIF


GOTO Main





If you get that, then you could extend it a bit by setting custom times for each day, or lighting an LED that corresponds to the correct day's pill compartment:




compartmentLeds VAR OUTL ' pins 0-7 with leds hooked up to pins 0-6; one for each compartment

pillsTaken VAR Byte ' each bit position is a flag for whether or not the pill was taken that day...

Start:

DIRL = $FF ' all outputs for leds


Main:

GOSUB GetTimeAndDate ' not shown, but returns hour, minute, dayOfWeek (0-6 with 0 = sunday), etc., from DS1302 or similar

IF pillsTaken.BIT0(dayOfWeek) = 0 THEN ' flag for this day not set!!!!!! better check it
IF ( hour > 7 AND minute > 30 ) OR ( hour >= 8 ) THEN ' if 7:30 or later, take the pill!
' sound alarm!!!!
GOSUB Make_Loud_Beep
compartmentLeds.BIT0(dayOfWeek) = ~compartmentLeds.BIT0(dayOfWeek) ' blink the led
IF switch = 0 THEN ' active low switch, check it and see if they did it!
pillsTaken.BIT0(dayOfWeek) = 1 ' they did, so set flag
ENDIF
ENDIF
ELSE
compartmentLeds.BIT0(dayOfWeek) = 0 ' make sure LED is off
ENDIF

IF dayOfWeek = 0 AND hour = 0 AND minute <= 1 THEN ' at first minute of day on sunday, reset the flags
pillsTaken = 0
ENDIF

GOTO Main


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST

1uffakind.com/robots/povBitMapBuilder.php (http://1uffakind.com/robots/povBitMapBuilder.php)
1uffakind.com/robots/resistorLadder.php (http://1uffakind.com/robots/resistorLadder.php)

Zoot
04-14-2010, 07:10 AM
P.S. -- one thing to think about might be to write the flags to EEPROM whenever a flag is set, and read it back from there. This will preserve status of flags even upon power out/up or reset. Since you'd only be writing once per day, you wouldn't have to worry about wearing out the EE.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST

1uffakind.com/robots/povBitMapBuilder.php (http://1uffakind.com/robots/povBitMapBuilder.php)
1uffakind.com/robots/resistorLadder.php (http://1uffakind.com/robots/resistorLadder.php)