Shop OBEX P1 Docs P2 Docs Learn Events
program — Parallax Forums

program

ArchiverArchiver Posts: 46,084
edited 2002-05-19 23:06 in General Discussion
hi :-)

i'm trying to write a program for the BS2 where i connect LED's to the
pins to demonstrate the algorithm i programmed in. When i built this i
will have actual external circuits connected to the pins but for now
i just want to have the controller run through different scenarios

any suggestion would be great.....

'=== constanst and pin
motion con pin 0
noise con pin 1
glass con pin 2
door con pin 3
buzz con pin 7
light con pin 8
light1 con pin 9


motion var bit
noise var bit
glass var bit
door var bit

motion = 0
noise = 1
glass = 1
door = 0

GOSUB check

motion = 1
noise = 0
glass = 1
door = 1

GOSUB check

motion = 1
noise = 1
glass = 1
door = 1

GOSUB check

end

check:
IF motion = 0 AMD noise = 1 AND glass = 1 AND door = 1
THEN LED
IF noise = 0 AND motion = 1 AND glass = 1 AND door = 1
THEN message
IF glass = 0 AND motion =1 AND noise = 1 AND door = 1
THEN message
IF door =0 AND noise = 1 AND glass = 1 AND motion = 1
THEN message

IF motion = 0 AND noise = 0 THEN Alarm
IF motion = 0 AND glass = 0 THEN Alarm
IF motion = 0 and door = 0 THEN Alarm
IF noise = 0 AND glass = 0 THEN Alarm
IF noise = 0 AND door = 0 THEN Alarm
IF glass = 0 AND door = 0 THEN Alarm

RETURN

LED:
low light
pause 250
high light
RETURN

Message:
FOR counter 0 to 3
GOSUB blink
NEXT

blink:
low light1
pause 50
high light1
pause 50
RETURN

Alarm:
low buzz
pause 50
high buzz
RETURN


does this look correct? i'm confused about the declaring the direction
of the pins and the input and output pins?? i have the scott edwards book
and the manual from the website but it's not clear to me

saying low light -- does that automatically make it a output?

