Shop OBEX P1 Docs P2 Docs Learn Events
Help reading simple four quadrant encoder disk with BS2 — Parallax Forums

Help reading simple four quadrant encoder disk with BS2

WHallWHall Posts: 22
edited 2010-04-20 21:58 in Accessories
I could use some help. I've created a simple four quadrant encoder disk (attached) that I can use with the QTI IR sensor to debug 1's and 0's. Circuitry is good, and manually moving the disk past the sensor works perfectly. But I'd like to take the bit read from the encoder to pulse a servo one quarter turn (which is why the encoder disk is so simple), then stop and wait for the next command to make a quarter turn, and so on.

My thought is to monitor the bit and tell the servo to stop turning when the bit changes from a "1" to a "0" (or vice versa). Conceptually, it seems simple (isn't that always the case!), but my lack of coding experience is killing me. I could use some help with the section below. What am I doing wrong? Do I need a write/read command? Any help for a newbie would be most appreciated!

Motor PIN 12

'
[noparse][[/noparse] Constants ]

Motorstop CON 750

'
[noparse][[/noparse] Variables ]

qti VAR BIT
old VAR BIT

DO
HIGH 5
PAUSE 1
qti = IN3
INPUT 5
qti = old
PULSOUT motor, 850
IF (qti <> old) THEN
PULSOUT motor, motorstop
ENDIF
LOOP
400 x 400 - 12K

Comments

  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-04-19 22:21
    Look at this post to get·some ·idea.gif·s on how to do this

    http://forums.parallax.com/showthread.php?p=893416

    I hope this helps

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·Now wanting to learn Spin· Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • ZootZoot Posts: 2,227
    edited 2010-04-20 18:31
    Your logic is bad. Plus you'll need a state to keep track of what you want the servo to "wait" for (a change in encoder edge, or a command to start again).

    state = 0  ' 0 = moving and waiting for encoder edge; 1 = waiting for "command" to start again
    
    DO 
    
      old = qti ' save the last reading BEFORE doing new reading    
      HIGH 5    ' charge cap
      PAUSE 1  ' wait
      qti = IN3 ' read it
      INPUT 5 ' make an input
    
      IF state = 0 THEN  ' moving waiting for edge
          PULSOUT motor, 850 
          IF (qti <> old) THEN     ' must be new edge
             state = 1
          ENDIF
      ENDIF
    
      IF state = 1 THEN  ' stopped, waiting for "command" -- not sure where command is coming from? so let's say a simple button press
         PULSOUT motor, motorstop 
         IF buttonIn = 1 THEN
           state = 0
         ENDIF
      ENDIF
    
    LOOP
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php


    Post Edited (Zoot) : 4/20/2010 10:05:02 PM GMT
  • WHallWHall Posts: 22
    edited 2010-04-20 21:58
    Zoot, "Your logic is bad." If you only knew smile.gif Many thanks for helping with the code! I've been really struggling with it - I'll your example and report back on how it worked.
Sign In or Register to comment.