Shop OBEX P1 Docs P2 Docs Learn Events
Input Question — Parallax Forums

Input Question

staffshomestaffshome Posts: 28
edited 2009-10-29 18:25 in BASIC Stamp
Hello all,

use code below in my program.

IF modein = 1 THEN····························· 'latched switch, resets to 1 on power up
GOSUB Economy
ELSE
GOSUB Sport
ENDIF

I look at this input repeatedly & only want action on change of state,is there a way to do this.
thought maybe get scratchpad byte & current status but not sure how to do it

regards Adrian

·

Comments

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-10-29 15:39
    Use a one-bit internal variables to store the state. That allows the program to look for a change:

    modein  PIN 0       ' alias for the input pin
    modein0 VAR BIT   ' state variables for current and prior state
    modein1 VAR BIT
    
    modein1 = modein  ' initialize prior state
    DO
       modein0 = modein   ' read the pin
       IF modein1 ^ modein0 & modein1  THEN                              ' =1 iff pin changes low to high
          GOSUB Economy
          ELSE
          GOSUB Sport
       ENDIF
       modein1 = modein0    ' update the prior state
    LOOP
    
    Economy:  DEBUG "1" : RETURN
    Sport: DEBUG "0" : RETURN
    



    The logic statements involving XOR=^ and AND=& detect the change of state.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • staffshomestaffshome Posts: 28
    edited 2009-10-29 15:46
    Hi Tracy,

    Thanks for your reply,will giveit a go in my prog

    Regards Adrian
  • staffshomestaffshome Posts: 28
    edited 2009-10-29 17:17
    Hi Tracy,

    tried your code & appears to work same as my original ??.

    I could be i didn't explain what i need correctly.

    my program runs the code shown as part of a main loop & each time it loops back if for example:·modein =1

    it runs the sub routine if the next time loop runs if modein unchanged sub runs again (each sub drives·same digi-pot·with with different ramping rates so if sub repeats endlessly the solenoid driven from the pot cycles & this is showing up as a pressure pulse in solenoid hydraulic circuit. So really need sub not to run again until modein changes state.

    How that is a bit clearer.

    regards Adrian
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-10-29 18:25
    Be sure the initialization statement,
    modein1 = modein ' initialize prior state
    is NOT part of your main loop. On the other hand the update statement,
    modein1 = modein0 ' update the prior state
    does have to be in your main loop.


    The logic function can be chosen to do what you want:
    modein1 ^ modein0  ' detect transition either high to low or low to high
    or
    modein1 ^ modein0 & modein1  ' detect transition high to low
    or
    modein1 ^ modein0 & modein0  ' detect transition low to high
    




    If you stilll have trouble with it, post your distilled code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.