Newbie Help Needed! Inactivity timeout code w/ DS1302
Im am new to Basic programming, minus a brief semester in in high school.
' {$STAMP BS2}
{$PBASIC 2.5}
I plan to use the demo code and RTC DS1302 at the following link: http://forums.parallax.com/showthread.php?77867-DS1302-Demo-Code-(Updated)
I am trying to write a program that uses a real-time-clock to perform an action 3 times each day. (turn lights on) I believe that I can figure out the programming for this action but I am having trouble trying to figure out a way to pause the program when there is no one in the home. I plan to connect a micro switch to the door which will send a signal to the stamp, letting it know that someone is home and to resume the program. But if no signal is received within three hours of the next time the lights are to go on then I want the program to pause until the next time it receives a signal. I think the name of this type of program is an inactivity timeout?
Any help would be greatly appreciated!!!
' {$STAMP BS2}
{$PBASIC 2.5}
I plan to use the demo code and RTC DS1302 at the following link: http://forums.parallax.com/showthread.php?77867-DS1302-Demo-Code-(Updated)
I am trying to write a program that uses a real-time-clock to perform an action 3 times each day. (turn lights on) I believe that I can figure out the programming for this action but I am having trouble trying to figure out a way to pause the program when there is no one in the home. I plan to connect a micro switch to the door which will send a signal to the stamp, letting it know that someone is home and to resume the program. But if no signal is received within three hours of the next time the lights are to go on then I want the program to pause until the next time it receives a signal. I think the name of this type of program is an inactivity timeout?
Any help would be greatly appreciated!!!

