Shop OBEX P1 Docs P2 Docs Learn Events
Command for getting input — Parallax Forums

Command for getting input

slackjackslackjack Posts: 25
edited 2006-11-09 01:17 in BASIC Stamp
Hey all,

Here is the situation. I have this circuit thats always high (+5V) by default (hooked up to a specific pin of the basic stamp). I wish to tally the number of times it changes from high to low and store it in a variable for future reference. Whats the best way to do this?

--thank you

Comments

  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-10-29 21:16
    See the BASIC STamp COUNT instruction.
    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    StampPlot - Graphical Data Acquisition and Control
    AppBee -·2.4GHz Wireless Adapters & transceivers·for the BASIC Stamp & Other controllers·
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2006-10-29 21:19
    How often does it change and how long do you want to monitor it? And is the signal "clean", that is, is there a possibity of short noise pulses that should not be counted?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • slackjackslackjack Posts: 25
    edited 2006-10-29 21:29
    I guess I should have given a little more info. I want to monitor it through out the execution of a stage (when user enters data) of the program ex - based on what the user enters, mechanisms will cause the change it state (high to low and back to high again...). If the user for example enters 2, I will expect to to receive two changes of state i.e. it should go from high to low 2 times. When this is met, the pin shouldnt be monitored anymore until the user decides to enter another decimal value.
    I expect the change from high to low to be under 1 second, but greater that 700 ms ( I cant, at the moment say for sure). For all purposes, the environment is assumed noise free.
  • Tommy BotTommy Bot Posts: 60
    edited 2006-11-01 22:54
    ' Compare the current state of the device with the old state. You'll have to come up with the exact syntax and placement.

    OldState································· 'Last stored condition of input pin.
    CurrentState··························· 'Current condition of input pin

    IF OldState <> CurrentState THEN Counter = Counter + 1·· 'If they are different, increment your counter.

    ButtonPressTotal = Counter / 2···· 'This adjusts for a press and release regardless of how long someone holds the button down.
    OldState = NewState··················'This will update the OldState variable.


    Something along these lines should work.

    Tommy

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    (Frequently heard from other's)

    Tommy, I know it wasn't designed to·x, but can you make it·do x·anyway?

    ·
  • slackjackslackjack Posts: 25
    edited 2006-11-02 01:27
    Thats for that hint tommy [noparse]:)[/noparse] I'll work along those lines and see what the outcome will be.
  • slackjackslackjack Posts: 25
    edited 2006-11-02 22:28
    I've made the necessary code, but I have one problem. Here is a sample of the code:

    IF (userIN > 6) THEN EXIT
    IF (opto_in = 0 OR opto_in = 1) THEN old_state = 0
    IF opto_in = 1 THEN current_state = 1
    IF old_state <> current_state THEN Counter = Counter + 1
    move_UP = 0
    current_state = 0
    LOOP UNTIL (counter = next_floor)
    move_UP = 1
    



    old_state should always be 0, as this is used to compare to the current_state. When the two dont match, the counter should increment. So say the user enters 5, this means that the loop souldnt exit until the counter is equal to what the use entered. This implies that the signal should be changed 5 times for the counter to increment 5 times. But here is where the problem happens, the signal isnt broken 5 times, instead it is broken once (and the value of counter is always equal whatever the user entered).
    It seems that it is reading the state of the pin too quickly, hence the once only increment. How can I solve this?
    Is it this code thats suppose to solve that problem: ButtonPressTotal = Counter / 2 How is this suppose to help?

    --thanks again.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-11-03 08:29
    slackjack -

    Although there is only a small part of your program offered, I'll take a shot at what the problem is. This is only based loosely on the text of your message.

    I must presume you are using the BUTTON comand to field the "signal" of which you speak. I suspect you don't have your BUTTON command in a loop. When BUTTON is used it is expected that you will have it in a reasonably tight loop, so the command gets continually re-exectuted, until the target state condition occurs. Once that happens, then the BUTTON command will branch to the routine you have specified as the TargetState routine. I suspect you are escaping that (necessary) BUTTON loop prematurely, or there is no loop at all.

    If you provide the entire program it will be a good deal easier to troubleshoot it.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • slackjackslackjack Posts: 25
    edited 2006-11-03 17:00
    Hello,

    I am not using the COUNT command. I am strictly just monitor the pin for a change in state, then do the necessary requirements when the state is changed. I will provide my code. If there is a way to do this with the COUNT command, I will be glad to give it a test run.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    opto_in PIN 3                     ' Input from optoisolator interrupter
    move_up PIN 10                    ' Sends up signal to motor
    move_down PIN 9                   ' Sends down signal to motor
    userIN VAR Word                   ' Get user input
    old_state VAR Word                ' Previous state of pin 3
    current_state VAR Word            ' Current state of pin 3
    counter VAR Word                  ' Keeps track of state changes
    current_floor VAR Word
    next_floor VAR Word
    
    OUTPUT move_UP                    ' Make pin 10 an output
    OUTPUT move_DOWN                  ' Make pin 9 an output
    move_UP = 1                       ' Motor will not move when signalUP = 1
    move_DOWN = 1                     ' Motor will not move when signalDOWN = 1
    INPUT opto_in                     ' Make pin 3 an input
    
    
    GOTO initial
    
    initial:
    counter = 0
    old_state = 0
    current_state = 0
    DEBUG "Please enter your floor destination: ",CR
    DEBUGIN DEC1 userIN
    next_floor = userIN
    GOSUB up
    
    nextFLOOR:
    counter = 0
    old_state = 0
    current_state = 0                              
    current_floor = userIN
    DEBUG "Please enter your floor destination (nextFLOOR:)", CR
    DEBUGIN DEC1 userIN
    next_floor = 6 - current_floor
    IF (current_floor < userIN) THEN GOSUB up
    IF (current_floor >  userIN) THEN GOSUB down
    
    
    
    
    up:
    DO
    IF (userIN > 6) THEN EXIT
    IF (opto_in = 0 OR opto_in = 1) THEN old_state = 0
    IF opto_in = 1 THEN current_state = 1
    IF old_state <> current_state THEN Counter = Counter + 1
    move_up = 0
    current_state = 0
    LOOP UNTIL (counter = next_floor)
    move_up = 1
    GOSUB nextFLOOR
    
    
    down:
    DO
    IF (userIN > 6) THEN EXIT
    IF (opto_in = 0 OR opto_in = 1) THEN old_state = 0
    IF opto_in = 1 THEN current_state = 1
    IF old_state <> current_state THEN Counter = Counter + 1
    move_down = 0
    current_state = 0
    LOOP UNTIL (counter = (next_floor+(6-userIN)))
    move_down = 1
    
    



    If you need further clarification, just let me know.

    Post Edited (slackjack) : 11/9/2006 1:20:47 AM GMT
  • FranklinFranklin Posts: 4,747
    edited 2006-11-03 18:58
    up:
    DO
    IF (userIN > 6 OR userIN = userIN) THEN EXIT
    IF (opto_in = 0 OR opto_in = 1) THEN old_state = 0
    IF opto_in = 1 THEN current_state = 1
    IF old_state <> current_state THEN Counter = Counter + 1
    move_up = 0
    current_state = 0
    LOOP UNTIL (counter = next_floor)
    move_up = 1
    GOSUB nextFLOOR
    
    

    If userIN = userIN then EXIT ? won't this ALWAYS happen?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • slackjackslackjack Posts: 25
    edited 2006-11-04 00:59
    Oh, I'm sorry. This is the old version of the code. I've edited it, but I've given you guys the wrong the code. I will correct it. The code I tested had this fixed.
  • slackjackslackjack Posts: 25
    edited 2006-11-07 00:23
    anyone?
  • slackjackslackjack Posts: 25
    edited 2006-11-09 01:17
    Guys, I seriously need help with this one problem. If I add a time delay, would that do any good?


    When the pin is high for approx. 1 sec, the do loop reads it in too quickly (and increments the counter multiple times within that 1 sec period). So if I wanted the pin to be high 5 times to exit the loop, it will be high only one time before the loop exits.
Sign In or Register to comment.