Shop OBEX P1 Docs P2 Docs Learn Events
need help on controlling servo — Parallax Forums

need help on controlling servo

apacheapache Posts: 7
edited 2007-03-11 17:27 in BASIC Stamp
first of all sorry for making multiple postings. i have worked on my project and got to a point. my code has become

DO
PULSOUT 15, 5
PULSIN 15, 1, time
DEBUG HOME
time = time ** 2251
DEBUG CR, "Distance = ", DEC4 time, " cm"
PAUSE 100
LOOP
IF (time < 880) THEN
PULSOUT 14, 1000
ENDIF
IF (time > 900) THEN
PULSOUT 14, 500
ENDIF

what i am trying to do is from the datas which is variable time i need to control a servo. i tried to write down a code from tutorials but it didnt work.· i need to reset my servo which means stay in the middle before starting or inaother meanings between time 880 and 900. when the time increases i need my servo to turn left or right. but i couldnt do it. i also need help on changing my variables. i dont want to talk with time in my program. i need to wirte it an when distance is greater than something. if someone can help me on these i will be very happy.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-11 15:14
    1) Please look at the example programs (www.parallax.com/dl/src/bs/BSExCode.zip). They show how to convert the echo time produced by the PING into a value in cm. Instead of "time = time ** 2251", you would do
    time = time */ $200
    time = time / 2
    time = time ** 2257
    


    2) Servos require that the control pulse be repeated about every 20ms. If the servo doesn't get a control pulse every 20ms, it will turn off the power to the motor. If you look at the Roaming with the PING program or the similar IR-based program in the Robotics tutorial, you'll see that the program always puts out the servo pulses in some kind of loop. Sometimes it puts out a sequence of 8 pulses, sometimes it puts out 20 to 40 pulses. Usually, the servo will finish moving within about 1/2 second to 1 second ... that's why the 20 - 40 pulses. Because of the gears in the servo and the friction and the drag of the motor, the servo will usually hold its position if there's not much force on it (try turning it when there's no power). Try putting your servo PULSOUT statements in FOR statements like:
    if time < 2
      for i = 1 to 20
        PULSOUT 14,1000
      next
    endif
    
  • apacheapache Posts: 7
    edited 2007-03-11 17:20
    what is the "i" mean in this one i couldnt understand it. and will i put this code after the loop i have or into the loop
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-11 17:27
    Sorry. Good question. Yes, you need to move the LOOP to the end. Everything between the DO and the LOOP is repeated.

    The "i" is simply a loop counter. Define it as "i VAR byte".
Sign In or Register to comment.