Comments
Your program would have two loops where it would check the door sensor. In one loop, it would wait for the door sensor to go on. This loop represents someone being at home, waiting for them to leave. If the sensor goes on, the program does a GOTO to the other loop. The second loop also waits for the door sensor to go on. This loop represents no one at home, waiting for them to arrive. If the sensor goes on, the program does a GOTO to the first loop. Inside the second loop, the program reads the RTC and compares the time to one of three values and turns the lights on if there's a match. Presumably there's some other set of values for turning the lights off.
If you want an "inactivity" timer instead, you'd have one loop that tests the door sensor and reads the RTC. If the door sensor goes on, the RTC value is saved as the "time of the last activity". Every time through the loop, the program subtracts the saved RTC value from the current RTC value (adjusting for different days). If the result is greater than 3 hours, the program compares the current time to the 3 times when an action should be done. If any of the comparisons match, the program turns the lights on. Presumably, there are other times when the lights should be turned off.
Thank you so much for the help! I was wondering if you could take a look at the attached code and see if it would work? I try debugging and keep getting an error when it gets ELSE and the ENDIF statement in the following:
Run_Door:
IF door = 1 THEN lastactive = hrs
IF (((hrsA = hrs) AND (minsA = mins) AND (secsA = secs)) OR ((hrsB = hrs) AND (minsB = mins) AND (secsB = secs)) OR ((hrsC = hrs) AND (minsC = mins) AND (secsC = secs))) AND (hrs - lastactive <= 4) THEN
GOSUB Run_Lights ' Run lights program
ELSE
ENDIF
ELSE
ENDIF
RETURN
Run_lights:
HIGH light ' Turn on motor to feed
PAUSE
3000 ' keep lights on for "3 secs" amount of time needed
LOW light ' Turn off lights
RETURN
I'm unsure what to edit in order to correct this problem...
One thing you should do is get the Scott Edwards book on Basic Stamps, were he shows an application with code using a RTC.
If you provide your whole code we can show you where your problems are.
This is short hand and does not require an ENDIF
You want
Or
IF door = 1 THEN lastactive = hrs IF (((hrsA = hrs) AND (minsA = mins) AND (secsA = secs)) OR ((hrsB = hrs) AND (minsB = mins) AND (secsB = secs)) OR ((hrsC = hrs) AND (minsC = mins) AND (secsC = secs))) AND (hrs - lastactive <= 4) THEN GOSUB Run_Lights ' Run lights program ENDIF ENDIFbsnut,
Thank you for the motivational assistance, and i will definitely check out the book.
The code that I currently have is based on Chris Savage's code for the ds1302...
' ========================================================================= ' ' File...... DS1302_Template.bs2 ' Purpose... Template For DS1302 Clock & RAM Functions ' Author.... Chris Savage -- Parallax, Inc. ' E-mail.... csavage@parallax.com ' Started... ' Updated... 04-26-2006 ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' This is a template for using the DS1302 in your own program. The I/O pin ' declarations, constants, variables and subroutines are already in place ' for you to use. Please see the DS1302_Demo.bs2 code for a more detailed ' explanation of how each section works. You can remove what you don't use ' without issue. ' -----[ Revision History ]------------------------------------------------ ' -----[ I/O Definitions ]------------------------------------------------- DataIO PIN 0 ' DS1302.6 Clock PIN 1 ' DS1302.7 CS1302 PIN 2 ' DS1302.5 Door PIN 3 ' Door signal Feed PIN 4 ' Feed motor ' -----[ Constants ]------------------------------------------------------- WrSecs CON $80 ' Write Seconds RdSecs CON $81 ' Read Seconds WrMins CON $82 ' Write Minutes RdMins CON $83 ' Read Minutes WrHrs CON $84 ' Write Hours RdHrs CON $85 ' Read Hours CWPr CON $8E ' Write Protect Register WPr1 CON $80 ' Set Write Protect WPr0 CON $00 ' Clear Write Protect WrBurst CON $BE ' Write Burst Of Data RdBurst CON $BF ' Read Burst Of Data WrRam CON $C0 ' Write RAM Data RdRam CON $C1 ' Read RAM Data Hr24 CON 0 ' 24 Hour Mode Hr12 CON 1 ' 12 Hour Mode hrsA CON 07 ' Hour of feed time A hrsB CON 13 ' Hour of feed time B hrsC CON 19 ' Hour of feed time C minsA CON 30 ' Minutes of feed time A minsB CON 30 ' Minutes of feed time B minsC CON 30 ' Minutes of feed time C secsA CON 30 ' Seconds of feed time A secsB CON 30 ' Seconds of feed time B secsC CON 30 ' Seconds of feed time C ' -----[ Variables ]------------------------------------------------------- index VAR Byte ' Loop Counter reg VAR Byte ' Read/Write Address ioByte VAR Byte ' Data To/From DS1302 secs VAR Byte ' Seconds secs01 VAR secs.LOWNIB secs10 VAR secs.HIGHNIB mins VAR Byte ' Minutes mins01 VAR mins.LOWNIB mins10 VAR mins.HIGHNIB hrs VAR Byte ' Hours hrs01 VAR hrs.LOWNIB hrs10 VAR hrs.HIGHNIB date VAR Byte month VAR Byte day VAR Nib ' Day year VAR Byte ' Year ampm VAR hrs.BIT5 ' AM/PM Flag Bit clockMode VAR hrs.BIT7 ' 12/24 Hour Mode Bit ampmFlag VAR Bit ' 0 = AM, 1 = PM modeFlag VAR Bit ' 0 = 24, 1 = 12 (Hours) work VAR Byte ' Work Data lastactive VAR Byte ' Last hour that door was activated ' -----[ EEPROM Data ]----------------------------------------------------- Sun DATA "SUN", 0 ' Day Abbreviations Mon DATA "MON", 0 Tue DATA "TUE", 0 ' These data statements could Wed DATA "WED", 0 ' contain the full day name and Thu DATA "THU", 0 ' the code would still work Fri DATA "FRI", 0 ' without change. Sat DATA "SAT", 0 ' -----[ Initialization ]-------------------------------------------------- Init: reg = CWPr ' Initialize DS1302 ioByte = WPr0 ' Clear Write Protect GOSUB RTC_Out ' Send Command ' -----[ Program Code ]---------------------------------------------------- Start: DO ' Place your main routines here GOSUB Get_Time ' Get The Current Date/Time GOSUB Run_Door ' Run door program GOSUB Show_Time ' Display It LOOP ' -----[ Subroutines ]----------------------------------------------------- Show_Time: ' This routine uses DEBUG to display the Date/Time information but you ' would customize the routine to display the data you want on the device ' you want. Not all information needs to be displayed. LOOKUP (day - 1), [Sun, Mon, Tue, Wed, Thu, Fri, Sat], work DO ' Display Day Abbreviation READ work, ioByte ' Read Each Character Into ioByte IF (ioByte = 0) THEN EXIT ' If 0 then done DEBUG ioByte ' Send Character To DEBUG Screen work = work + 1 ' Increment Address LOOP ' Next Character, If 0 Then Done modeFlag = clockMode ' Save 12/24 Hour Clock Mode clockMode = 0 ' Clear BIT7 ampmFlag = ampm ' Save AM/PM Flag IF modeFlag = 1 THEN ampm = 0 ' Clear BIT5 (12 Hour Mode only!) DEBUG " ", HEX2 month, "/", HEX2 date, "/", HEX2 year, " " IF (modeFlag = Hr24) THEN ' Check For 24 Hour Display mode DEBUG HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs ELSE DEBUG HEX2 hrs, ":", HEX2 mins ' Print Hours/Minutes IF ampmFlag = 0 THEN DEBUG "AM" ELSE DEBUG "PM" ENDIF DEBUG " [", HEX2 secs, "]" ENDIF RETURN RTC_Out: HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [reg, ioByte] LOW CS1302 ' Deselect DS1302 RETURN RTC_In: HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [reg] SHIFTIN DataIO, Clock, LSBPRE, [ioByte] LOW CS1302 ' Deselect DS1302 RETURN Set_Time: ' DS1302 Burst Write HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [WrBurst] SHIFTOUT DataIO, Clock, LSBFIRST, [secs, mins, hrs, date, month, day, year, 0] LOW CS1302 ' Deselect DS1302 RETURN Get_Time: ' DS1302 Burst Read HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [RdBurst] SHIFTIN DataIO, Clock, LSBPRE, [secs, mins, hrs, date, month, day, year] LOW CS1302 ' Deselect DS1302 RETURN Run_Door: IF door = 1 THEN lastactive = hrs IF (((hrsA = hrs) AND (minsA = mins) AND (secsA = secs)) OR ((hrsB = hrs) AND (minsB = mins) AND (secsB = secs)) OR ((hrsC = hrs) AND (minsC = mins) AND (secsC = secs))) AND (hrs - lastactive <= 4) THEN GOSUB Run_Feed ' Run feed program if time is ENDIF ENDIF RETURN Run_Feed: HIGH Feed ' Turn on lights PAUSE 3000 ' Continue for ~"3 secs" amount of time ' LOW Feed ' Turn off lights RETURNMike G,
Thank you! This worked perfectly! i didn't know that there was a shorthand version of this code as well! For peace of mind, how would one use this shorthand if they are doing multiple loops like the one that i tried to do? or is it not an action that can be performed???
I am sure that I will be back often throughout the next few months to ask many more questions on this code. Thank you again