Shop OBEX P1 Docs P2 Docs Learn Events
DS1302 and Led as Switch.............................. — Parallax Forums

DS1302 and Led as Switch..............................

sam_sam_samsam_sam_sam Posts: 2,286
edited 2006-08-20 20:33 in BASIC Stamp
·Hi, EveryOne······· smile.gif


I am working on another project this time

(1)·· I want to use a Led as switch to see the Sun Light
(2)·· I want to count the amount of hours Sun Light
(3)·· I want to check for Sun Light Every 15 minutes
(4)·· I want to have an Out Put Come on when the Sun Gose to SLEEP and Stay ON for·3 Hour in the SUMMER·and· Go to SLEEP
······ The amount of hours will not be the same as far the OUT PUT Staing On..... The Hours of·Day Light are not·the same
······· As the day·get shorter the amount the Out Put stay on will get longer
(5)··· I want to have an Out Put Come on when the Sun Gose to SLEEP and Stay ON for·6 Hour in the·WINTER·and· Go to SLEEP
(6)··· Now what i want to do here is to be able to ADJ the Amount of TIME the Out Put Stay On Based on the Amount of hour
of Sun Light and ADJ as it changes

I will be using a battery back up to keep the amout of hour of Day Light
I will be using a Led as my switch

In the ATTACHMENT·Day Light· Timer ·Lighting ·below i have some of what i have in mind I update comments
I would like to Thank any one that can help with this problem i not sure what way to go with this
I have a project that use the Led as a Switch and using a DS1302
I· wrote a routine for Day Light Pool Pump Timer ·it and work very well and when i finish the project i will post on the Forum

·I update comments·



·Clock_Run:
hrs· = $00
mins = $00
secs = $00

GOSUB Set_Time
DEBUG CLS, CLS

DO
DEBUG CR,CR, "START"

·· GOSUB Get_Time
· DEBUG HOME, HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs

·'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
· 'Is this right way to write this routine or is there a better way to this

· IF chg = 1 AND looks = 42 THEN
· GOSUB relay_On
· ENDIF

· IF chg = 1 AND looks = 40 THEN
· GOSUB relay_On
· ENDIF

· IF chg = 1 AND looks = 38 THEN
· GOSUB relay_On
· ENDIF

· 'And So On >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
· IF hrs = $00 AND mins = $15 AND secs = $00· THEN EXIT
· LOOP
· looks = looks + 1
· DEBUG CR, CR, "00:15:00 Elapsed"
· PAUSE 1000
·· GOSUB look

· 'Is this right way to write this routine or is there a better way to this
· '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'

[noparse][[/noparse] Subroutines ]

·Sam


Post Edited (sam_sam_sam) : 8/18/2006 11:06:36 PM GMT

