Shop OBEX P1 Docs P2 Docs Learn Events
Modifying Pulsout Duration. Spinning servos 5 times in 10 seconds — Parallax Forums

Modifying Pulsout Duration. Spinning servos 5 times in 10 seconds

hp16hp16 Posts: 11
edited 2013-11-10 22:11 in BASIC Stamp
I'm working on this thing right here. I think I understand it a little bit but I just have trouble with my code, I don't know how to modify it. I'll be posting it here, hopefully someone can explain to me if I'm doing it right or not. I have to write a program that will make the servos spin 5 times in 10 seconds with the push of a button. At the same time I have to set up two LEDs that will flash on and off every time the servo makes one complete revolution. This is what I've come up with so far.


DO
IF(IN3=1) THEN
  FOR COUNTER = 1 TO ??
    FOR COUNTER2 = 1 TO ??
      PULSOUT 12, 777
      PULSOUT 13, 726
      PAUSE 20
    NEXT
      HIGH 11
      HIGH 10
      PAUSE 125
      LOW 11
      LOW 10
      PAUSE 125
  NEXT
ELSE
  ENDIF

LOOP
      
  


Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-11-10 19:33
    You don't want a separate loop for your LED code (I assume they're on pins 10 and 11?). The servos should be pulsed every 20ms so you don't want to have a pause longer than 20ms when the servos are active (and ideally when stopped). You'll want to turn the LEDs on once the counter reaches a certain number and back off at another number. You don't want to wait to pulse the servos while the LED is on.

    I think you have the right idea with counters. You'll have to experiment with the speed numbers to get the servo to move at the desired speed. You should be able to come up with a reasonable guess of how many 20ms are in two seconds which will be your counter2 limiting value. The limit for "counter" should be obvious IMO (how many rotations do you want?).

    So you have two continuous rotation servos and two LEDs right? How long are the LED flashes supposed to be?

    I'd suggest running the servos off a regulated power source. If you run straight off the batteries the speed will change as the batteries lose their charge (this might not be big problem, a lot depends on what kind of batteries you use).
  • hp16hp16 Posts: 11
    edited 2013-11-10 21:02
    never mind my first post. I'm gonna start over again. There's like 3 codes that I have to come up with. The first one asks for me to create a code that will make the boe-bot go forward at a rate of 2 revolutions in 4 seconds. For the counter I had to modify it to read "from 1 to 163" because I'm trying to compensate for the time it takes pulsout, the overhead code, and pause to run this program. Please tell me if my code is correct.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    counter VAR byte
    
    
    Do
    
    
      IF(IN3=1) then
    
    
        FOR counter = 1 TO 163 'will run servos for at a rate of 1 rev every 2 seconds
        DEBUG ? counter
    
    
          PULSOUT 12, 777
          PULSOUT 13, 726
          PAUSE 20
    
    
        next
    
    
      else
        endif
    
    
    loop
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-11-10 21:30
    Do you have a BS2 to test it on? It always pretty easy to see if code "is correct" by seeing if it does what you expect when it's run on a Stamp.

    The one issue I see is the debug statement. I'm not sure how much that will throw off your timing.

    An alternative to reducing the number of loops it to reduce the pause time. It's common to use pauses less than 20ms in order to compensate for code overhead.

    I don't know if you plan to add something after "else" or not but if you're not, then you can probably just delete it.
  • hp16hp16 Posts: 11
    edited 2013-11-10 22:11
    I have no way to test it right now. How much time would I have to compensate for debug? about 1.3 ms?
    If I reduce the pause from 20 ms to 10 ms thus requiring the for counter to be from 1 to 274, I'll need to declare a counter variable in Word instead of Byte. I think 20 ms is fine for now.
    The Else is there so that after it completes the for counter next loop it's just gonna go through Do Loop forever, waiting to see if I press the button again.
Sign In or Register to comment.