Menu

Servo code not working?

Help
2007-10-09
2013-05-30
  • Nobody/Anonymous

    Hello!
    I am trying to get a servo to change it's direction according to button inputs.

    I would like the servo to move a bit to the left if the left button is pressed, and keep doing so every time the event occurs. I'd like it to do the same for when the right button is pressed, as well.

    Here is the code I've tried; Perhaps I am overlooking something.
    Thank you very much!
    -Omar

    -----------------------------------------
    #chip 16F628A, 4
    #config INTOSC, WDT = OFF

    'define the ports
    DIR PORTB.3 OUT    'pan servo pin
    DIR PORTB.2 IN    'move left input
    DIR PORTB.1 IN    'move right input

    'dimension variable for pulse length
    DIM PAN AS WORD

    'set everything off
    PAN = 150 'set the PAN value to be servo neutral value

    SET PORTB.3 OFF
    SET PORTB.2 OFF
    SET PORTB.1 OFF

    START:
        'wait until either input is high, then modify the PAN value accordingly and start pulsing
        IF PORTB.2 ON THEN
            'turning to the left
            'making sure PAN value is low enough
            IF PAN >= 110 THEN
                PAN = PAN-10
            END IF
            'go to the pulse sub
            servoLEFTone
        END IF
        IF PORTB.1 ON THEN
            'turning to the right
            'making sure PAN value is high enough
            IF PAN <= 190 THEN
                PAN = PAN+10
            END IF
            'go to the pulse sub
            servoLEFTone
        END IF
    goto START

    sub servoLEFTone
        'do while inputs are not high
        DO WHILE PORTB.1 OFF AND PORTB.2 OFF
            'code for servo pulse
            Wait 20 ms
            SET PORTB.3 ON
            'keep portb.3 high for the pan value times 10 us
            Wait PAN 10us
            SET PORTB.3 OFF
            Wait 4 ms
        LOOP
    end sub
    -----------------------------------------

     
    • Hugh Considine

      Hugh Considine - 2007-10-10

      GCBASIC is having trouble with this line:

      DO WHILE PORTB.1 OFF AND PORTB.2 OFF

      At the moment, support for bit operations is very limited and it's not possible to combine bit tests using And/Or. I will try to do something about this in the next couple of days.

      In the meantime a workaround is to create some functions to test the bit, like this:

      DO WHILE Button1Released AND Button2Released

      ...

      Function Button1Released
      Button1Released = False
      If PORTB.1 OFF Then Button1Released = True
      End Function

      Function Button2Released
      Button2Released = False
      If PORTB.2 OFF Then Button2Released = True
      End Function

       
      • Nobody/Anonymous

        Thank you very much for your help.
        However, I changed the code like you stated and programmed the chip-- it still did not work :(.

        Do you think the variable is not being updated / the loop being run with that variable value?
        I am so stumped. I tried moving different parts of the code around hoping that it would work.  I've noticed that when I initially power up the circuit, when I press a button the servo actually moves to a certain direction (depending upon which button is pressed). After that, I am not able to get it to move again (it just stays there).

        Omar

         
    • Nobody/Anonymous

      Pheuf.
      After much work, I think I have gotten the code to work.
      It seemed that GCB was having troubles with that 'AND' comparison-- even when I defined two functions. I tried many possible methods of getting the code to work; it seems the easiest and the most overlooked method was the one that wanted to work!

      Thanks for your help! Here's the code, free for anyone to play around with and to use:
      --------------------------------
      'Servo Test: rotates servo (in set motions) towards direction -- left or right (depending upon button press)
      #chip 16F628A, 4
      #config INTOSC

      'define the ports
      DIR PORTB.3 OUT 'servo output pin
      DIR PORTB.4 IN 'move left button
      DIR PORTB.5 IN 'move right button

      PAN = 150 '150 corresponds to 150 ms -- meaning servo will be in center position upon startup
      'set all ports to be off
      SET PORTB.3 OFF
      SET PORTB.4 OFF
      SET PORTB.5 OFF

      START:
          'start pulsing to the servo
          Wait 20 ms
          SET PORTB.3 ON
          'wait value corresponds to PAN value
          Wait PAN 10us
          SET PORTB.3 OFF
          Wait 4 ms
         
          'manipulate the variable PAN according to button press
          'right button pressed
          IF PORTB.5 ON THEN
              'this wait value can be changed around.
              WAIT 125 10ms
              IF PAN <= 180 THEN
                  'if possible to add (so pulse does not exceed possible servo range), then add to the pan value
                  PAN = PAN+10
              END IF
          END IF
         
          IF PORTB.4 ON THEN
              'this wait value can be changed around.
              WAIT 125 10ms
              IF PAN >= 110 THEN
                  'if possible to subtract (so pulse does not exceed possible servo range), then add to the pan value
                  PAN = PAN-10
              END IF
          END IF
          'back to start and pulse new value
      goto START

      --------------------------------

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.