Comments

  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2006-08-18 23:56
    Hi, EveryOne

    The problem i am having is that i am not sure the right way to write this routine

    ·IF chg = 1 AND looks = 42 THEN······· ·====··········· IF hrs = 03·AND mins = 00 AND secs = 00 THEN
    · GOSUB relay_On·····························====··········· GOSUB· relay_OFF
    · ENDIF·········································· ====··········· ENDIF


    · IF chg = 1 AND looks = 40 THEN······· ====··········· IF hrs = 03·AND mins =·30 AND secs = 00 THEN
    · GOSUB relay_On·····························====··········· GOSUB· relay_OFF
    · ENDIF·········································· ====·········· ·ENDIF


    · IF chg = 1 AND looks = 38 THEN······· ====··········· IF hrs = 04·AND mins = 00 AND secs = 00 THEN
    · GOSUB relay_On···························· ====············GOSUB· relay_OFF
    · ENDIF·········································· ====··········· ENDIF

    ·

    These would all be IF statement doing this way save space


    look = 36························ =========··· 4 hrs··30 mins· 00· secs
    look = 34························ =========····5 hrs··00 mins· 00· secs
    look = 32························ =========····6 hrs··30·mins··00· secs
    look·= 30························ =========··· 7 hrs· 00 mins· 00· secs

    I may all of these i am not sure at this point

    look = 28
    look = 26
    look = 24
    look = 22
    look = 20

    I may need to add more this part i do not this yet


    Is there and easier way to write this part of this routine
    ·I could write it this way

    I would like to not use so many IF statements
    Could this be done or do i just have use all of these· IF Statements

    I would like to Thank Any One That Can Help Me With This

    Sam
  • willthiswork89willthiswork89 Posts: 359
    edited 2006-08-19 05:11
    sam,

    im not sure i understand, LED's Emit thier own light not see it, are you meaning use an LED as an atrificial sunlight or as a real sunlight? are you wanting to know how many hours its up or hours minutes and seconds? for now i will assume the LED is replaces with a photoresistor...
    you do understand that relays are very hard to to run through the microcontroller dont you? and have you considered that the sunlight will move during the day thus possibly making your microcontroller not know that light is in the area?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Learn somthing about everything, and Everthing about somthing-
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2006-08-19 13:13
    willthiswork89

    Thanks·for your reply

    ·LED's Emit thier own light not see it, are you meaning use an LED as an atrificial sunlight or as a real sunlight?

    While it is true that Led's Emit light they can also be made to see light with this routine

    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    '
    Initialize:

    dly1 = 1
    dly2 = 50········ 'Changing This Value Will Change The Light Level Respone
    dly3 = 100


    '
    [noparse][[/noparse] Program Code ]


    Look:··············· '· File Name··· Dual LED Switch
    DO·················· '· Author....·· Sid Weaver
    HIGH led············ '· Some changes where made for this this code to work
    LOW chg············· '· In My Project I would like to Thank Sid For Posting
    PAUSE dly1·········· '· His Code This made this part easer for me to make this
    'charge············· '· work
    LOW led············· '· This is One LED Switch
    HIGH chg
    PAUSE dly1
    'input
    LOW led
    INPUT chg
    PAUSE dly2··········· 'Changing This Value Will Change The Light Level Respone
    DEBUG CR, DEC ? chg

    IF chg = 0 THEN
    GOTO Clock_Run
    ENDIF

    PAUSE dly3
    LOOP

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    ·Are you wanting to know how many hours its up or hours minutes and seconds?

    Yes i want to know how many hours of Sun Light there is in a day and Based on that , That will be how long my out side light
    will stay on Now i know that i could use the DS1302 as a Time Clock but i do not want to have to ADJ for Day Light Saving
    I do not want have to that way

    For now i will assume the LED is replaces with a photoresistor...

    The··photoresistor to me are some what flake and did not want use that

    Sam



  • willthiswork89willthiswork89 Posts: 359
    edited 2006-08-19 13:37
    alright, thanks for the fast reply!

    so what exactly are you having trouble with? and again, unless you consider using like 4 LEDs to see the sunlight you will need a servo turning at the exact rate of the sun which would take some time and math but is definantly possible. As for the function you will have to have somebody else help you out, as i am new to the coding standards my way of doing things is probably harder then the way bruce and other co-forumgoers would do it. hopefully my information helped a little but ill do a little work on it and see what i can do to help out.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Learn somthing about everything, and Everthing about somthing-
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2006-08-19 18:04
    willthiswork89

    Thanks for your reply

    The problem i am having is that i am not sure the right way to write this routine

    ·IF chg = 1 AND looks = 42 THEN······· ·====··········· IF hrs = 03·AND mins = 00 AND secs = 00 THEN
    · GOSUB relay_On·····························====··········· GOSUB· relay_OFF
    · ENDIF·········································· ====··········· ENDIF


    · IF chg = 1 AND looks = 40 THEN······· ====··········· IF hrs = 03·AND mins =·30 AND secs = 00 THEN
    · GOSUB relay_On·····························====··········· GOSUB· relay_OFF
    · ENDIF·········································· ====·········· ·ENDIF


    · IF chg = 1 AND looks = 38 THEN······· ====··········· IF hrs = 04·AND mins = 00 AND secs = 00 THEN
    · GOSUB relay_On···························· ====············GOSUB· relay_OFF
    · ENDIF·········································· ====··········· ENDIF



    I would to write this with out having to write so many IF statements is there a·way to do this·

    If· the only way to do what i want to is to use the IF statements then so be it
    But if there is another way to the same thing I would like to LEARN HOW TO DO THAT WAY ALSO
    ·

    These would all be IF statement doing this way save space


    look = 36························ =========··· 4 hrs··30 mins· 00· secs
    look = 34························ =========····5 hrs··00 mins· 00· secs
    look = 32························ =========····6 hrs··30·mins··00· secs
    look·= 30························ =========··· 7 hrs· 00 mins· 00· secs

    I may all of these i am not sure at this point

    look = 28
    look = 26
    look = 24
    look = 22
    look = 20


    Unless you consider using like 4 LEDs to see the sunlight you will need a servo turning at the exact rate of the sun which would take some time and math but is definantly possible.

    One led work fine i can ADJ the light in the CODE to take care of that part of it· But thanks sharing that thought with me and if i find that i need to use that
    i will let you know


    As for the function you will have to have somebody else help you out, as i am new to the coding standards my way of doing things is probably harder then the way bruce and other co-forumgoers would do it.

    I would like it if one of the·other co-forumgoers or Bruce·would help out or point to what Command to use and how to use it

    Hopefully my information helped a little but ill do a little work on it and see what i can do to help out.

    Thanks for any help that you can give in this matter

    Sam
  • willthiswork89willthiswork89 Posts: 359
    edited 2006-08-19 18:38
    Sam if tatements Arnt bad if you organize them correctly...

    do
    pause 60000
    minute = minute + 1
    gosub Check_Light
    loop until(sunlight = No)

    then

    Check_Light:

    if(led = seeslight) === Does the Led See Light? if so Change Light Status
    sunlight = Yes
    endif

    if(sunlight = no) === If Sunlight is Not Present do a small check...
    sleep 900000 ' Sleep 15 Minutes
    if (counter <> 0) ' If The Counter is Not Zero Then we can assume it is now Dark Time not Morning and we can use the data during the day to log.
    hours = Minutes/60 ' Turn tHe minutes into Hours

    now of course its not written right its More Psuedo Code but just tell me what else yo need or maybe you can go off that... try writing the data into the eeprom possibly?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Learn somthing about everything, and Everthing about somthing-
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2006-08-19 19:39
    Sam,

    Using the PAUSE and SLEEP commands are not going to give you the accuracy you are looking for.


    In your title,·you indicate the use of·a DS1302 ( Dallas Semiconductor Real Time Clock ) and since this
    seems to be the heart of your application, I would focus on this first.· Here is an example using the
    DS1302 with the BS2...

    http://www.parallax.com/dl/docs/books/sw/exp/sw30.pdf

    As far as the LED as a sensor is concerned,·I have tested this with the Propeller, but not·with a BS2.
    The basic theory is that by applying power in reverse to a LED, the PN junction acts as a capacitor.
    When Photons from another light source fall on the PN junction, the "PN-capacitor" discharges.· The
    more photons that strike the junction the quicker the discharge.· Remember, this is a very small
    "PN-capacitor" and the discharge rate might be very quick simply because there is not much there
    to discharge.· The discharge is so quick, that the BS2 will not be able to resolve the·time it takes for
    the discharge to occur.· You can lengthen this amount of time by placing a small (100pF or so)
    capacitor in parallel with the "PN-capacitor"


    I would focus on both of these aspects one at a time before I tried to combine them just yet.· Let's
    get the DS1302 working, and then run some DEBUG tests with the LED sensor.





    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2006-08-19 20:06
    willthiswork89
    Thanks for your reply

    I am using a DS1302 to keep track of the time

    I am looking for a way to do this with out having to use alot of IF
    Statements

    All i am lookiing for to use some other command that would do the same thing
    if i have

    15 hours of sun light then my lights would stay ON for 3 hours
    14 hours·························································for 3 1/2 hours
    13 hours························································ for 4 hours
    12 hours of sun light then my lights would stay ON for 5 hours
    11 hours·························································for·6 hours
    10·hours of sun light·then my lights would stay ON for 7 hours

    This would mean that i would have to have· 12 IF
    Statements at the least
    ·That was not the way i want to do this and i am tring to·learn··different way of writing
    code

    I would like it if one of the·other co-forum goers or Bruce·would help out or point to what Command to use and how to use it

    Sam
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-08-19 20:26
    Sam_sam_sam,

    You could condense your statements like this

    IF chg = 1 AND (looks = 42 OR looks = 40  OR looks = 38) THEN   GOSUB relay_On
    




    When using an RTC like the DS1302, you can have the program compute a variable that goes from 0 to 1439, the sequential minute of the day from midnight to 23:59:
    julian = hour * 60 + minute  ' if hour and minute are unitary values
    ' or directly from the RTC chip, if BCD, which is the usually the case
       julian = hour.nib1 * 10 + hour.nib0 * 6 + minute.nib1 * 10 + minute.nib0   ' no parentheses!
    



    With that, computations become a lot easier. For example, if you determine that the sun has arisen at 6 am (julian=360) and has set at 6pm (julian=1080) then it is easy to calculate the total number of minutes of sunlight (1080-360 = 720) and from that compute the seqential minute at which the output should change state. Say, 720 minutes of sunlight translates into 3 hours (180 minutes) of output after the sunset, or at julian = 1080+180 = 1260.

    The LED will work well as a sunlight sensor.




    Then statements like your multiple IF can more easily be done with a SELECT CASE or LOOKUP command.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2006-08-19 22:26
    Tracy Allen
    Thanks for your reply and pointing me in this direction

    ·Sam

    Post Edited (sam_sam_sam) : 8/24/2006 2:18:41 AM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2006-08-19 22:40
    Tracy Allen

    Would i be better off to use minutes to what i want to
    Then use hour and minutes and seconds

    With that, computations become a lot easier. For example, if you determine that the sun has arisen at 6 am (julian=360) and has set at 6pm (julian=1080) then it is easy to calculate the total number of minutes of sunlight (1080-360 = 720) and from that compute the seqential minute at which the output should change state. Say, 720 minutes of sunlight translates into 3 hours (180 minutes) of output after the sunset, or at julian = 1080+180 = 1260.
    THANKS for all your help and time

    Sam
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2006-08-19 23:03
    Beau Schwabe

    Thanks for your reply
    I·have used the DS1302 in two other project i·understand what commands to use to control an OutPut

    I have the LED as a SWITCH routine working Just Fine

    And have put the two routine together and they are working an other project that i am working on
    but in that project i only have FOUR IF statements and that not so bad

    What i want to know to is to use something else other than the IF statement because it take atl least 12 of them
    at least and may be a few more i might have to add

    The Problem··i have·had tring to use 5 or more it has give me trouble before

    Sam

    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2006-08-19 23:37
    You can also use a lookup table with the READ and DATA statements. You could have a table with one entry for each number
    of hours there's sun. The table entry would have the number of hours the lights need to be on. Since you seem to want this
    in units of 1/2 hour, I'd make the table entries in terms of that. Here I assume there's never more than 19 hours of sunlight.
    Table  DATA  16, 16, 16, 16, 16, 16, 16, 16, 16, 16     ' Sun hours from 0 to 9 -> Half hours of on time
              DATA  14, 12, 10,  8,   7,    6,   5,   4,   3,   2    '  Sun hours from 10 to 19 -> Half hours of on time
    
    GetOnTime:
              READ  Table+SunHours, HalfHoursOn
    
    


    This code fragment sets up a table in EEPROM with bytes containing the number of 1/2 hours the lights should remain on.
    The offset in the table is the number of hours of sunlight seen [noparse][[/noparse]SunHours] (from 0 to 19). The result is put in "HalfHoursOn".

    The two variables "SunHours" and "HalfHoursOn" can be bytes or nibbles if the values are constrained to be 0-15.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2006-08-20 06:27
    Mike Green············································· smile.gif

    Thank You for showing this to me
    This is some thing very new to me so bear with· if ask somethings to help me·understand how to· use this
    I have never used these commands before

    I seen·the data and lookup in drffrenet code and different project· before
    But· never· had to set one up

    Please help understand how to set·this up········ idea.gif

    Could you show me how··to·set the frist one·up··>···· 10 hours of sun light would = 6 hours ON
    This will help me uderstand how to do it

    Thank you for any help that you·can give and for all of your time·.......Ahead of time

    (1) Why in the frist Data line is all the NO 16· and dose .....14 to 2·....is .....10 to 19...... in the same order
    (2) How do you set up HalfHourOn
    (3) how do i set up sun hours·I know how to start the clock


    Table··DATA··16,·16,·16,·16,·16,·16,·16,·16,·16,·16·····'·Sun·hours·from·0·to·9·->·Half·hours·of·on·time
    ··········DATA··14,·12,·10,··8,···7,····6,···5,···4,···3,···2····'··Sun·hours·from·10·to·19·->·Half·hours·of·on·time

    GetOnTime:
    ··········READ··Table+SunHours,·HalfHoursOn



    Sam
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2006-08-20 20:33
    Hi, EveryOne·· smile.gif

    ·Can someone PLEASE EXPLANE THIS TO· HOW TO USE THIS
    I am trying VERY HARD NOT TO SHOUT

    ·Can some one PLEASE help me· with this problem that i have

    I·tried to do this routine with IF statements but there will be to many
    ·I tried using the SELECT CASE statements and can not get this to work

    ·I am not UPSET with ANYONE i would just like some help
    I WANT TO LEARN THIS

    I have 3 or 4 projects that i have in mind


    ·How do you··write the READ·statements·hours·from·0·to·9·->·

    How do· you write·READ and DATA statements·the·Half·hours·of·on·time·
    ·
    How to write the TURN ON OUTPUT STATEMENT
    ·
    ·Table·
    ······· ·DATA··16,·16,·16,·16,·16,·16,·16,·16,·16,·16·
    ·············

    ··········DATA··14,·12,·10,··8,···7,····6,···5,···4,···3,···2····'··Sun·hours·from·10·to·19·->·Half·hours·of·on·time

    GetOnTime:
    ··········READ··Table+SunHours,HalfHoursOn


    I· understand that·DATA is used for the LOOKUP and READ commands

    but i do not understand how to use them

    If someome·could give and·smiple example of how to use DATA and READ for one Time Frame

    14 hours of sun light·· 3· ·hour of ON TIME
    13 hours of sun light·· 3.5 hours of on TIME

    and how to set it

    THANKS TO ANYONE THAT CAN HELP ME WITH THIS
    Sam···· idea.gif






    Post Edited (sam_sam_sam) : 8/24/2006 3:03:24 AM GMT
Sign In or Register to comment.