- LaQuida

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-06-24 20:23
    I am trying to develop a program that I can insert into a line of Pbasic
    code that will look at an input and will count the number of transitions
    it makes from 0-1 and 1-0. I don't want it to slow the cpu down and I
    only want it add to the count when the input changes. Any one had
    experience in this area?


    Kind regards,


    Leroy
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-24 21:09
    Leroy,
    In just 1 line of code you can 'read' the staus of stamp inputs to
    determine if anything has changed. If an input has changed you can
    then go to a lookup table that will perform the appropriate function,
    in your case start counting. here is a sample of the code. The inl
    instruction looks at the low byte of ins, or pins 0 through 7 (ins
    looks at all 16). Mine were all active low inputs (normally at 5
    volts but pulled to 0 when activated). 255 means all were high and
    none were activated. If one of the inputs was activated (low) inl
    would be < than 255 and would continue on after the if then. This
    detection loop runs in ~500 us and cold be run periodically while
    doing other stuff.


    cell_detect:
    if inl = 255 then cell_detect 'inl Looks at pins 0-7. 255 = all high.
    io_status = inl ' Write the input status to a variable.

    x = 0
    for x = 0 to 255
    lookdown io_status,[noparse][[/noparse]254,253,251,247,239,223,191,127],look_index
    branch
    look_index,[noparse][[/noparse]cell_43,cell_44,cell_45,cell_46,cell_47,cell_48,cell_49,ce
    ll_50]
    next
    goto cell_detect



    > I am trying to develop a program that I can insert into a line of
    Pbasic
    > code that will look at an input and will count the number of
    transitions
    > it makes from 0-1 and 1-0. I don't want it to slow the cpu down
    and I
    > only want it add to the count when the input changes. Any one had
    > experience in this area?
    >
    >
    > Kind regards,
    >
    >
    > Leroy
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-25 13:40
    My input signal is going to be turning on for 1/2 second and then off
    for one half second. I want a variable to keep track of how many times
    this occurs, without slowing down the processor. is suedo code:

    start

    Look at input and check if on or off

    if on goto to "on"
    if off goto "off"

    goto start


    on:
    if a transition just occurred from off to on add one to count
    return


    off:

    do something???

    Thanks for your help, the group is just great..

    Leroy
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-25 14:20
    Hi Leroy,

    Lets define ON and OFF

    OFF con 0
    ON con 1

    Oldin var bit
    Newin var bit
    Count var word

    Assume input 1 is our input

    Newin=in1

    Loop:
    Oldin=newin
    Newin=in1
    If newin ^ oldin & newin = 0 then continue
    Count=count+1 'only if transition from 0 to 1
    Continue:
    Do something
    Goto loop

    Greetings peter
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-25 14:31
    Hi Leroy,

    One extra remark: your 'do something' code MUST take less than 0.5 secs or
    You will miss transitions.

    Freetings peter
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-25 14:58
    If I understand correctly, you want the code to take the same amount of time
    to execute whether a transition is detected or not?

    Original Message

    > One extra remark: your 'do something' code MUST take less than 0.5 secs or
    > You will miss transitions.
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-25 15:29
    Hi Rodent and Leroy,

    The code executed when the input is ON or OFF may be different but must be
    less then the time
    Leroy specified: 0.5secs ON and 0.5secs OFF.
    If the 'do something' would take 0.6secs, a positive AND negative transition
    can occur while executing
    the code for the OFF state resulting in a missed 0 -> 1 transition. Things
    would even get worse if the input
    was ON for 0.1secs and OFF for 0.9secs, then the execution time should be
    less than 0.1secs.

    Hope this clearifies the difficulty counting pulses while performing other
    tasks.

    Greetings peter
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-25 16:11
    Just read the port and add it to a counter. If its zero, the counter is not
    incremented -- if its one, well...

    A hex or binary 1 will add to a decimal counter with no problems.

    Original Message

    > The code executed when the input is ON or OFF may be different but must be
    > less then the time
    > Leroy specified: 0.5secs ON and 0.5secs OFF.
    > If the 'do something' would take 0.6secs, a positive AND negative
    transition
    > can occur while executing
    > the code for the OFF state resulting in a missed 0 -> 1 transition. Things
    > would even get worse if the input
    > was ON for 0.1secs and OFF for 0.9secs, then the execution time should be
    > less than 0.1secs.
    >
    > Hope this clearifies the difficulty counting pulses while performing other
    > tasks.
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-25 16:25
    Hi Rodent,

    >Just read the port and add it to a counter. If its zero, the counter is not
    >incremented -- if its one, well...

    >A hex or binary 1 will add to a decimal counter with no problems.


    As Leroy wants to count transitions, the above does not work.
    If I read the port the second time and it is still ON then my
    Count will not reflect the number of transitions. Sorry.


    Greetings peter
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-25 16:42
    Hi Rodent and Leroy,

    I could add the expression after the IF to the counter.
    If an entry point for code at a transition is not needed
    things can be simplified to:

    Lets define ON and OFF

    OFF con 0
    ON con 1

    Oldin var bit
    Newin var bit
    Count var word

    Assume input 1 is our input

    Newin=in1

    Loop:
    Oldin=newin
    Newin=in1
    Count = newin ^ oldin & newin + count
    Do something
    Goto loop

    The time constraint for 'do something' still holds.

    Greetings peter
  • ArchiverArchiver Posts: 46,084
    edited 2001-06-25 16:51
    To add one to the count whenever it changes state,

    old var bit ' state variable
    new var bit ' state variable
    all var word ' counter
    old=in0 ' init state variable
    loop:
    new=in0 ' read the input
    all=new ^ old + all ' count transition if any
    old=new ' update the state
    ' other stuff
    goto loop

    The code is constant time, about 1 millisecond, and it does not use
    any if-then logic, only math. To count only 0-1 transitions, the
    expression should be
    all=new ^ old & new + all
    Or to count only 1-0 transitions, use,
    all=new ^ old & old + all

    -- regards,
    Tracy Allen
    electronically monitored ecosystems
    mailto:tracy@e...
    http://www.emesystems.com


    At 3:23 PM -0400 6/24/01, Leroy Hall, Senior wrote:
    >>>I am trying to develop a program that I can insert into a line of Pbasic
    >>>code that will look at an input and will count the number of transitions
    >>>it makes from 0-1 and 1-0. I don't want it to slow the cpu down and I
    >>>only want it add to the count when the input changes. Any one had
    >>>experience in this area?
    >>>
    >>>
    >>>Kind regards,
    >>>
    >>>
    >>>Leroy

    "Peter Verkaik" <peterverkaik@b...> wrote:
    >>Hi Leroy,
    >>
    >>Lets define ON and OFF
    >>
    >>OFF con 0
    >>ON con 1
    >>
    >>Oldin var bit
    >>Newin var bit
    >>Count var word
    >>
    >>Assume input 1 is our input
    >>
    >>Newin=in1
    >>
    >>Loop:
    >>Oldin=newin
    >>Newin=in1
    >>If newin ^ oldin & newin = 0 then continue
    >>Count=count+1 'only if transition from 0 to 1
    >>Continue:
    >>Do something
    >>Goto loop
    >>
    >>Greetings peter

    count is a reserved variable!!


    At 10:11 AM -0500 6/25/01, Rodent wrote:
    >Just read the port and add it to a counter. If its zero, the counter is not
    >incremented -- if its one, well...
    >
    >A hex or binary 1 will add to a decimal counter with no problems.


    But he wants it to add to the count only when it makes a transition!
  • ArchiverArchiver Posts: 46,084
    edited 2002-05-19 21:45
    HI,

    Could anyone help me with this code.
    I want to convert it to a PIC16F84 but don't know where to start.


    Ren
  • ArchiverArchiver Posts: 46,084
    edited 2002-05-19 21:55
    For rene:

    There was no code attached, but generally, converting to PIC from Stamp is
    pretty simple. However, you will need a PICPro Basic compiler before you can
    compile and load the program. You will also need an editor such as
    "MicroCode Studio", which is a free download, and a programmer like the
    WARP-13.

    For a start, with PIC you always have to add

    Define osc 20 (if you are using a 20mhZ resonator) at the very beginning of
    your program.

    serouts become serout2 and serins become serin2

    If I can help you with specifics I will be happy to do so.

    Sid


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-05-19 23:06
    At 16:45 05/19/02, Ren
Sign In or Register to comment.