Shop OBEX P1 Docs P2 Docs Learn Events
Using th Button command — Parallax Forums

Using th Button command

NWCCTVNWCCTV Posts: 3,629
edited 2012-08-06 14:29 in BASIC Stamp
Questin, Would a Button command be the appropriate approach to the following code to invoke a "reset" after each of the Servo commands? If so, how would I code it and if not, what should I use.
IF x = 1 THEN
  pulseleft = 850: pulseright = 650: pulsecount = 24:
  DO UNTIL  (counter = pulsecount)
  PULSOUT 13, pulseleft
  PULSOUT 12, pulseright
  counter = counter+1000
  LOOP
ELSEIF x = 2 THEN
  pulseleft = 650: pulseright = 850: pulsecount = 24:
  DO UNTIL  (counter = pulsecount)
  PULSOUT 13, pulseleft
  PULSOUT 12, pulseright
  counter=counter+1000
  LOOP
ELSEIF x = 3 THEN
  pulseleft = 850: pulseright = 750: pulsecount = 24:
  DO UNTIL  (counter = pulsecount)
  PULSOUT 13, pulseleft
  PULSOUT 12, pulseright
  counter=counter+1000
  PAUSE 20
  LOOP
ELSEIF x = 4 THEN
  pulseleft = 750: pulseright = 650: pulsecount = 24:
  DO UNTIL  x = 6
  PULSOUT 13, pulseleft
  PULSOUT 12, pulseright
  counter=counter+1000
  PAUSE 20
  LOOP
ENDIF
counter = 0
LOOP

