Shop OBEX P1 Docs P2 Docs Learn Events
HELP!: I need a simple encoder using my photoresistor and a PBASIC program. — Parallax Forums

HELP!: I need a simple encoder using my photoresistor and a PBASIC program.

itsme_melbitsme_melb Posts: 5
edited 2004-12-09 00:06 in BASIC Stamp
I need Help with this project.....any help would be great I am really dumb when it comes to coding...

Use your photoresistor and a PBASIC program to make a simple encoder. An encoder is a device that reads spokes on a wheel as they pas by. Use your photoresistor as the sensor that reads the spokes and use your hand as the spokes. By waving your hand past the photoresistor, you can emulate spokes passing by the sensor. Using the DEBUG terminal to display how many times you pass youyr hand over the photoresistor. With some calibration, you can also hold your fingers wide apart and pass them over the photoresistor to emulate spokes.

Comments

  • steve_bsteve_b Posts: 1,563
    edited 2004-12-06 18:47
    melb,

    since I'm sure you have Pbasic, look up the COUNT command.

    This should work well enough for you.· As for hooking up the circuit....I quickly edited this image (robbed it from an optoisolator setup--works the same).



    No doubt there are other ways to set this up....someone else may have a better circuit too.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·

    Steve
    http://members.rogers.com/steve.brady
    http://www.geocities.com/paulsopenstage

    "Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."

    600 x 600 - 11K
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-12-06 22:15
    If I may make a suggestion...I don't believe a PhotoResistor will work for detecting spokes on a wheel that is spinning...PhotoResistors are rather slow devices, electrically speaking, and wouldn't be able to count at any reasonable speed.

    I did notice that steve_b posted a schematic using an phototransistor and LED.· This is the more appropriate way to go.· The transistor can switch alot faster, and will often have a smaller aperture for the light to pass through, so with an adjacent LED, it will more accurately detect the spokes.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs
    ·
  • steve_bsteve_b Posts: 1,563
    edited 2004-12-06 23:20
    Good point Chris.

    Didn't pay enough attention to that.



    Also, to melb....VCC in my schematic is 5Volts.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·

    Steve
    http://members.rogers.com/steve.brady
    http://www.geocities.com/paulsopenstage

    "Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-12-07 01:57
    Steve,

    ·· Sub-conciously you must've thought about it, otherwise you'd have posted a schematic of what he was looking for, instead of what he should be using.· So you were on some level thinking about it...wink.gif

    Did any of this help the original poster?



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs
    ·
  • steve_bsteve_b Posts: 1,563
    edited 2004-12-07 02:05
    I do try to do most of my thinking conciously...but some comes out Sub-conciously where my best ideas are all unconsciously born!



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·

    Steve
    http://members.rogers.com/steve.brady
    http://www.geocities.com/paulsopenstage

    "Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."

  • edited 2004-12-07 04:31
    I read the post and thought to myself, "gee, that looks like something I'd say...".· Sure enough, it turned out to be a Project from What's a Microcontroller v2.1.·

    It's true that the IR LED/Transistor combo is the most common optical encoding method.· It's reliable and can work at much higher speeds and resolutions than photoresistors.· However, I couldn't include it in What's a Microcontroller because it was already covered in Industrial Control.· The most I could do was include it as an optional project at the end of the photoresistor chapter.· My reasoning behind this project was that writing a PBASIC program to track the number of black/white, white/black,... transitions is·still a valuable lesson.

    Even thought the most common use for photoresistors seems to be rough ambient light measurements, they actually do work pretty well as encoders at lower speeds and resolutions.· With fenders to block out the light and LEDs to illuminate the stripes, they're alright.· A pair of them can be used for quadrature, and with RCTIME measurements, a software Schmidt trigger can also be implemented.··Fun!

    I don't want to derail any instructors by posting the solution, but here are some hints.·
    • Start with TestPhotoresistor.bs2.·
    • Run the program and record the time variable's value when you put the photoresistor close to white paper.
    • Use a magic marker to draw a 1-inch wide black line on part of the white paper.
    • Place the black line in front of the photoresistor, and record the time variable again.·

    Now that you know the light and dark measurements, you can modify TestPhotoresistors.bs2 to count the black/white, white/black transitions.
    • First, you can use an IF...THEN statement to examine the time variable and determine whether you are looking at black or white.·
    • Once you can make your debug terminal tell you whether you are looking at black or white, it's time to start writing the code for encoding.
    • Start by declaring three more variables.· Declare one named counter, it can either by a Byte or a Word.· Also declare two Bit variables, stateNow and stateOld.
    • Modify your IF...THEN statement to make stateNow 1 when the photoresistor sees black and 0 when the photoresistor sees white.
    • Add a second IF...THEN statement that adds 1 to the counter variable whenever stateNow is not equal to stateOld.· This second IF...THEN statement will·also have to be a code block that also sets stateOld equal to stateNow.· (In the first draft, these instructions incorrectly said to set stateOld equal to counter.)· This way, the program is ready to detect the next transition.· By examining some of the other example programs in the·earlier chapters, you should have enough to finish the project.· Good luck!

    Post Edited (Andy Lindsay) : 12/8/2004 7:39:08 PM GMT
  • itsme_melbitsme_melb Posts: 5
    edited 2004-12-08 16:38
    Thank you for the help Andy....

    DO

    HIGH 2
    PAUSE 100
    RCTIME 2, 1, time
    DEBUG HOME, "time = ", DEC5 time

    IF time <= 12 THEN
    stateNow = 0
    ELSEIF time => 30 THEN
    stateNow = 1
    ENDIF

    IF stateNow <> stateOld THEN
    counter = counter + 1
    DEBUG CR, "Spoke Count·= ", DEC counter
    stateOld = counter
    ENDIF

    LOOP

    I have now clue to what I am doing...but I am trying....

    it still doesn't work right....when I pass my hand over the photoresistor it adds 1 to the counter and when my hand·isn't over the photoresistor it still adds 1 to the counter....what am I doing wrong?...also I don't think my IF then statements are correct....

    Post Edited (itsme_melb) : 12/8/2004 4:51:38 PM GMT
  • edited 2004-12-08 19:18
    Nicely done. I think if you change

    stateOld = counter

    to

    stateOld = stateNow

    it should start to work better.

    My instructions incorrectly told you to set stateOld equal to counter, stateOld should instead be set equal to stateNow.· Sorry.· I will edit those instructions so that they do not lead others astray.

    The program·should add 1 to the counter when you place your hand in front of it, and add another 1 when you remove your hand, then another 1 when you place your hand in front of it again, etc...

    The·PAUSE 100 is in there so that slower computer's don't suffer from serial buffer overload.· When you are using it in an application that's counting stipes moving by at faster rates, reduce the PAUSE 100 TO PAUSE 1, and comment out the debug commands.

    For·motor speeds, the Industrial Control text has a better design that uses·infrared parts and the COUNT command.· Industrial Control is available from the www.parallax.com -> Downloads -> Stamps in Class Tutorials page.

    Post Edited (Andy Lindsay) : 12/8/2004 7:51:50 PM GMT
  • itsme_melbitsme_melb Posts: 5
    edited 2004-12-08 21:06
    Thanks alot for the help with the last project...I definitely needed guidance.

    I have another question...How can I use a potentiometer circuit to control the tempo of this program. Any suggestions would be great....

    Here is the code for the program so far:


    Notes DATA "C", "C", "G", "G", "A", "A", "G"

    Frequencies DATA Word 2093, Word 2093, Word 3136, Word 3136,
    Word 3520, Word 3520, Word 3136

    Durations DATA Word 500, Word 500, Word 500, Word 500,
    Word 500, Word 500, Word 1000

    index VAR Nib
    noteLetter VAR Byte
    noteFreq VAR Word
    noteDuration VAR Word

    DEBUG "Note Duration Frequency", CR,
    "----

    ", CR

    FOR index = 0 TO 6

    READ Notes + index, noteLetter
    DEBUG " ", noteLetter

    READ Durations + (index *2), Word noteDuration
    DEBUG " ", DEC4 noteDuration

    READ Frequencies + (index * 2), Word noteFreq
    DEBUG " ", DEC4 noteFreq, CR

    FREQOUT 9, noteDuration, noteFreq

    NEXT

    END
  • edited 2004-12-09 00:06
    Check the Stamps in Class posts; there were some discussions on that topic a few months back. There's also a search·button you can use to potentially find this information if the title of the post doesn't make it obvious.

    Also, it would be better to post these questions to the Stamps in Class Forum.· It is intended for questions like these, to help students collaborate on solutions.·

    Last but not least, this subject is not encoder related, so it would be best to start a new thread in Stamps in Class.

    Post Edited (Andy Lindsay) : 12/9/2004 12:41:25 AM GMT
Sign In or Register to comment.