Shop Learn
Setting up LED's to only run at night for 3 hours and then repeat the next day — Parallax Forums

Setting up LED's to only run at night for 3 hours and then repeat the next day

I made a program to run 3 LED's when it got dark using a photo resistor and the RCTIME function which worked fine. I now want it to only run for 3 hours after dark and then reset itself to run the next day. I have tried using a counter variable together with a DO UNTIL the counter equals a set value but am stuck how to make it reset for the next day. Is there a way to bring in some type of clock function or other way to limit the run time.

Comments

  • AwesomeCronkAwesomeCronk Posts: 1,025
    edited 2021-02-05 04:12

    If you label your main code with a line like this:

    <stamp declarations>
    
    Main:
    <your code>
    GOTO Main
    

    Then when your code hits the goto line it will jump back up to the beginning of your code. I am not 100% on my syntax because I have not written PBASIC in forever, but this should get you started in the right direction.

  • tomcrawfordtomcrawford Posts: 1,095
    edited 2021-02-05 18:01

    If I understand your post, you are able to detect when it gets dark, turn on the LEDs for three hours and then turn them off. And what you want to do is leave them off until the following dusk.

    You could use a loop containing a PAUSE that gets you past dawn and then start over again searching for dusk.

  • Tracy AllenTracy Allen Posts: 6,553
    edited 2021-02-06 02:11

    The SLEEP command can be your friend for the long stretch to the next day. The SLEEP command on the Stamp is fairly accurate, because it is calibrated against the resonator.

    I don't know what your 3 display LEDs need to do during the 3 hours after dark, but let's suppose that they need to be updated once a second. A loop that runs once per second 10800 times will eat up three hours. The PAUSE command can help with that.

    main:
      dark=false.  ' define this variable
      DO
        SLEEP 600. ' sleep for 10 minutes
        GOSUB darkCheck  ' your RCTIME code, could make this check several times to be sure
      UNTIL dark=true.     ' flag is updated by the subroutine
      ' time to dance...
      FOR reps = 1 to 10800.  ' fine tune to adjust total time ~3hours, reps is a word variable
        GOSUB updateLEDS.  ' once a second, your code
        PAUSE 1000.  ' adjust this to fine tune the interval ~1 second
      NEXT
      ' time for bed...
      SLEEP 64800    ' sleep in low current mode for 18 hours
    
      GOTO main.  ' do it again.
    
    
  • Thanks to all, the Pause and Sleep commands worked perfectly. - Bob

Sign In or Register to comment.