Shop OBEX P1 Docs P2 Docs Learn Events
Would it be the IF...ELSEIF Command or something else??? — Parallax Forums

Would it be the IF...ELSEIF Command or something else???

gr8_big_geekgr8_big_geek Posts: 34
edited 2009-07-15 19:53 in BASIC Stamp
Okay, I'm trying to run a proceedure until a button is pressed. If the button is not pressed then run the proceedure to the end of its cycle.

I'm having the hardest time getting this right. I've got all of my other coding to run my robot, all but this.

I'm trying to use the the if, elseif, goto commands, and I'm so turned around I don't know which way is up. I am using Basic Stamp v2.4 on windows XP SP2 (not sure if that matters, but I'll just throw it in there).

I have attached the file of the coding I'm working on right now. If you have some time, please look it over and shed some light on my blank spot. I usually am good at working these codes out, but I think I've just been looking at it too long.

Thank you...and I mean that!!! THANK YOU in advance, It really means a lot to me (I'm at the end of my rope)

Your Geek~

Comments

  • CounterRotatingPropsCounterRotatingProps Posts: 1,132
    edited 2009-07-14 17:53
    Hi Your Geek [noparse]:)[/noparse]

    two things:

    1. Indent your code so it's easier to see where the blocks are.
    2. Make sure your button/switch is debounced.

    RE 1:
    IF IN3 = 1 THEN
    GOTO time_me8
    
    'If it is not at its usual place then stop the program with "time_stopper".
    ELSEIF (IN3 = 0) THEN
    GOTO time_stopper
    
    time_me8:
    ....
    
    Should be
    
    IF IN3 = 1 THEN
       GOTO time_me8
    
        'If it is not at its usual place then stop the program with "time_stopper". <- not 
         sure if the comment is for above or below?
    ELSEIF (IN3 = 0) THEN
       GOTO time_stopper
    
       time_me8:   ...
    
    


    Once you indent, you might see if you've made logical errors (I didn't look that close, but jumps and labels nested inside IFs and DO's can get messy.)

    HTH
    - Howard

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2009-07-14 18:22
    how do you "debounce"? I've never heard of that but I'm interested...
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-07-14 18:23
    1. terminate IF... ELSE with ENDIF immediately. Even if it does not logically get there it is a bad practice to include code in wrong flow.
    2. Your labels GOTO are not "ended" properly - I would use subroutine instead just to return to your main if...else safely.
    ·· (But you would have to redo the main loop.)
    ·· Executing at label time_me8:··"falls thru " to yur next label even with improperly placed ENDIF

    3.·It is not necessary to test ELSEIF in = 0 since you have only two values 0 and 1 posible anyway.
    ····simple
    ··· IF IN = 1 then do this ....

    ·· ELSE
    ····· do this...
    ···ENDIF

    ·And as already·said - indent and you will be happier coder.

    Partialy edited code attached

    Cheers Vaclav


    PS To debounce is to eliminate mechnical devices ea pushbuttons in particaular to send multiple electrical pulses when single pulse is expected.
    ···· Check BUTTON command.

    Post Edited (vaclav_sal) : 7/14/2009 6:32:24 PM GMT
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2009-07-14 18:48
    I modified the coding so it is more simple to look at, I've attached it here. Also, I have included some notes in there as well that will explain a little more about what is going on and what I need to have done.

    It sounds like I'm needing some "step out" command that will allow my program to step outside of the loop, or nested looping. I thought that's what GOTO was used for, but maybe I'm using it incorrectly.

    I'm basically wanting to tell the code to run so long as the button is NOT pressed, once the button IS pressed terminated your action (of pressing your little robot arm forward), provide the time it took you to reach the "button press", and also STOP pushing forward!

    If you could help me figure that out, that would be awesome!!! I'm very willing to practice with some alternantive coding ideas, I'm just completely out of ideas on it. I've worked on my coding now for about a month. I have gotten all of the other aspects of the coding worked out great, but this one has got me beat.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2009-07-14 18:59
    Vaclav,

    I just got your edited version of the code. I will try that out and see if that works. My biggest concern was getting the code to know (at any point in time durying the running) whether the button was pressed, and at THAT moment go to option 2.

    For some reason it doesn't see when I press the button. If I start out in the beginning pressing the button, then it will go to option 2 but otherwise it does not seem to know/see when I'm pressing the button.

    Either way, I'll try your version and repost. Thank you for all of your time in helping me [noparse]:)[/noparse]

    Matt, eh-hem, gr8_big_geek~
  • CounterRotatingPropsCounterRotatingProps Posts: 1,132
    edited 2009-07-14 21:23
    Your Geek (actually, I want to say "Your Geekyness," as in "Your Highness" [noparse]:)[/noparse]

    > If I start out in the beginning pressing the button, then it will go to option 2

    Sounds like a bouncy switch. You see, when you press a mechanical switch, it actually doesn't close (or open) instantly at once. If you look at the connection with a fast enough detection device, you'll see that it is really turning on and off many times before it closes. That's why if you start off with the button pressed, it will have enough time to fully close *before* your code runs - so it get's it right (assuming there's no other logic errors in the code). Even if your code is right, this will cause problems, so you have to make sure it's properly debounce.

    Here's The Manual on switch bounce:

    http://www.ganssle.com/debouncing.pdf


    And if you do a forum search on 'debounce' you'll probably find more info. (Don't use the search button on the top right of this page - it's bjorked up - use: search.parallax.com)

    http://search.parallax.com/search?q=debounce

    > Thank you for all of your time in helping me [noparse]:)[/noparse]

    Hey, no problem - that's why we're here - and because we also all need assistance too [noparse]:)[/noparse]

    - Howard

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Post Edited (CounterRotatingProps) : 7/14/2009 9:29:05 PM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-07-14 22:03
    If you want keep checking your input then you send it back to the top of you look routine and see what status of your input


    'OPTION 1, IF BUTTON IS NOT PRESSED (or if it is at its usual position)
    'then continue to "time_me8"


    LooK:
    IF IN3 = 1 THEN
    GOTO time_me8
    'If it is not at its usual place then stop the program with "time_stopper".
    ELSEIF (IN3 = 0) THEN
    GOTO time_stopper
    RETURN

    I hope this helps let me know if this works for you

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2009-07-15 03:32
    Okay,· I tried the edited code and it still is not working. It recognizes the button push more, how should I say, effectively? In that it understands more simply when it is pressed at the very beginning of the run, however, it does not understand when I push the button during the process.

    It needs to stop (even if it happens to be half way through) when the button is pressed. If it doesn't it will knock all of my blocks over.

    Sam- I tried your code and the arm extended and stayed that way, even if/when I pressed the button. It just continued to repeat over and over again with the arm outwardly pressed.

    Still working on the coding for this. I did take a few things and put them into the attached, it still is not working completely yet. I'll keep on it though.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2009-07-15 03:34
    oh yeah, I have been reading up about the debouncing stuff...HOLY COW, that's a lot of information. I'm trying to make sense of that as well. I'm not so sure it's a switch triggering, or going off that is the problem. it's when the switch is pressed that the device doesn't seem to recognize it. I'm trying to get it to recognize a good old fashioned button press and stop doing what it's doing.

    I'll keep reading more about the bouncy bouncy stuff and see what I can make of it though
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2009-07-15 18:33
    Alright ya'll I got it figured out!

    I attached the completed copy to this so you can see. I think I've just been really tired and frustrated so I couldn't focus on it. I thought about it all night at work, then it hit me. I came straight home and worked in the code I had thought about and poof!

    It was my DO...LOOP, I was working it incorrectly.

    Thank you for all of your help, you all are so smart and it's wonderful to be able to chat with you!

    Matt~ AKA gr8_big_geek
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-07-15 19:36
    I need to tell you that I was not looking for proper operation of you program, just for programming functionality.

    If I was your "computer science"·teacher·you would get C-!

    Your IF· commands are still not correct.

    As stated before you need proper syntax

    ··· IF··

    ··· do something

    ·· ELSE

    ·· do something···

    ·· ENDIF



    Maybe what is missing - the processor does something when IF is true and than skips over the ELSE to the end of the IF command.

    It works in your case but you have a simple two way decisison to deal with.

    Cheers Vaclav









    ·
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2009-07-15 19:53
    Thank you for the compliment/insult?

    The way you are describing does not work for the functionality of this project because with using the standard format of IF..ELSE...ENDIF; as with your suggestions the button needs to be pressed at the time of the beginning of the code in order for the IF....(do something)...ELSE...(do something)...ENDIF to recognize option 2.

    The problem lies in looping back to the beginning and continuing that loop so that the code continues to check whether the button is pressed at any point. The loops are short enough that it checks quickly and there isn't a delay in the pressing of the button.

    It has never been about a fork in the road where the code needed to follow one route or another; it has been about continuing in one operation unless interrupted, or otherwise told to stop by a button press.

    In this example or coding the time counts, the operation continues down path A unless a button press interrupts its cycle, then it goes down path B, which is to provide the time it took and then END.

    I understand the function of the IF..ELSE..ENDIF...functions. However, I also understand my project.

    I'm working on a complex robotic device that has an airdraulic arm that is now functioning correctly thanks to the code I have attached above. It's a darn good thing you aren't my science teacher because you would be grading out of ignorance as apposed to logic.
Sign In or Register to comment.