Comments

  • ercoerco Posts: 20,257
    edited 2012-07-31 14:08
    Not sure exactly what you want the pushbuttons to do, but you're missing a couple of PAUSE 20's in your code.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-07-31 14:23
    Yes, I am aware of this. What I need to do is invoke a reset within the software after each of the commands that turn the servos for the specified time.
  • Mike GMike G Posts: 2,702
    edited 2012-07-31 15:40
    What do you want to reset?

    This will loop forever as counter will never equal pulsecount.
     IF x = 1 THEN
      pulseleft = 850: pulseright = 650: pulsecount = 24:
      DO UNTIL  (counter = pulsecount)
      PULSOUT 13, pulseleft
      PULSOUT 12, pulseright
      counter = counter+1000
      LOOP
    

    Maybe this is what you are trying to do?
    *** NOT TESTED ***
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    LOOPS         CON 24
    RESET         CON 750
    
    x             VAR Byte
    pulseleft     VAR Word
    pulseright    VAR Word
    counter       VAR Word
    
    Init:
      counter = 0
      x = 0
    
    Main:
      x = (x + 1)//5
      GOSUB PrepareCommand
      GOSUB SendCommand
    GOTO Main
    
    
    PrepareCommand:
      IF x = 0 THEN
        pulseleft = RESET: pulseright = RESET
      ELSEIF x = 1 THEN
        pulseleft = 850: pulseright = 650
      ELSEIF x = 2 THEN
        pulseleft = 650: pulseright = 850
      ELSEIF x = 3 THEN
        pulseleft = 850: pulseright = 750
      ELSEIF x = 4 THEN
        pulseleft = 750: pulseright = 650
      ENDIF
    RETURN
    
    
    SendCommand:
      FOR counter = 0 TO LOOPS
        PULSOUT 13, pulseleft
        PULSOUT 12, pulseright
        PAUSE 20
      NEXT
    RETURN
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-08-01 11:44
    Echoing the question, what do you mean by "invoke a "reset""?

    The BUTTON command is usually overkill, unless you need the auto-repeat feature. It is easier to read the button-switch with a simple IN command. Are you using an original BS2?
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-08-01 13:29
    I am using a newer BS2. I need to reset the BS2 in the software after each Servo rotation. I know this loops and would need it to rerun after the reset. This needs to be applicaple for each of the Servo commands.
    IF x = 1 THEN
      pulseleft = 850: pulseright = 650: pulsecount = 24:
      DO UNTIL  (counter = pulsecount)
      PULSOUT 13, pulseleft
      PULSOUT 12, pulseright
      counter = counter+1000
      LOOP
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-08-02 09:04
    Sorry, I still don't understand. Maybe it would help if you could insert a comment or two in your example code to illustrate what you want to happen where. For example, is this what you want
    [FONT=courier new]IF x = 1 THEN
       pulseleft = 850 : pulseright = 650 : pulsecount = 24
      DO UNTIL  (counter = pulsecount)     
         PULSOUT 13, pulseleft
         PULSOUT 12, pulseright
         counter = counter+1000  ' <---- WHY R U adding 1000, to match with pulsecount=24?!
        ' if user presses the STOPbutton while this is
        ' running, then stop immediately, go back to 
        ' the main menu and wait for another command.
      [/FONT][FONT=courier new][/FONT][FONT=courier new]LOOP[/FONT]
    
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-08-05 17:49
    OK. I will try to clarify. I know I need to pull the "Loop" command out and place at the end of the code. I do not want the user to have to press anything. I want the sofware to send a "Reset" after each statement is executed. The code is executed via an external C# pushbutton. Each "Pushbotton" represents the numbers 1-4. I hope this clarifies the situation.
    IF x = 1 THEN  pulseleft = 850: pulseright = 650: pulsecount = 24:  DO UNTIL  (counter = pulsecount)  PULSOUT 13, pulseleft  PULSOUT 12, pulseright  counter = counter+1000 
     LOOP
    "[U][B]Reset Here"[/B][/U]
    ELSEIF x = 2 THEN 
     pulseleft = 650: pulseright = 850: pulsecount = 24:  DO UNTIL  (counter = pulsecount)  PULSOUT 13, pulseleft  PULSOUT 12, pulseright  counter=counter+
    1000  LOOP
    [U][B]"Reset Here"[/B][/U]
    ELSEIF x = 3 THEN
    pulseleft = 850: pulseright = 750: pulsecount = 24:  DO UNTIL  (counter = pulsecount)  PULSOUT 13, pulseleft  PULSOUT 12, pulseright  counter=counter+1000  PAUSE 20 
     LOOP
    [U][B]"Reset Here"[/B][/U]
    ELSEIF x = 4 
    THEN  pulseleft = 750: pulseright = 650: pulsecount = 24:  DO UNTIL  x = 6  PULSOUT 13, pulseleft  PULSOUT 12, pulseright  counter=counter+1000  PAUSE 20  LOOPENDIFcounter = 0LOOP[U][B]""Reset Here"[/B][/U]
    [IMG]http://forums.parallax.com/images/misc/progress.gif[/IMG][URL="http://forums.parallax.com/editpost.php?p=1114653&do=editpost"][IMG]http://forums.parallax.com/clear.gif[/IMG] Edit Post[/URL]
    
  • Mike GMike G Posts: 2,702
    edited 2012-08-05 18:55
    NWCCTV, what are you trying to reset? The STAMP, set the servos to neutral, initialize variables...???

    Are you trying to detect a reset command on the serial line issued by software running on a PC? If so, you must poll the serial line within your looping construct in PBASIC. The Stamp does not have a serial port buffer and the Stamp can only do one thing at a time. So, the only time the Stamp can detect a reset command on the serial line is when the Stamp is looking for a command on the serial line. The PC software should resend the reset command until the Stamp acknowledges receiving the reset command. After the acknowledgment, the stamp will reset something...
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-08-05 19:44
    Back in post 6 I stated I need to reset the BS2 (STAMP) in the software. If this is not doable then I will try another route. I do not want to mess with the C# code. It works fine.
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-08-05 23:40
    All a reset does is make the Stamp start back at the beginning of the program. Just add a label at the very beginning of the program and use a GOTO, or use one of the Stamp's pins to pull the the reset pin low and force it that way.
  • davejamesdavejames Posts: 4,047
    edited 2012-08-06 12:30
    NWCCTV,

    Connect a resistor (10k) to the Stamp's reset line, and the other end of the resistor to +5V (make sure it's really 5V).
    Dedicate a Stamp pin to use for the operation/purpose.
    Connect the dedicated pin to the Stamp reset pin (maybe through a 220 resistor).
    In the Stamp code, set the dedicated pin to output, default "high".
    Then, when a Stamp reset is desired, programatically pull the dedicated pin low, which will in turn reset the Stamp.

    Lemme know?




    ***edit post...guess I should have read RDL2004's post a bit more carefully!
  • RDL2004RDL2004 Posts: 2,554
    edited 2012-08-06 14:08
    Well, your post was much more detailed :)

    I should also point out that just using a label with GOTO commands isn't going to clear anything already in memory (variables and such), so you would need to deal with that.
  • NWCCTVNWCCTV Posts: 3,629
    edited 2012-08-06 14:29
    So I should create a sub routine to clear the memory and and then use the Goto "main" to start over, correct?
Sign In or Register to comment.