Shop OBEX P1 Docs P2 Docs Learn Events
!!!!! The Hardest Thing I've ever had to program !!!!!!!!!!!!!! — Parallax Forums

!!!!! The Hardest Thing I've ever had to program !!!!!!!!!!!!!!

gr8_big_geekgr8_big_geek Posts: 34
edited 2009-06-01 02:26 in BASIC Stamp
Greetings all!

I have a push button on the end of a rod. I have my servo set to·slide this rod forward UNLESS something is obstructing the way of the rod, in which case I want the servo·sliding the rod forward to STOP IMMEDIATELY!

To slide the rod forward I am using the following:

' {$STAMP BS2}
' {$PBASIC 2.5}
counter VAR Word

'**************************(Slide Rod Forward)******************************
    FOR counter = 1 TO 40
    PULSOUT 4, 1000
    PAUSE 22
    NEXT
    PAUSE 2000


This code works perfectly in sliding the rod forward, only it extends the rod completely forward regardless of obstructions.

****Here is where it gets tricky. I am trying to start a timecounter when the rod is told to slide forward, IF the button is pressed, I need the timecounter to STOP counting (at the same time the servo STOPS pushing the rod). I basically need to know how long this rod is being extended before it·becomes obstructed so I need the time to return, maybe in a debug mask? <---I'm not sure...

I am completely at my wits end. I have tried using nested loops and such but I am terrible at them. All of my variations have not worked. I have also played with that timecounter = timecounter yadda, yadda, yadda thing, but it has not worked well for me.

If you all have any suggestions please let me know.

Just as an FYI: I have two different buttons I can try. One clicks in when you press it, and it stays in until you press it again. Then I have another type of button that does not click in, it just presses, makes contact, and then bounces, or springs back to the same position. Should I use one over the other?

Completely lost,

gr8_big_geek

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-31 22:00
    The PULSOUT takes 2ms on a BS2. The PAUSE in the loop takes 22ms (for most servos, this needs to be about 18ms). The servos pulses are supposed to occur about 50 times a second. The FOR and NEXT statement and the PULSOUT and PAUSE statements don't take much to execute, so you could use a "tick" of about 24ms the way you've written your routine. 40 times through the FOR/NEXT loop will take about 0.96 seconds.

    When your limit switch is activated, just GOTO out of the loop and the loop counter will give you a time in units of 24ms.
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2009-06-01 00:17
    Oh gosh, I hate to sound absolutely ignorant, but could you provide sample coding that·I could apply/play with? I know what you mean about the ms, so I can change that, but·how would a GOTO command work nested into that, or in othe words, how would I make the button click trigger a GOTO command?

    Thank you so much for your reply and everything you are doing to help me; it really means a lot as I have really no friends I can talk to about this problem! [noparse]:)[/noparse]

    Matt, eh-hem, gr8_big_geek
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-01 00:36
    You need to go through the exercises in "What's a Microcontroller?" and the "Stamp Basic Syntax and Reference Manual". Both are downloadable from Parallax. The chapter on the BUTTON statement in the Manual shows one way to detect switch closures. Chapter #3 in "What's a Microcontroller?" deals entirely with pushbuttons.

    www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/139/Default.aspx?txtSearch=whats+a+microcontroller

    www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/143/Default.aspx?txtSearch=stamp+basic+syntax
  • SRLMSRLM Posts: 5,045
    edited 2009-06-01 00:37
    Main:
    IF IN1 = 0 THEN GOTO my_function
    
    my_function:
    'stuff
    GOTO Main
    
  • gr8_big_geekgr8_big_geek Posts: 34
    edited 2009-06-01 02:07
    I'm familiar with the push button section. I have the following:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    counter VAR Word
    timecounter VAR Word
    '**************************(Push Rod Forward)******************************
    FOR counter = 1 TO 40
    PULSOUT 4, 1000
    PAUSE 24
    NEXT
    '**************************(Start The Clock)*****************************
    DEBUG "Waiting until button is pressed.", CR, CR
    DO
    LOOP UNTIL IN3 = 0
    LOW 1
    HIGH 2
    timecounter = 0
    DO
    PAUSE 1
    timecounter = timecounter + 1
    LOOP UNTIL IN3 = 1
    LOW 14
    DEBUG "Your time was ", DEC timecounter,
    " ms.", CR, CR

    I just don't know how to fuse both of these together. The rod pushes forward, THEN my timer starts goes all the way to the end, and then the timer sequence begins. I don't know how to start the timer and the rod pushing with the exception that if the button is clicked then the whole thing stops and returns the total time activated.

    SRLM I see your code you put in there, but where do I type "main: IF" and such? Or where would I work it in my code?
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-01 02:26
    You put
    do
    loop until in3 = 0
    


    before your FOR and
    if in3 = 1 then goto panic
    


    after the PAUSE 24. Somewhere in your code you have
    panic: PULSOUT 750
    


    followed by what you want to do when the button is pushed. "counter" will have the value of the number of times your program has gone through the FOR loop. It's in units of 26ms. As I mentioned, you may need to change the 24 to 18 to keep the servo from jittering from too long a delay between pulses.
Sign In or Register to comment.