Shop OBEX P1 Docs P2 Docs Learn Events
Multiple relays — Parallax Forums

Multiple relays

ArchiverArchiver Posts: 46,084
edited 2002-12-29 12:42 in General Discussion
Hi All,
I am trying to select multiple relays via a 4x4 matrix keypad. I can
achieve this by writing lots of subroutines for each realy. (READ
KEYPRSS, OUTPUT TO RELAY, VIEW RELAY STATUS, AND TOGGLE RELAY.
However, to write code for 24 relays fills all the available memory
space, and then some.

Is it possible to convert a key press into a variable "value" and
refer back to this "value" throughout the code. i.e. Read
the "value" corresponding to the key press, output that "value",
dispaly and toggle that "value"? This should reduce the code size
significantly.

If I assign a number to the the output. i.e OUTPUT 2, OUT2=1, TOGGLE
2, this works fine.

However, the following code does not work as I have
subsituted "value" for a number. How can this be achieved?

'{$STAMP BS2p}
'{$PORT COM1}

dirs=%1111111111111111 '0=input >> 1=output
bps con 240 '9600 Baudrate
dly con 10 'Global pause delay..
auxio 'use aux i/o
Rx con 13 'Serin (receive) pin 13
Value VAR BYTE
statusIN var byte 'staus on output pin
index var byte
pause 500
gosub reset 'reset lcd

loop:
auxio
serin Rx,bps,[noparse][[/noparse]w2]
if w2=8 then reset 'goto reset LCD
if w2=13 then tgl 'goto toggle
value = w2
index = 1
PUT Index, value

relay: 'store value of w2
GET index, value
debug dec value - 48,cr 'show key pressed
mainio
output(value)
out(value)=1

statusIN=in(value) 'read & display staus of relay
if IN(value)=0 then displayO
if IN2=1 then displayC
goto loop2

tgl: 'toggle
mainio
toggle (value)
if in(value)=0 then displayO
if in(value)=1 then displayC
goto loop

displayC:
gosub secline
serout 0,bps,[noparse][[/noparse]"RELAY ",(value), "CLOSED"]
goto loop

displayO:
gosub secline
serout 0,bps,[noparse][[/noparse]"RELAY ",(value), "OPEN "]
goto loop

secline:
pause dly
serout 0,bps,[noparse][[/noparse]254,192]
pause dly
return

reset:
mainio
serout 0,bps,[noparse][[/noparse]254,1]
pause dly
serout 0,bps,[noparse][[/noparse]"READY"]
pause 20
return

Any thoughts would be appreciated.

Ken

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-12-28 14:02
    Ken-

    As Peter noted, it's asking for trouble to use the pre-defined
    variables in conjuction with others you declare yourself.

    You can refer to the nth IN or OUT as an array element by
    using the syntax IN0(n) or OUT0(n). For instance:

    statusIN=in0(value) 'read & display staus of relay

    or

    out0(value)=1

    Other things:

    - debug dec value - 48,cr 'show key pressed

    This suggests to me that the value you are receiving is an ASCII
    numeric representation. If that's the case, I'd suggest you convert
    to a numeric value right after the SERIN to simplify things.
    Statements such as:

    output(value)

    won't work right unless the variable "value" holds the intended
    numeric quantity as opposed to an ASCII representation.

    - Be careful you don't exceed your Stamp's max current capacity by
    turning on too many relays with successive actions.

    - You can tighten your code up a bit by realizing that an input pin
    will either read as 0 or 1. If it's not 0 then it must be 1. No
    point testing for both states on successive IFs. You might also
    choose to display common display elements like "RELAY " or
    [noparse][[/noparse]DEC value] before the IF statement so you don't have to duplicate
    them in either case.


    Regards,

    Steve
  • ArchiverArchiver Posts: 46,084
    edited 2002-12-28 19:09
    Hi Ken,

    Rename w2 to something else.
    w0-w12 and b0-b25 are reserved names for fixed ram addresses.
    Now w2 is being overwritten by declared variables.

    Regards peter


    Oorspronkelijk bericht
    Van: digdul2002 <jduldig@s...> [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=AnHfTlzq0DlRSykShgF9WpDo0wa_LLegjqtgwKV4edm2JnNjmdDo8BojS6hTlZ1cUO5U7cVHloPwPw]jduldig@s...[/url
    Verzonden: vrijdag 27 december 2002 21:44
    Aan: basicstamps@yahoogroups.com
    Onderwerp: [noparse][[/noparse]basicstamps] Multiple relays

    Hi All,
    I am trying to select multiple relays via a 4x4 matrix keypad. I can
    achieve this by writing lots of subroutines for each realy. (READ
    KEYPRSS, OUTPUT TO RELAY, VIEW RELAY STATUS, AND TOGGLE RELAY.
    However, to write code for 24 relays fills all the available memory
    space, and then some.

    Is it possible to convert a key press into a variable "value" and
    refer back to this "value" throughout the code. i.e. Read
    the "value" corresponding to the key press, output that "value",
    dispaly and toggle that "value"? This should reduce the code size
    significantly.

    If I assign a number to the the output. i.e OUTPUT 2, OUT2=1, TOGGLE
    2, this works fine.

    However, the following code does not work as I have
    subsituted "value" for a number. How can this be achieved?

    '{$STAMP BS2p}
    '{$PORT COM1}

    dirs=%1111111111111111 '0=input >> 1=output
    bps con 240 '9600 Baudrate
    dly con 10 'Global pause delay..
    auxio 'use aux i/o
    Rx con 13 'Serin (receive) pin 13
    Value VAR BYTE
    statusIN var byte 'staus on output pin
    index var byte
    pause 500
    gosub reset 'reset lcd

    loop:
    auxio
    serin Rx,bps,[noparse][[/noparse]w2]
    if w2=8 then reset 'goto reset LCD
    if w2=13 then tgl 'goto toggle
    value = w2
    index = 1
    PUT Index, value

    relay: 'store value of w2
    GET index, value
    debug dec value - 48,cr 'show key pressed
    mainio
    output(value)
    out(value)=1

    statusIN=in(value) 'read & display staus of relay
    if IN(value)=0 then displayO
    if IN2=1 then displayC
    goto loop2

    tgl: 'toggle
    mainio
    toggle (value)
    if in(value)=0 then displayO
    if in(value)=1 then displayC
    goto loop

    displayC:
    gosub secline
    serout 0,bps,[noparse][[/noparse]"RELAY ",(value), "CLOSED"]
    goto loop

    displayO:
    gosub secline
    serout 0,bps,[noparse][[/noparse]"RELAY ",(value), "OPEN "]
    goto loop

    secline:
    pause dly
    serout 0,bps,[noparse][[/noparse]254,192]
    pause dly
    return

    reset:
    mainio
    serout 0,bps,[noparse][[/noparse]254,1]
    pause dly
    serout 0,bps,[noparse][[/noparse]"READY"]
    pause 20
    return

    Any thoughts would be appreciated.

    Ken


    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2002-12-28 19:30
    Hi Ken,

    The line
    if w2=9 then reset
    inside loop is a goto reset instead of a gosub reset

    When you use serin ,,[noparse][[/noparse]w2]
    You test for 8 (backspace) and 13 (entet), is that what you want?
    w2 is a word variable.
    I suggest to use serin ,,[noparse][[/noparse]dec inputvalue]
    where inputvalue is a bytevariable.

    Regards peter

    Oorspronkelijk bericht
    Van: digdul2002 <jduldig@s...> [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=You-XFmNg46d1yGgNKhJYCR7V56g5jCJKEgM0o7avYuWzcjq6GSROOl4t64ypdaParcAO7WPkaSbbg]jduldig@s...[/url
    Verzonden: vrijdag 27 december 2002 21:44
    Aan: basicstamps@yahoogroups.com
    Onderwerp: [noparse][[/noparse]basicstamps] Multiple relays

    Hi All,
    I am trying to select multiple relays via a 4x4 matrix keypad. I can
    achieve this by writing lots of subroutines for each realy. (READ
    KEYPRSS, OUTPUT TO RELAY, VIEW RELAY STATUS, AND TOGGLE RELAY.
    However, to write code for 24 relays fills all the available memory
    space, and then some.

    Is it possible to convert a key press into a variable "value" and
    refer back to this "value" throughout the code. i.e. Read
    the "value" corresponding to the key press, output that "value",
    dispaly and toggle that "value"? This should reduce the code size
    significantly.

    If I assign a number to the the output. i.e OUTPUT 2, OUT2=1, TOGGLE
    2, this works fine.

    However, the following code does not work as I have
    subsituted "value" for a number. How can this be achieved?

    '{$STAMP BS2p}
    '{$PORT COM1}

    dirs=%1111111111111111 '0=input >> 1=output
    bps con 240 '9600 Baudrate
    dly con 10 'Global pause delay..
    auxio 'use aux i/o
    Rx con 13 'Serin (receive) pin 13
    Value VAR BYTE
    statusIN var byte 'staus on output pin
    index var byte
    pause 500
    gosub reset 'reset lcd

    loop:
    auxio
    serin Rx,bps,[noparse][[/noparse]w2]
    if w2=8 then reset 'goto reset LCD
    if w2=13 then tgl 'goto toggle
    value = w2
    index = 1
    PUT Index, value

    relay: 'store value of w2
    GET index, value
    debug dec value - 48,cr 'show key pressed
    mainio
    output(value)
    out(value)=1

    statusIN=in(value) 'read & display staus of relay
    if IN(value)=0 then displayO
    if IN2=1 then displayC
    goto loop2

    tgl: 'toggle
    mainio
    toggle (value)
    if in(value)=0 then displayO
    if in(value)=1 then displayC
    goto loop

    displayC:
    gosub secline
    serout 0,bps,[noparse][[/noparse]"RELAY ",(value), "CLOSED"]
    goto loop

    displayO:
    gosub secline
    serout 0,bps,[noparse][[/noparse]"RELAY ",(value), "OPEN "]
    goto loop

    secline:
    pause dly
    serout 0,bps,[noparse][[/noparse]254,192]
    pause dly
    return

    reset:
    mainio
    serout 0,bps,[noparse][[/noparse]254,1]
    pause dly
    serout 0,bps,[noparse][[/noparse]"READY"]
    pause 20
    return

    Any thoughts would be appreciated.

    Ken


    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2002-12-29 12:42
    --- In basicstamps@yahoogroups.com, "S Parkis" <parkiss@e...> wrote:
    Hi Peter,
    I have tried to follow your comments, however I am struggling to make
    sense of what is happening.

    Does the following code look reasonable?

    I have exchanged (w2) for (value). Something odd has occurred with
    the "carraige return" and "backspace" keys as they now return very
    large numbers. This wasn't the case when I used w2. Returned values 8
    and 13.

    I can't seem to get the toggle function to work either.

    I would appreciate your comments - or anyone elses.
    Regards
    ken.
    CODE SO FAR

    '{$STAMP BS2p}
    '{$PORT COM1}

    dirs=%1111111111111111 '0=input >> 1=output
    bps con 240 '9600 Baudrate
    dly con 10
    auxio 'Global pause delay..
    va var in12 'valid key press
    Rx con 13 'Serin (receive) pin 13
    Value VAR BYTE(10)
    status var byte 'staus on output pin
    pause 50
    HIGH 11 'Set the pin high (Power LED on).

    goto reset 'reset lcd

    loop2:
    auxio
    if va=1 then loop2 'loop until valid data
    if va=0 then loop

    loop:
    auxio
    serin Rx,bps,[noparse][[/noparse]value] 'key in info
    pause 100
    debug value,cr '
    if value = 13 then tgl 'goto toggle
    if value = 8 then reset 'goto reset
    mainio
    dirs(value)=0
    output(value)

    tgl: 'toggle output
    mainio

    toggle (value)
    if out0(value)=0 then displayo 'display output
    if out0(value)=1 then displayc
    goto readkey

    displayC:
    gosub secline
    serout 0,bps,[noparse][[/noparse]"RLY ", (value), " CLOSED"]
    pause dly
    goto readkey

    displayO:
    gosub secline
    serout 0,bps,[noparse][[/noparse]"RLY ", (value), " OPEN "]
    pause dly
    goto readkey

    readkey:
    auxio
    serin Rx,bps,[noparse][[/noparse]value]
    if value=13 then tgl
    if value=8 then reset
    debug value
    goto loop

    secline:
    pause dly
    mainio
    serout 0,bps,[noparse][[/noparse]254,192]
    pause dly
    return

    reset:
    mainio
    serout 0,bps,[noparse][[/noparse]254,1]
    pause dly
    serout 0,bps,[noparse][[/noparse]"READY"]
    pause dly
    goto loop



    > Ken-
    >
    > As Peter noted, it's asking for trouble to use the pre-defined
    > variables in conjuction with others you declare yourself.
    >
    > You can refer to the nth IN or OUT as an array element by
    > using the syntax IN0(n) or OUT0(n). For instance:
    >
    > statusIN=in0(value) 'read & display staus of relay
    >
    > or
    >
    > out0(value)=1
    >
    > Other things:
    >
    > - debug dec value - 48,cr 'show key pressed
    >
    > This suggests to me that the value you are receiving is an ASCII
    > numeric representation. If that's the case, I'd suggest you
    convert
    > to a numeric value right after the SERIN to simplify things.
    > Statements such as:
    >
    > output(value)
    >
    > won't work right unless the variable "value" holds the intended
    > numeric quantity as opposed to an ASCII representation.
    >
    > - Be careful you don't exceed your Stamp's max current capacity by
    > turning on too many relays with successive actions.
    >
    > - You can tighten your code up a bit by realizing that an input pin
    > will either read as 0 or 1. If it's not 0 then it must be 1. No
    > point testing for both states on successive IFs. You might also
    > choose to display common display elements like "RELAY " or
    > [noparse][[/noparse]DEC value] before the IF statement so you don't have to duplicate
    > them in either case.
    >
    >
    > Regards,
    >
    > Steve
Sign In or Register to comment.