​ ​ ​ ​ Smart picaxe outdoor light - Page 8
Page 8 of 12 FirstFirst ... 6 7 8 9 10 ... LastLast
Results 71 to 80 of 117

Thread: Smart picaxe outdoor light

  1. #71
    Senior Member
    Join Date
    Feb 2012
    Location
    London
    Posts
    2,649

    Default

    Hi,

    Quote Originally Posted by newplumber View Post
    "poke HOURS,0"
    that is the same as
    poke HOURS,B0 ?
    No they're not the same, but it's more the "numbers" themselves than the memory type. In computing there are (at least) two different types of numbers called "variables" and "constants".

    b0 is a "Variable"; consider it like an empty box in which you can put up to 255 "counters", be they coins, matches, marbles or sugar cubes*. So to remember today's date, you might put 10 cubes into a box marked "b0". Tomorrow, you'd add one cube (or "INCrement" the contents) so that you then know tomorrow's date is 11th. To help you remember what the contents of the box actually represent, you would probably write "DATE" on the outside of the box (consider that like the symbol command: SYMBOL DATE = b0).

    To make a calendar, you might also mark a box b1 with the word MONTH and at the end of each month you would empty the DATE box (or strictly leave one cube in place) and add one cube to the MONTH box. In practice, the "rules" for the end of the month are quite complex ("Thirty days hath, September, April, June and November.....etc.") which is why there are some long conditional instructions in marks' program code.

    A "constant" can be considered as being similar to our "Variable" boxes above, but the lid is glued in place so you cannot change the contents. Let's assume the box is transparent so you can still see the number of cubes inside. In a computing scenario, the box becomes "locked" when you write or download the program. In some computer languages there is a special character/symbol to identify a "constant", but in PICaxe basic, a number that does not have a "b" or "w" prefix may be considered as a "constant" number.

    So back to the quoted example above: HOURS is a "constant" which was previously defined by the: SYMBOL HOURS = 32 and the "0" in POKE HOURS,0 is also a constant. So the command says: "store the value zero into the box (memory) numbered 32" (i.e. empty the HOURS box).

    However, POKE HOURS, b0 says "read the (variable) number which is stored in b0 and save that same number in the box numbered 32" (i.e. copy the number stored in b0 into the RAM memory location 32).

    You might be concerned that SYMBOL HOURS = might specify for example either "b0" or a "0", which could cause confusion. To overcome this, some people add a character to the variable or the constant name to indicate the "type", but personally I normally use CAPITALS to indicate CONSTANTS and (mainly) Lower Case names to indicate Variables.

    * One could invent various scenarios as to why each "box" would be physically limited to exactly 255 "markers", and how the contents might be counted quickly (e.g. by counting the complete rows, columns and layers occupied by identical cubes). But you'd soon realise that exactly the same can be done with a pencil, some "Post It notes" and a few "rules" (e.g. you're not allowed to write a number > 255 onto any sheet), etc..

    Cheers, Alan.
    Last edited by AllyCat; 10-09-2017 at 17:42.

  2. #72

    Default

    Hi newplumber,
    yes when ever you get a high from the motionsensor ,variable b0 will stay high for the time period
    if you retrigger within that time 12(6seconds) the count starts again so will always stay high.
    when the switch is on it will always remain high also.switch off cancels the count then goes low.
    ssrcontrol works with flag DL=1 which meant daylight time between sunrise to sunset
    this held the ssrpower output low during the day DL=0 meant zero daylight.
    i was not too sure what you wanted but changed a few variable names to try and make things a little clearer.
    I'm still blurry eyed from the weekend Our team the Eagles had another miracleous win its been non stop drinking since
    code requires testing hopefully you'll do that...
    Code:
    #picaxe 20m2         'marks 
    #terminal 38400
    SETFREQ M32          ' time increments 0.5 seconds
    dirsB = %11111001             
    dirsC = %10111000
    SYMBOL SensorTime = 12 ' so will be 6 seconds
    SYMBOL LIGHTswitch  = pinB.2
    SYMBOL MOTIONsensor = pinB.1
    SYMBOL SSRpower     = outpinB.0
    SYMBOL MSR       = b0     
     SYMBOL secs     = b1
     SYMBOL mins     = B2
     SYMBOL hours    = B3      
     SYMBOL date     = B4        : SYMBOL day  = B4  'day not used from RTC
     SYMBOL month    = B5   
     SYMBOL year     = B6 
     SYMBOL CommonYear  = b7     : SYMBOL Daylight = b7  : SYMBOL MFL = b7
     SYMBOL D0          = b8    
     SYMBOL D1          = b9
     SYMBOL D2          = b10
     SYMBOL D3          = b11
     SYMBOL DayNumber   = W6 
     SYMBOL DSTstart    = w7
     SYMBOL DSTend      = w8     : SYMBOL DayS = w8   : SYMBOL SunrisePlus10 = w8
     SYMBOL SunRiseTake60  = w9  : SYMBOL Sunrise = w9  
     SYMBOL SunSet      = W10
     SYMBOL DayTime     = w11
     SYMBOL CRC = b24 
     SYMBOL AON = b25
    	TABLE  0,(204,203,202,199,194,189,182,174,166,156)  '   Sunrise +240 minutes
    	TABLE 10,(147,136,126,115,105, 94, 83, 73, 63, 54)  '   North Dakota
    	TABLE 20,( 45, 37, 29, 23, 18, 14, 11, 10, 10, 11)  '  -7 StandardTime
    	TABLE 30,( 13, 17, 21, 26, 32, 38, 44, 51, 58, 64)
    	TABLE 40,( 71, 77, 84, 91, 97,104,111,118,125,133)  '  2018 (betweenleapyear) 
    	TABLE 50,(140,148,156,163,171,178,185,191,196,200)
    	TABLE 60,(202,204)
    	TABLE 100,( 25, 30, 37, 44, 52, 60, 68, 76, 84, 92) '  Sunset +960 minutes
    	TABLE 110,(100,107,115,122,129,136,143,150,157,164)
    	TABLE 120,(171,178,185,191,197,203,207,211,214,215)
    	TABLE 130,(215,214,212,208,203,197,190,182,173,163)
    	TABLE 140,(153,143,132,121,110, 99, 89, 78, 68, 58)
    	TABLE 150,( 49, 41, 34, 27, 22, 18, 16, 15, 15, 17)
    	TABLE 160,( 21, 25)
     InitialiseTime:
        HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte  ' Set to 100kbps 
          HI2Cout $0 , ( $30, $22, $04, 0 , $07 , $09 , $17)  ' program with UTC/GMT -7 hours StandardTime	
     Main: 
       Switches:  IF MOTIONsensor = 1 THEN : TIME = 0 : ENDIF
                  IF LIGHTswitch  = 1 THEN : TIME = SensorTime -1 : ENDIF 
                  IF LIGHTswitch  = 0 THEN : MSR = 0 : ENDIF
       SSRconditionals1: IF TIME  > SensorTime THEN : TIME = SensorTime : ENDIF	  
                         IF TIME  < SensorTime THEN : MSR = 1 : ENDIF
          HI2Cin  $0 , (secs,mins,hours,day,date,month,year) ' Read DS1307	or DS3231
            FOR bptr = 2 TO 6   
              @bptr = @bptr/$10*250+@bptr                    ' Convert BCD to Decimal  
            NEXT
      MountainDaylightTime:                     ' United States       
         CommonYear = year //4 +3 /4            ' CommonYear =1              
          DayNumber = month +9 /12   
          DayNumber = CommonYear + DayNumber * DayNumber
          DayNumber = month *275 /9 +date -30 -DayNumber 
               DayS = year *512 **46752         ' year =1 to 99    
           DSTstart = Days +72//7                 
           DSTstart = 74 -CommonYear -DSTstart  ' Second SunDay in March                  
             DSTend = DSTstart +238             ' First SunDay in November    
    	 SunriseToSunset:
                Daylight = Daynumber /6 : READtable Daylight,D0 : INC Daylight :  READtable Daylight,D1
                DayLight = DayLight +99 : READtable Daylight,D2 : INC Daylight :  READtable Daylight,D3     
                DayLight = DayNumber //6 : IF D0 > D1 THEN : SWAP D0,D1 : DayLight = 6 -DayLight : ENDIF
                 SunRiseTake60 = D1 -D0 * Daylight /6 +D0 +180
                DayLight = DayNumber //6 : IF D2 > D3 THEN : SWAP D2,D3 : DayLight = 6 -DayLight : ENDIF
                  SunSet = D3 -D2 * Daylight /6 +D2 +960   	           	              
         IF DayNumber < DSTstart OR DayNumber > DSTend THEN Display
         IF DayNumber = DSTstart AND hours <2 THEN Display   ' DST starts at 0200 
         IF DayNumber = DSTend   AND hours >1 THEN Display   ' DST ends at 0200
    	 IF hours =23 THEN : INC date  
    	   IF month =4 or month =6 or month =9  and date =31 OR date=32 THEN : date =1 INC month: ENDIF
    	 ENDIF : hours =hours +1//24 : Sunrise =Sunrise +60 : Sunset =Sunset +60  
    	   d0="D"   :  GOTO SSRconditionals2:              ' DST +1	  
      Display: d0= "S"                                       ' StandardTime  
       SSRconditionals2:DayTime = hours *60 +mins	         '' DayTime (total minutes of day)
    MFL=0 :IF DayTime < SunriseTake60 THEN  : MFL =1 :  ENDIF
    AON=0 :IF DayTime > Sunset THEN : AON =1 : ENDIF : SunRisePlus10 = SunriseTake60 +70
           IF DayTime > SunriseTake60 AND Daytime < SunRisePlus10 THEN : AON=1 : ENDIF
       IF secs <> CRC THEN                                   ' wait for change of secs
       Sertxd ("M",d0,"T ")
       sertxd ("20",#year,"/",#month,"/",#date)              ' Date 20yy/mm/dd 
    	d3=mins/10
    	d2=mins//10
    	d1=secs/$10
    	d0=secs//$10
          sertxd ("  ",#hours,".",#d3,#d2,".",#d1,#d0)       ' 24hr time hh/mm/ss
             sunrise = SunriseTake60 +60 
    	   d2=sunrise/60
    	   d1=sunrise//60 /10
    	   d0=sunrise//60//10
    	   Sertxd ("  Sunrise   ",#d2,":",#d1,#d0)
    	      d2=sunset/60
    	      d1=sunset//60 /10
    	      d0=sunset//60//10
    	      Sertxd ("  Sunset ",#d2,":",#d1,#d0, cr,lf)
    	ENDIF : CRC = secs	
    SSRcontrol:  IF MSR = 0 AND AON = 0 THEN : SSRpower = 0 : ENDIF    ' MotionSenseRelay    AlwaysON             
                 IF MSR = 1 AND MFL = 1 OR AON = 1 THEN : SSRpower = 1 : ENDIF  'MidnighttoFirstLight	   
       goto main
    Last edited by marks; 11-09-2017 at 04:17. Reason: corrected daytime even eagles needed timeon ang goal after siren...

  3. #73
    Senior Member
    Join Date
    Apr 2013
    Location
    north dakota
    Posts
    279
    Blog Entries
    2

    Default

    @allycat okay now it makes sense thanks for explaining ...just when I thought I might have a chance at being smart
    my knowledge ladder collapses but I am learning one bit per hour at a time

    @marks thanks I will test your code later on, its a good thing I never started drinking other wise I would still be celebrating my
    vintage first led test (my blood has horrible alcoholism)

  4. #74
    Senior Member
    Join Date
    Apr 2013
    Location
    north dakota
    Posts
    279
    Blog Entries
    2

    Default

    @ Marks I have been testing your code but it seems not to make SSpower to zero "off" (its staying on forever)
    after i reset the picaxe it stays off till sunset time then after sunset time = RTC time then SSpower =1 (which is correct) but after that it stays on even after sunrise till i reset it again.
    I have been trying to determine why, of course it's probably something pretty simple but keeps jumping over my head
    I will keep on studying it

  5. #75

    Default

    Hi newplumber,
    I dont have an rtc or hardware available to me at this time '''
    but you did give me a clue .
    i did spot an error if switch was left on it would remain on or until sensor timed out just after sunrise.
    how it's meant to work example

    sensor or switch 0000 - 0527
    always on 0528 - 0637 (if sunrise 0628 and sunset 1904)
    off 0638 - 1903 (was unsure what time after sunrise so made it 10 mins after)
    always on 1904 - 2359
    i did spot an error if switch was left on it would remain on or until sensor timed out after sunrise.

    3rd time lucky maybe lol - the lines i changed this time are marked '''
    Code:
     #picaxe 20m2         'marks 
    #terminal 38400
    SETFREQ M32          ' time increments 0.5 seconds
    dirsB = %11111001             
    dirsC = %10111000
    SYMBOL SensorTime = 12 ' so will be 6 seconds
    SYMBOL LIGHTswitch  = pinB.2
    SYMBOL MOTIONsensor = pinB.1
    SYMBOL SSRpower     = outpinB.0
    SYMBOL MSR       = b0     
     SYMBOL secs     = b1
     SYMBOL mins     = B2
     SYMBOL hours    = B3      
     SYMBOL date     = B4        : SYMBOL day  = B4  'day not used from RTC
     SYMBOL month    = B5   
     SYMBOL year     = B6 
     SYMBOL CommonYear  = b7     : SYMBOL Daylight = b7  : SYMBOL MFL = b7
     SYMBOL D0          = b8    
     SYMBOL D1          = b9
     SYMBOL D2          = b10
     SYMBOL D3          = b11
     SYMBOL DayNumber   = W6 
     SYMBOL DSTstart    = w7
     SYMBOL DSTend      = w8     : SYMBOL DayS = w8   : SYMBOL SunrisePlus10 = w8
     SYMBOL SunRiseTake60  = w9  : SYMBOL Sunrise = w9  
     SYMBOL SunSet      = W10
     SYMBOL DayTime     = w11
     SYMBOL CRC = b24 
     SYMBOL AON = b25
    	TABLE  0,(204,203,202,199,194,189,182,174,166,156)  '   Sunrise +240 minutes
    	TABLE 10,(147,136,126,115,105, 94, 83, 73, 63, 54)  '   North Dakota
    	TABLE 20,( 45, 37, 29, 23, 18, 14, 11, 10, 10, 11)  '  -7 StandardTime
    	TABLE 30,( 13, 17, 21, 26, 32, 38, 44, 51, 58, 64)
    	TABLE 40,( 71, 77, 84, 91, 97,104,111,118,125,133)  '  2018 (betweenleapyear) 
    	TABLE 50,(140,148,156,163,171,178,185,191,196,200)
    	TABLE 60,(202,204)
    	TABLE 100,( 25, 30, 37, 44, 52, 60, 68, 76, 84, 92) '  Sunset +960 minutes
    	TABLE 110,(100,107,115,122,129,136,143,150,157,164)
    	TABLE 120,(171,178,185,191,197,203,207,211,214,215)
    	TABLE 130,(215,214,212,208,203,197,190,182,173,163)
    	TABLE 140,(153,143,132,121,110, 99, 89, 78, 68, 58)
    	TABLE 150,( 49, 41, 34, 27, 22, 18, 16, 15, 15, 17)
    	TABLE 160,( 21, 25)
     InitialiseTime:
        HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte  ' Set to 100kbps 
          HI2Cout $0 , ( $30, $20, $04, 0 , $13 , $09 , $17)  ' program with UTC/GMT -7 hours StandardTime
    	TIME = SensorTime ''' remove sensortime out when first powering on during MFL
     Main: 
       Switches:  IF MOTIONsensor = 1 THEN : TIME = 0 : ENDIF
                  IF LIGHTswitch  = 1 THEN : TIME = SensorTime -1 : ENDIF 
                  IF LIGHTswitch  = 0 THEN : MSR = 0 : ENDIF
       SSRconditionals1: IF TIME  > SensorTime THEN : TIME = SensorTime : ENDIF	  
                         IF TIME  < SensorTime THEN : MSR = 1 : ENDIF
          HI2Cin  $0 , (secs,mins,hours,day,date,month,year) ' Read DS1307	or DS3231
            FOR bptr = 2 TO 6   
              @bptr = @bptr/$10*250+@bptr                    ' Convert BCD to Decimal  
            NEXT
      MountainDaylightTime:                     ' United States       
         CommonYear = year //4 +3 /4            ' CommonYear =1              
          DayNumber = month +9 /12   
          DayNumber = CommonYear + DayNumber * DayNumber
          DayNumber = month *275 /9 +date -30 -DayNumber 
               DayS = year *512 **46752         ' year =1 to 99    
           DSTstart = Days +72//7                 
           DSTstart = 74 -CommonYear -DSTstart  ' Second SunDay in March                  
             DSTend = DSTstart +238             ' First SunDay in November    
    	 SunriseToSunset:
                Daylight = Daynumber /6 : READtable Daylight,D0 : INC Daylight :  READtable Daylight,D1
                DayLight = DayLight +99 : READtable Daylight,D2 : INC Daylight :  READtable Daylight,D3     
                DayLight = DayNumber //6 : IF D0 > D1 THEN : SWAP D0,D1 : DayLight = 6 -DayLight : ENDIF
                 SunRiseTake60 = D1 -D0 * Daylight /6 +D0 +180
                DayLight = DayNumber //6 : IF D2 > D3 THEN : SWAP D2,D3 : DayLight = 6 -DayLight : ENDIF
                  SunSet = D3 -D2 * Daylight /6 +D2 +960   	           	              
         IF DayNumber < DSTstart OR DayNumber > DSTend THEN Display
         IF DayNumber = DSTstart AND hours <2 THEN Display   ' DST starts at 0200 
         IF DayNumber = DSTend   AND hours >1 THEN Display   ' DST ends at 0200
    	 IF hours =23 THEN : INC date  
    	   IF month =4 or month =6 or month =9  and date =31 OR date=32 THEN : date =1 INC month: ENDIF
    	 ENDIF : hours =hours +1//24 : Sunrise =Sunrise +60 : Sunset =Sunset +60  
    	   d0="D"   :  GOTO SSRconditionals2:              ' DST +1	  
      Display: d0= "S"                                       ' StandardTime  
       SSRconditionals2:DayTime = hours *60 +mins	         '' (moved) DayTime  total minutes of day 
    MFL=0 :IF DayTime < SunriseTake60 THEN  : MFL =1 :  ENDIF
    AON=0 :IF DayTime => Sunset THEN : AON =1 : ENDIF : SunRisePlus10 = SunriseTake60 +70 ''' (=)
           IF DayTime >= SunriseTake60 AND Daytime < SunRisePlus10 THEN : AON=1 : ENDIF ''' (=)
       IF secs <> CRC THEN                                   ' wait for change of secs
       Sertxd ("M",d0,"T ")
       sertxd ("20",#year,"/",#month,"/",#date)              ' Date 20yy/mm/dd 
    	d3=mins/10
    	d2=mins//10
    	d1=secs/$10
    	d0=secs//$10
          sertxd ("  ",#hours,".",#d3,#d2,".",#d1,#d0)       ' 24hr time hh/mm/ss
             sunrise = SunriseTake60 +60 
    	   d2=sunrise/60
    	   d1=sunrise//60 /10
    	   d0=sunrise//60//10
    	   Sertxd ("  Sunrise   ",#d2,":",#d1,#d0)
    	      d2=sunset/60
    	      d1=sunset//60 /10
    	      d0=sunset//60//10
    	      Sertxd ("  Sunset ",#d2,":",#d1,#d0, cr,lf)
    	ENDIF : CRC = secs	
    SSRcontrol:  IF MSR = 0 or MFL = 0 AND AON = 0 THEN : SSRpower = 0 : ENDIF  ''' ( or MFL=0) MotionSenseRelay    AlwaysON             
                 IF MSR = 1 AND MFL = 1 OR AON = 1 THEN : SSRpower = 1 : ENDIF  ' MidnighttoFirstLight	   
       goto main

  6. #76
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,726

    Default

    The code is becoming quite monolithic. It might be best to factor that out into individual routines which set or clear status bits or bytes. That way you can simplify control to something which may look like -

    Code:
    Do
      Gosub CheckPir
      Gosub CheckDaylightTimes
      If pirActivated = 1 And isDayTime = 0 Then
        Gosub LightOn
        Gosub StartTimeout
      End If
      If LightIsOn = 1 And timedOut = 1 Then
        Gosub LightOff
      End If 
    Loop
    That allows the routines to do what they need to without worrying about the rest, and the decision making can be done without worrying about how the data it uses is determined.

    That will make things much easier to code, debug and alter to fit the specification.

    For example, you can check the PIR is coming on and light timeout simply by including "isDayTime = 0 : Return" at the start of the 'CheckDaylightTimes:" routine.

    You can also check the "CheckDaylightTimes;" routine without worrying about the PIR or lamp.

  7. #77
    Senior Member
    Join Date
    Dec 2010
    Location
    usa: west of the mississippi
    Posts
    122

    Default

    I can guarantee that a year later, when you haven't messed with it for awhile, it will be much easier to pickup what's going on.

    learned the hard way, tom

  8. #78
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    23,726

    Default

    Quote Originally Posted by geezer88 View Post
    learned the hard way, tom
    I think we all did, and those who have not simply have that to come :-)

    There's nothing like a year-old bit of code which one looks at, can't understand, realise it's easier to rewrite from scratch than figure out, to make one realise there may be better ways to do things.

    That's not criticism because we will all walk that path at some point; it's part of the learning experience. Things particularly to watch out for is what names of variables mean, will "MFL", "MSR" and "AON" be meaningful in a year ?

    It is somewhat acceptable if a routine isn't explained or easily understood providing what it takes in and and what it gives out is well documented and it works.

    When one comes back to code it's like others watching now who aren't familiar with it. The simpler it is made for them to follow now the easier it will be oneself in the future. One always starts with the outer big picture, the main loop; "On-Pause-Off-Pause", easy to understand even if one doesn't know what On, Off or Pause is or does, exactly how it's coded or works. You already know the program if not the detail.

    The great advantage of modularisation and subroutines is that observers can look at what there is and say that looks right or wrong without considering the rest, can take it on trust that 'isDayTime' does what it's meant to, a different problem - and someone else's - if it does not.

    If something is in a short loop and it's not working the problem is within that loop, or something is providing wrong data for the loop. Easier to track down and solve. If the code becomes monolithic an issue could lay anywhere, and it will be harder to know where and what the fix should be.

  9. #79
    Senior Member
    Join Date
    Feb 2010
    Location
    Don't Mess With My Texas!
    Posts
    2,195

    Default

    Tacking on to what Hippy and geezer88 have to say, it doesn't cost anything to make your variable names verbose. Verbose means highly descriptive in this case.

    MSD would be MostSignificantDigit, or Psw might be PowerSwitch. Certainly you can overdo it and end up typing a lot, but that's nothing compared to having to rewrite parts of your code a month or two from now.
    - Tex
    __________________________________________________ _______________________
    "Truth lies dormant in our future history." ― Tex Clodhopper LXV
    “Confidence is ignorance. If you're feeling cocky, it's because there's something you don't know.” ― Eoin Colfer, Artemis Fowl

  10. #80
    Senior Member
    Join Date
    Apr 2013
    Location
    north dakota
    Posts
    279
    Blog Entries
    2

    Default

    @ okay Marks I have it on test again ... 3rd times the charm for you ... 30th times the charm for me
    but thanks again now I should be able to understand 2 percent of it.
    How you keep this code all together in a tight form is beyond me

    @hippy I think i understand what you said ..
    like put chapters in it like a book
    thats what i try to do with my code is subroutines
    of course I'll need a few extra years to make it squeezeable like yours

    @geezer88 lol I don't know about a year ... maybe 5
    "learned the hardway"
    I do that alot outside real world..
    but for picaxe learning the hard way? ..I guess I don't know enough for a simpler way
    I'm just happy when I click the "CHECK" button it says "compiled successfully"
    memory used = 2047 out 2048 bytes
    (joking but prolly use a few extra 100 bytes then needed)

    @Texasclodhopper
    Certainly you can overdo it and end up typing a lot
    When I first started learning to code a picaxe I explained every line with 'and a note
    Now after watching the pros code I understand how it can be totally over done with
    lot more information then needed
    but nothing helps me more then "_" key ... like I.E.
    SYMBOL PAUSE_TIME = B1
    instead of
    SYMBOL PT = B1 ' pt = pause time

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •