October 31, 2024, 07:25:29 PM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


How i make to different DATA Tables One for ON and One for OFF

Started by samsam, December 26, 2007, 08:50:03 AM

Previous topic - Next topic

JonnyMac

Okay, Sam, here's a program for you to work with.  Now... let me make it very clear that this isn't ready to run, and that you're going to have to do some tweaking to get it working correctly.  The first step is to build the light detector and them measure daylight levels at several point during the day -- especially near sunrise and sunset -- so that you can set the daylight threshold.

This operates as a state machine.  The main loop has a ~one minute delay which is followed by a sensor measurement.  Then BRANCH is used to jump to one of three sections based on the current state.  The way I see it the program states are: 0) measuring the number of daylight minutes, 1) running the relay, and 2) waiting for daylight to return.

This is a moderately sophisticated program and I urge you to simply study it for a few days before you attempt to use or modify it.  My book, StampWorks (you can get the PDF from Parallax), will be helpful in understanding PBASIC 2.5 -- if you haven't read it you really should.

' =========================================================================
'
'   File...... Daylight_Monitor.BS2
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

Sensor          PIN     15                      ' CdS circuit
Relay           PIN     14                      ' control output


' -----[ Constants ]-------------------------------------------------------

NIGHT           CON     300                     ' change for your circuit!

IsOn            CON     1
IsOff           CON     0

DARK_THRESH     CON     5
LIGHT_THRESH    CON     5

M_DAYLIGHT      CON     0                       ' day mode
M_RELAY_ON      CON     1                       ' running relay
M_RELAY_OFF     CON     2                       ' relay off, waiting for day


' -----[ Variables ]-------------------------------------------------------

state           VAR     Byte                    ' program state

lightLevel      VAR     Word                    ' measured light level
dayMins         VAR     Word
nightMins       VAR     Word
relayTmr        VAR     Word

idx             VAR     Byte


' -----[ Initialization ]--------------------------------------------------

Reset:
  LOW Relay                                     ' make output and off

  GOSUB Measure_Light
  IF lightLevel < NIGHT THEN
    state = M_DAYLIGHT                          ' measure daylight
  ELSE
    state = M_RELAY_OFF                         ' wait for new day
  ENDIF

Clear_Timers:
  dayMins = 0
  nightMins = 0
  relayTmr = 0


' -----[ Program Code ]----------------------------------------------------

Main:
  PAUSE 59995                                   ' fine-tune for 1 minute

Measure_Light:
  HIGH Sensor                                   ' charge RC
  PAUSE 1
  RCTIME Sensor, 1, lightLevel                  ' measure CdS

Run_Pgms:
  BRANCH state, [Measure_Day, Run_Relay, Wait_For_Day]

  state = M_DAYLIGHT                            ' trap state error
  GOTO Main

Measure_Day:
  IF lightLevel < NIGHT THEN
    dayMins = dayMins + 1
    nightMins = 0                               ' clear false dark
  ELSE
    nightMins = nightMins + 1
    IF nightMins = DARK_THRESH THEN
      idx = 2
      LOOKDOWN dayMins, <=[600, 660, 720, 780, 840], idx
      LOOKUP idx, [420, 360, 300, 240, 180], relayTmr
      state = M_RELAY_ON
      Relay = IsOn
      dayMins = 0                               ' it's night now, so clear
    ENDIF
  ENDIF
  GOTO Main

Run_Relay:
  relayTmr = relayTmr - 1                       ' update timer
  IF relayTmr > 0 THEN                          ' still running?
    Relay = IsOn                                ' yes, refresh relay
    IF lightLevel < NIGHT THEN
      dayMins = dayMins + 1
      IF dayMins = LIGHT_THRESH THEN
        state = M_RELAY_OFF
      ENDIF
    ELSE
      dayMins = 0                               ' clear false light
    ENDIF
  ELSE
    Relay = IsOff                               ' no, kill relay
    state = M_RELAY_OFF                         ' update state
  ENDIF
  GOTO Main

Wait_For_Day:
  Relay = IsOff
  IF lightLevel < NIGHT THEN                    ' check light
    dayMins = dayMins + 1                       ' update if day detected
    IF dayMins = LIGHT_THRESH THEN              ' full daylight?
      state = M_DAYLIGHT                        ' yes, update state
    ENDIF
  ELSE
    dayMins = 0                                 ' no, clear false light
  ENDIF
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

samsam

JonnyMac

Thank You for posting this code for me

It will take some time for me to understand ALL that you have here

I do understand most of what you have here

Thanks agian for all of your help
Sam

I Want  Thank You for All Of Your Time And Help

...............In Helping Getting Thing To Work........