Shop OBEX P1 Docs P2 Docs Learn Events
Reading switches — Parallax Forums

Reading switches

ArchiverArchiver Posts: 46,084
edited 2003-01-29 18:31 in General Discussion
This is probably a newbie question but I'm not sure how to read
multiple switches and use the info for a particular application. I am
building a motor control with fwd,rev,go,stop and limit switch. I
understand the loop technique of debouncing-
SwIn VAR InA ' Five inputs, pins 0 - 4
switches VAR Word ' Debounced inputs
GetSwitches
switches = %11111 ' enable all five inputs
FOR x = 1 TO 10
swtches = swtches & ~SwIn ' test inputs
PAUSE 5 ' delay between tests
NEXT
Return
-what I'm stumped by is how to use the info of the switches(ie. look
for just the "Stop" and/or "Limit" switch and use it to stop the
motor). Do you specify a particular bit in the variable "switches"?
Is there a better way to do this? I am using the PAK-V(pre MOSFET H-
bridge) to run the motor to ramp up to speed on a "Start" and visa-
versa on a "Stop". Any help is appreciated. Ian

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-01-28 21:02
    Hi Ian,

    Well, there are many ways you might do this. One is you can use
    individual variables for each input:

    Fwdbutton var in0
    Revbutton var in1

    If Fwdbutton=0 then go_forward

    As you point out, you'll need to debounce.

    You can also use the BUTTON command in a loop to provide some
    debouncing, delay, and repeat functions.

    If you wanted to keep reading them together, you could extract the
    information you want with a bitmask:

    Fwdbtn con %1
    Revbtn con %10

    Switches=~SwIn

    If Switches & Fwdbtn then go_forward


    Hope that gives you some ideas....

    Al Williams
    AWC
    * Easy RS-232 Prototyping
    http://www.al-williams.com/awce/rs1.htm



    >
    Original Message
    > From: iphillipsca <iphillips@s...>
    > [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=e6fvZrPLlH9d6ZnIUpMo2tyepQV6rmRHnFK5AqxThtDXeMBZfyokJSMz00XiQ-lJu32Uz4yuFusMV2yo]iphillips@s...[/url
    > Sent: Tuesday, January 28, 2003 2:52 PM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] Reading switches
    >
    >
    > This is probably a newbie question but I'm not sure how to read
    > multiple switches and use the info for a particular application. I am
    > building a motor control with fwd,rev,go,stop and limit switch. I
    > understand the loop technique of debouncing-
    > SwIn VAR InA ' Five inputs, pins 0 - 4
    > switches VAR Word ' Debounced inputs
    > GetSwitches
    > switches = %11111 ' enable all five inputs
    > FOR x = 1 TO 10
    > swtches = swtches & ~SwIn ' test inputs
    > PAUSE 5 ' delay between tests
    > NEXT
    > Return
    > -what I'm stumped by is how to use the info of the switches(ie. look
    > for just the "Stop" and/or "Limit" switch and use it to stop the
    > motor). Do you specify a particular bit in the variable "switches"?
    > Is there a better way to do this? I am using the PAK-V(pre MOSFET H-
    > bridge) to run the motor to ramp up to speed on a "Start" and
    > visa- versa on a "Stop". Any help is appreciated. Ian
    >
    >
    > 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 2003-01-29 15:24
    Thanks Al. One question though, if for instance I look for a "Limit" by
    Fwd con %1
    Rev con %10
    Jog con %100
    Limitswitch con %1000
    Stopswitch con %10000
    'code
    If Limit = %10000 then stop
    If Stopswitch = %1000 then stop
    -will this still work when one of the other switches are pressed? For
    example a reading when Fwd and the Limit are switched- %1001. Thanks,
    Ian

    Original Message
    From: Al Williams [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=7Yl2QFDSsxaTNW7N5Y9OkW1XQ1DEb3dba7Wxw6MlT2pHvBVTXHetQxu6O-HH7-LCXVMtZZiOU-w3]alw@a...[/url
    Sent: January 28, 2003 4:03 PM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] Reading switches

    Hi Ian,

    Well, there are many ways you might do this. One is you can use
    individual variables for each input:

    Fwdbutton var in0
    Revbutton var in1

    If Fwdbutton=0 then go_forward

    As you point out, you'll need to debounce.

    You can also use the BUTTON command in a loop to provide some
    debouncing, delay, and repeat functions.

    If you wanted to keep reading them together, you could extract the
    information you want with a bitmask:

    Fwdbtn con %1
    Revbtn con %10

    Switches=~SwIn

    If Switches & Fwdbtn then go_forward


    Hope that gives you some ideas....

    Al Williams
    AWC
    * Easy RS-232 Prototyping
    http://www.al-williams.com/awce/rs1.htm



    >
    Original Message
    > From: iphillipsca <iphillips@s...>
    > [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=aRvWFaYspzXxugIM2gUMYmnkfur53X7eL2D31143N3J9TT7frk0L76MAbLbGrW3_8vTv90MbjVpekEgz5s8]iphillips@s...[/url
    > Sent: Tuesday, January 28, 2003 2:52 PM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] Reading switches
    >
    >
    > This is probably a newbie question but I'm not sure how to read
    > multiple switches and use the info for a particular application. I am
    > building a motor control with fwd,rev,go,stop and limit switch. I
    > understand the loop technique of debouncing-
    > SwIn VAR InA ' Five inputs, pins 0 - 4
    > switches VAR Word ' Debounced inputs
    > GetSwitches
    > switches = %11111 ' enable all five inputs
    > FOR x = 1 TO 10
    > swtches = swtches & ~SwIn ' test inputs
    > PAUSE 5 ' delay between tests
    > NEXT
    > Return
    > -what I'm stumped by is how to use the info of the switches(ie. look
    > for just the "Stop" and/or "Limit" switch and use it to stop the
    > motor). Do you specify a particular bit in the variable "switches"?
    > Is there a better way to do this? I am using the PAK-V(pre MOSFET H-
    > bridge) to run the motor to ramp up to speed on a "Start" and
    > visa- versa on a "Stop". Any help is appreciated. Ian
    >
    >
    > 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/
    >
    >
    >
    >


    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 2003-01-29 15:25
    Binary correction.

    Original Message
    From: Ian Phillips
    Sent: January 29, 2003 10:24 AM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] Reading switches

    Thanks Al. One question though, if for instance I look for a "Limit" by
    Fwd con %1
    Rev con %10
    Jog con %100
    Limitswitch con %1000
    Stopswitch con %10000
    'code
    If Limit = %1000 then stop
    If Stopswitch = %10000 then stop
    -will this still work when one of the other switches are pressed? For
    example a reading when Fwd and the Limit are switched- %1001. Thanks,
    Ian

    Original Message
    From: Al Williams [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=9iOICNbT2-kK5_SUbAhRuxUi4QYZ6Y6osfSnGpEUDSrTEJrN0yX94oRQ51-fJ8aGvbk06q_H9vC80R2VcQ]alw@a...[/url
    Sent: January 28, 2003 4:03 PM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] Reading switches

    Hi Ian,

    Well, there are many ways you might do this. One is you can use
    individual variables for each input:

    Fwdbutton var in0
    Revbutton var in1

    If Fwdbutton=0 then go_forward

    As you point out, you'll need to debounce.

    You can also use the BUTTON command in a loop to provide some
    debouncing, delay, and repeat functions.

    If you wanted to keep reading them together, you could extract the
    information you want with a bitmask:

    Fwdbtn con %1
    Revbtn con %10

    Switches=~SwIn

    If Switches & Fwdbtn then go_forward


    Hope that gives you some ideas....

    Al Williams
    AWC
    * Easy RS-232 Prototyping
    http://www.al-williams.com/awce/rs1.htm



    >
    Original Message
    > From: iphillipsca <iphillips@s...>
    > [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=NWa8g-ho7ESsAJEXmP563hqcTl2zX6IhjGoXMQ2va64Ph-OhGx_u4nXF9i-tGjD26i4X2qbZOpw5iQRyB1yI_A]iphillips@s...[/url
    > Sent: Tuesday, January 28, 2003 2:52 PM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] Reading switches
    >
    >
    > This is probably a newbie question but I'm not sure how to read
    > multiple switches and use the info for a particular application. I am
    > building a motor control with fwd,rev,go,stop and limit switch. I
    > understand the loop technique of debouncing-
    > SwIn VAR InA ' Five inputs, pins 0 - 4
    > switches VAR Word ' Debounced inputs
    > GetSwitches
    > switches = %11111 ' enable all five inputs
    > FOR x = 1 TO 10
    > swtches = swtches & ~SwIn ' test inputs
    > PAUSE 5 ' delay between tests
    > NEXT
    > Return
    > -what I'm stumped by is how to use the info of the switches(ie. look
    > for just the "Stop" and/or "Limit" switch and use it to stop the
    > motor). Do you specify a particular bit in the variable "switches"?
    > Is there a better way to do this? I am using the PAK-V(pre MOSFET H-
    > bridge) to run the motor to ramp up to speed on a "Start" and
    > visa- versa on a "Stop". Any help is appreciated. Ian
    >
    >
    > 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/
    >
    >
    >
    >


    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/



    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 2003-01-29 15:29
    Yeah, I see you already corrected the extra 0. But the answer is still
    no. That's why you use the &. Let's say that Limit and Fwd are set at
    the same time (%1001).

    %1001 = %1000 FALSE (0)

    %1001 & %1000 TRUE (because %1000 is not zero)

    In3 TRUE (1)

    So if you use in0, in1, in2, etc. that sidesteps the whole problem. If
    you still want to read a snapshot, use the & operator to mask off the
    bits you want to test.

    Hope that helps.

    Al Williams
    AWC
    * Limit monitor 8 channels at once
    http://www.al-williams.com/awce/pak10.htm



    >
    Original Message
    > From: Ian Phillips [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=ipsaLGo7LXCprc8oqNCvAV-80SIqPr69BfkvZZRD8qXSxryShoan3qaV8nnnt-NbKfVoY1nqa31eOoQV]iphillips@s...[/url
    > Sent: Wednesday, January 29, 2003 9:24 AM
    > To: basicstamps@yahoogroups.com
    > Subject: RE: [noparse][[/noparse]basicstamps] Reading switches
    >
    >
    > Thanks Al. One question though, if for instance I look for a
    > "Limit" by
    > Fwd con %1
    > Rev con %10
    > Jog con %100
    > Limitswitch con %1000
    > Stopswitch con %10000
    > 'code
    > If Limit = %10000 then stop
    > If Stopswitch = %1000 then stop
    > -will this still work when one of the other switches are
    > pressed? For example a reading when Fwd and the Limit are
    > switched- %1001. Thanks, Ian
    >
    >
    Original Message
    > From: Al Williams [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=KytA3tPhrCdzrGHk8Fua63JBIozYxpw9w6JWqdnoBNxnHZYxO_GNsyXEK9iKpIcDBThNlVF5a9Ci474-Rg]alw@a...[/url
    > Sent: January 28, 2003 4:03 PM
    > To: basicstamps@yahoogroups.com
    > Subject: RE: [noparse][[/noparse]basicstamps] Reading switches
    >
    > Hi Ian,
    >
    > Well, there are many ways you might do this. One is you can
    > use individual variables for each input:
    >
    > Fwdbutton var in0
    > Revbutton var in1
    >
    > If Fwdbutton=0 then go_forward
    >
    > As you point out, you'll need to debounce.
    >
    > You can also use the BUTTON command in a loop to provide some
    > debouncing, delay, and repeat functions.
    >
    > If you wanted to keep reading them together, you could
    > extract the information you want with a bitmask:
    >
    > Fwdbtn con %1
    > Revbtn con %10
    >
    > Switches=~SwIn
    >
    > If Switches & Fwdbtn then go_forward
    >
    >
    > Hope that gives you some ideas....
    >
    > Al Williams
    > AWC
    > * Easy RS-232 Prototyping http://www.al-williams.com/awce/rs1.htm
    >
    >
    >
    > >
    Original Message
    > > From: iphillipsca <iphillips@s...>
    > > [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=ipsaLGo7LXCprc8oqNCvAV-80SIqPr69BfkvZZRD8qXSxryShoan3qaV8nnnt-NbKfVoY1nqa31eOoQV]iphillips@s...[/url
    > > Sent: Tuesday, January 28, 2003 2:52 PM
    > > To: basicstamps@yahoogroups.com
    > > Subject: [noparse][[/noparse]basicstamps] Reading switches
    > >
    > >
    > > This is probably a newbie question but I'm not sure how to read
    > > multiple switches and use the info for a particular
    > application. I am
    > > building a motor control with fwd,rev,go,stop and limit switch. I
    > > understand the loop technique of debouncing-
    > > SwIn VAR InA ' Five inputs, pins 0 - 4
    > > switches VAR Word ' Debounced inputs
    > > GetSwitches
    > > switches = %11111 ' enable all five inputs
    > > FOR x = 1 TO 10
    > > swtches = swtches & ~SwIn ' test inputs
    > > PAUSE 5 ' delay between tests
    > > NEXT
    > > Return
    > > -what I'm stumped by is how to use the info of the switches(ie. look
    > > for just the "Stop" and/or "Limit" switch and use it to stop the
    > > motor). Do you specify a particular bit in the variable "switches"?
    > > Is there a better way to do this? I am using the PAK-V(pre MOSFET H-
    > > bridge) to run the motor to ramp up to speed on a "Start" and
    > > visa- versa on a "Stop". Any help is appreciated. Ian
    > >
    > >
    > > 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/
    > >
    > >
    > >
    > >
    >
    >
    > 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/



    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 2003-01-29 16:41
    Another take.. Pbasic (all versions) allows you to make alias names
    for the the bits within a variable. That is handy. Once the whole
    byte is debounced, the order of your if-then statements will
    determine the priority.

    '{$PBASIC 2.5}
    switches var byte ' to read all the switches
    stopit var switches.bit4 ' individual bits as part of whole
    limit var switches.bit3
    jog var switches.bit2
    rev var switches.bit1
    fwd var switches.bit0

    main:
    do
    switches=inH ' read 5 switches (inA only reads 4)
    ' more debouncing here
    ' then tasks...
    if stopit then dostopit ' if bit is = 1
    if limit then dolimit ' arranged by priority
    if ' and so on...
    loop

    dostopit:
    '
    goto main ' I'm not sure if Pbasic 2.5 allows a "loop" here.

    dolimit:
    '
    goto main

    ' ...
    end


    BTW, a lot of debouncing can be accomplished with an RCdiode circuit
    on the pin input, if you don't want to do it in software.

    -- Tracy



    >Thanks Al. One question though, if for instance I look for a "Limit" by
    >Fwd con %1
    >Rev con %10
    >Jog con %100
    >Limitswitch con %1000
    >Stopswitch con %10000
    >'code
    >If Limit = %10000 then stop
    >If Stopswitch = %1000 then stop
    >-will this still work when one of the other switches are pressed? For
    >example a reading when Fwd and the Limit are switched- %1001. Thanks,
    >Ian
    >
    >
    Original Message
    >From: Al Williams [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=5CaAZs1iBxRj0vmxNBWwJNo2tWtbLwuNinASVFaoE3klC5Mj68hkKyEveZrZXHi77N4bMBMzxqz-EvlPaBQ]alw@a...[/url
    >Sent: January 28, 2003 4:03 PM
    >To: basicstamps@yahoogroups.com
    >Subject: RE: [noparse][[/noparse]basicstamps] Reading switches
    >
    >Hi Ian,
    >
    >Well, there are many ways you might do this. One is you can use
    >individual variables for each input:
    >
    >Fwdbutton var in0
    >Revbutton var in1
    >
    >If Fwdbutton=0 then go_forward
    >
    >As you point out, you'll need to debounce.
    >
    >You can also use the BUTTON command in a loop to provide some
    >debouncing, delay, and repeat functions.
    >
    >If you wanted to keep reading them together, you could extract the
    >information you want with a bitmask:
    >
    >Fwdbtn con %1
    >Revbtn con %10
    >
    >Switches=~SwIn
    >
    >If Switches & Fwdbtn then go_forward
    >
    >
    >Hope that gives you some ideas....
    >
    >Al Williams
    >AWC
    >* Easy RS-232 Prototyping
    >http://www.al-williams.com/awce/rs1.htm
    >
    >
    >
    > >
    Original Message
    > > From: iphillipsca <iphillips@s...>
    > > [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=_Pzs8fqr1k97WMW6k84RvBoPUcuk1pMJAgVidUo4NbR1w3hO1luZzEmPfb27I9zQgnQsbhl1Rq3QJFol]iphillips@s...[/url
    > > Sent: Tuesday, January 28, 2003 2:52 PM
    > > To: basicstamps@yahoogroups.com
    > > Subject: [noparse][[/noparse]basicstamps] Reading switches
    > >
    > >
    > > This is probably a newbie question but I'm not sure how to read
    > > multiple switches and use the info for a particular application. I am
    > > building a motor control with fwd,rev,go,stop and limit switch. I
    > > understand the loop technique of debouncing-
    > > SwIn VAR InA ' Five inputs, pins 0 - 4
    > > switches VAR Word ' Debounced inputs
    > > GetSwitches
    > > switches = %11111 ' enable all five inputs
    > > FOR x = 1 TO 10
    > > swtches = swtches & ~SwIn ' test inputs
    > > PAUSE 5 ' delay between tests
    > > NEXT
    > > Return
    > > -what I'm stumped by is how to use the info of the switches(ie. look
    > > for just the "Stop" and/or "Limit" switch and use it to stop the
    > > motor). Do you specify a particular bit in the variable "switches"?
    > > Is there a better way to do this? I am using the PAK-V(pre MOSFET H-
    > > bridge) to run the motor to ramp up to speed on a "Start" and
    > > visa- versa on a "Stop". Any help is appreciated. Ian
    > >
  • ArchiverArchiver Posts: 46,084
    edited 2003-01-29 16:48
    I haven't heard of a RCdiode circuit before. Any info?
    Ian

    Original Message
    From: Tracy Allen [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=Az6C82tnV8GMXERUdIrWQJs4xZi5ByoySrOQsnPoof4sEe5Cte3SKPiHRSiyY0Tve1gKEHv21x3uN_Jyk3w]tracy@e...[/url
    Sent: January 29, 2003 11:41 AM
    To: basicstamps@yahoogroups.com
    Subject: RE: [noparse][[/noparse]basicstamps] Reading switches

    Another take.. Pbasic (all versions) allows you to make alias names
    for the the bits within a variable. That is handy. Once the whole
    byte is debounced, the order of your if-then statements will
    determine the priority.

    '{$PBASIC 2.5}
    switches var byte ' to read all the switches
    stopit var switches.bit4 ' individual bits as part of whole
    limit var switches.bit3
    jog var switches.bit2
    rev var switches.bit1
    fwd var switches.bit0

    main:
    do
    switches=inH ' read 5 switches (inA only reads 4)
    ' more debouncing here
    ' then tasks...
    if stopit then dostopit ' if bit is = 1
    if limit then dolimit ' arranged by priority
    if ' and so on...
    loop

    dostopit:
    '
    goto main ' I'm not sure if Pbasic 2.5 allows a "loop" here.

    dolimit:
    '
    goto main

    ' ...
    end


    BTW, a lot of debouncing can be accomplished with an RCdiode circuit
    on the pin input, if you don't want to do it in software.

    -- Tracy



    >Thanks Al. One question though, if for instance I look for a "Limit" by
    >Fwd con %1
    >Rev con %10
    >Jog con %100
    >Limitswitch con %1000
    >Stopswitch con %10000
    >'code
    >If Limit = %10000 then stop
    >If Stopswitch = %1000 then stop
    >-will this still work when one of the other switches are pressed? For
    >example a reading when Fwd and the Limit are switched- %1001. Thanks,
    >Ian
    >
    >
    Original Message
    >From: Al Williams [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=gH11jLB_kON7Idyw26Sj3-KiKX1VNJLgRD_hhPeObY4y3oX9SD_zlOGFgMKcnmrI1g5esNtZh3nAN6C55A]alw@a...[/url
    >Sent: January 28, 2003 4:03 PM
    >To: basicstamps@yahoogroups.com
    >Subject: RE: [noparse][[/noparse]basicstamps] Reading switches
    >
    >Hi Ian,
    >
    >Well, there are many ways you might do this. One is you can use
    >individual variables for each input:
    >
    >Fwdbutton var in0
    >Revbutton var in1
    >
    >If Fwdbutton=0 then go_forward
    >
    >As you point out, you'll need to debounce.
    >
    >You can also use the BUTTON command in a loop to provide some
    >debouncing, delay, and repeat functions.
    >
    >If you wanted to keep reading them together, you could extract the
    >information you want with a bitmask:
    >
    >Fwdbtn con %1
    >Revbtn con %10
    >
    >Switches=~SwIn
    >
    >If Switches & Fwdbtn then go_forward
    >
    >
    >Hope that gives you some ideas....
    >
    >Al Williams
    >AWC
    >* Easy RS-232 Prototyping
    >http://www.al-williams.com/awce/rs1.htm
    >
    >
    >
    > >
    Original Message
    > > From: iphillipsca <iphillips@s...>
    > > [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=IbeMPXicysdOMZ1-4qEoklInklN7Obgtfv4f_AxwMAadPlTAxo7iT-9alLHFzgMZIoKcrZlPgvWHx0JsilSTxw]iphillips@s...[/url
    > > Sent: Tuesday, January 28, 2003 2:52 PM
    > > To: basicstamps@yahoogroups.com
    > > Subject: [noparse][[/noparse]basicstamps] Reading switches
    > >
    > >
    > > This is probably a newbie question but I'm not sure how to read
    > > multiple switches and use the info for a particular application. I
    am
    > > building a motor control with fwd,rev,go,stop and limit switch. I
    > > understand the loop technique of debouncing-
    > > SwIn VAR InA ' Five inputs, pins 0 - 4
    > > switches VAR Word ' Debounced inputs
    > > GetSwitches
    > > switches = %11111 ' enable all five inputs
    > > FOR x = 1 TO 10
    > > swtches = swtches & ~SwIn ' test inputs
    > > PAUSE 5 ' delay between tests
    > > NEXT
    > > Return
    > > -what I'm stumped by is how to use the info of the switches(ie. look
    > > for just the "Stop" and/or "Limit" switch and use it to stop the
    > > motor). Do you specify a particular bit in the variable "switches"?
    > > Is there a better way to do this? I am using the PAK-V(pre MOSFET H-
    > > bridge) to run the motor to ramp up to speed on a "Start" and
    > > visa- versa on a "Stop". Any help is appreciated. Ian
    > >


    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 2003-01-29 16:48
    Well, in that vein, I would just say:

    Stopbtn var in4
    Fwdbtn var in0

    Etc.

    Which was what I led off with. The only reason I can see to go with the
    & scheme is if you wanted to look for chords. So you could say:

    If buttons & (fwdbtn | revbtn) then

    To detect if the forward and reverse button were pushed at once.

    Al Williams
    AWC
    * Easy RS-232 Prototyping
    http://www.al-williams.com/awce/rs1.htm




    >
    Original Message
    > From: Tracy Allen [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=4wGU3c8cKxf_YTJWpxPhv27VwjOn-Dc_JQOzT7nxeKZgWcvOdM-4QhPf25cDhNTNfWPabbn7yevbE2Y]tracy@e...[/url
    > Sent: Wednesday, January 29, 2003 10:41 AM
    > To: basicstamps@yahoogroups.com
    > Subject: RE: [noparse][[/noparse]basicstamps] Reading switches
    >
    >
    > Another take.. Pbasic (all versions) allows you to make alias names
    > for the the bits within a variable. That is handy. Once the whole
    > byte is debounced, the order of your if-then statements will
    > determine the priority.
    >
    > '{$PBASIC 2.5}
    > switches var byte ' to read all the switches
    > stopit var switches.bit4 ' individual bits as part of whole
    > limit var switches.bit3
    > jog var switches.bit2
    > rev var switches.bit1
    > fwd var switches.bit0
    >
    > main:
    > do
    > switches=inH ' read 5 switches (inA only reads 4)
    > ' more debouncing here
    > ' then tasks...
    > if stopit then dostopit ' if bit is = 1
    > if limit then dolimit ' arranged by priority
    > if ' and so on...
    > loop
    >
    > dostopit:
    > '
    > goto main ' I'm not sure if Pbasic 2.5 allows a "loop" here.
    >
    > dolimit:
    > '
    > goto main
    >
    > ' ...
    > end
    >
    >
    > BTW, a lot of debouncing can be accomplished with an RCdiode circuit
    > on the pin input, if you don't want to do it in software.
    >
    > -- Tracy
    >
    >
    >
    > >Thanks Al. One question though, if for instance I look for a
    > "Limit" by
    > >Fwd con %1
    > >Rev con %10
    > >Jog con %100
    > >Limitswitch con %1000
    > >Stopswitch con %10000
    > >'code
    > >If Limit = %10000 then stop
    > >If Stopswitch = %1000 then stop
    > >-will this still work when one of the other switches are
    > pressed? For
    > >example a reading when Fwd and the Limit are switched-
    > %1001. Thanks,
    > >Ian
    > >
    > >
    Original Message
    > >From: Al Williams [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=VJiXBEVINvx8nuUyt7DPgcPT8_9JZvJRVWw31Y7v4i4_Wzll15gyd__b9XGjjADtDnP1XBtT_HrvxVI]alw@a...[/url
    > >Sent: January 28, 2003 4:03 PM
    > >To: basicstamps@yahoogroups.com
    > >Subject: RE: [noparse][[/noparse]basicstamps] Reading switches
    > >
    > >Hi Ian,
    > >
    > >Well, there are many ways you might do this. One is you can use
    > >individual variables for each input:
    > >
    > >Fwdbutton var in0
    > >Revbutton var in1
    > >
    > >If Fwdbutton=0 then go_forward
    > >
    > >As you point out, you'll need to debounce.
    > >
    > >You can also use the BUTTON command in a loop to provide some
    > >debouncing, delay, and repeat functions.
    > >
    > >If you wanted to keep reading them together, you could extract the
    > >information you want with a bitmask:
    > >
    > >Fwdbtn con %1
    > >Revbtn con %10
    > >
    > >Switches=~SwIn
    > >
    > >If Switches & Fwdbtn then go_forward
    > >
    > >
    > >Hope that gives you some ideas....
    > >
    > >Al Williams
    > >AWC
    > >* Easy RS-232 Prototyping http://www.al-williams.com/awce/rs1.htm
    > >
    > >
    > >
    > > >
    Original Message
    > > > From: iphillipsca <iphillips@s...>
    > > > [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=EOkYA1_-lm4Yq4HU4j6ed8RSp20wyj8GiHetEqTuIW6zop2iPOS2W42c62882M_R8Soq4-n3JFmPujoBwfc]iphillips@s...[/url
    > > > Sent: Tuesday, January 28, 2003 2:52 PM
    > > > To: basicstamps@yahoogroups.com
    > > > Subject: [noparse][[/noparse]basicstamps] Reading switches
    > > >
    > > >
    > > > This is probably a newbie question but I'm not sure how to read
    > > > multiple switches and use the info for a particular
    > application. I
    > > > am building a motor control with fwd,rev,go,stop and
    > limit switch. I
    > > > understand the loop technique of debouncing-
    > > > SwIn VAR InA ' Five inputs, pins 0 - 4
    > > > switches VAR Word ' Debounced inputs
    > > > GetSwitches
    > > > switches = %11111 ' enable all five inputs
    > > > FOR x = 1 TO 10
    > > > swtches = swtches & ~SwIn ' test inputs
    > > > PAUSE 5 ' delay between tests
    > > > NEXT
    > > > Return
    > > > -what I'm stumped by is how to use the info of the
    > switches(ie. look
    > > > for just the "Stop" and/or "Limit" switch and use it to stop the
    > > > motor). Do you specify a particular bit in the variable
    > "switches"?
    > > > Is there a better way to do this? I am using the
    > PAK-V(pre MOSFET H-
    > > > bridge) to run the motor to ramp up to speed on a "Start" and
    > > > visa- versa on a "Stop". Any help is appreciated. Ian
    > > >
    >
    >
    > 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 2003-01-29 18:31
    >I haven't heard of a RCdiode circuit before. Any info?
    >Ian
    >

    These circuits give about 4 milliseconds of debounce. When the
    switch closes or when the voltage goes low, the capacitor discharges
    rapidly. When the switch opens, the capacitor recharges slowly. So
    if the switch bounces for a millisecond, closing and opening rapidly,
    the Stamp does not see it.

    The resistor in series from the input to the capacitor slows down the
    response to rapid spikes. When the switch closes, it has to stay
    closed for at least a fraction of a millisecond before the capacitor
    discharges enough to register as low at the stamp input. The noise
    might come from the switch mounted a long way from the stamp in an
    area where there is a pop noise produced by compressors etc.

    Adjust the resistors and capacitor to get the required amount of debounce.

    In software debouncing, the stamp looks to be sure that the input is
    stable for 2 or more successive readings.

    The hardware debouncer is really important if you are using a command
    like COUNT or PULSIN, which will happily register every bit of bounce
    and noise.





    '
    /\/\
    Vdd stamp
    | 20k pullup
    |
    o---/\/\--o
    /\/\--- P0 Stamp
    | 2k | 1k protection
    o |
    switch / ===== 0.22uf
    / o |
    | |
    `
    o
    Vss common





    20k
    switched '---/\/\--- Vdd
    voltage 1N4148 |
    input o-/\/\---|<---o-o----/\/\--- P0 Stamp
    e.g. 2k | 1k protection
    0-5 |
    ===== 0.22uf
    |
    |
    o
    o
    Vss common
Sign In or Register to comment.