Shop OBEX P1 Docs P2 Docs Learn Events
Voting Circuit on a BS2 from photocell outputs — Parallax Forums

Voting Circuit on a BS2 from photocell outputs

TheForsytheTheForsythe Posts: 12
edited 2012-03-27 13:23 in BASIC Stamp
I'm pretty new to the basic stamp. I really need a hand with the creating a voting circuit on the basic stamp. There are 13 photocells that i will have hooked up with comparators on a separate bread board. The output (high or low) will be put into the basic stamp board. When any 10 or more of the 13 photocells are high, i need to get a high signal. When less than 10 of the photocells are high, i need to receive a low signal. If anyone can help me it would be greatly appreciated. Thank You!

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-20 17:06
    TheForsythe,

    Welcome to the forum!

    The way I would go about this is to read all of the inputs at once using the INS operator, and assign it to a word variable. Then mask off the three unused bits. Next, isolate each 4-bit nybble and use that as an argument to a GOSUB routine which looks up the one-bit count from a table in EEPROM. Do that four times, adding the result to the total, and you'll have your count.

    The slower (but simpler) way to do it would be to take the variable to which INS was assigned, and check each bit one-by-one by shifting right and using the .BIT0 attribute to add to your count.

    -Phil
  • ercoerco Posts: 20,256
    edited 2012-03-21 09:22
    The Stamp's TTL pins work very well as comparators. Using them as input pins, a voltage below ~1.4V will read low and above 1.4V reads high.
    In lieu of building a comparator board, you could just use your photocells (phototransisitors are better) in a simple voltage divider arrangement. One series resistor (properly selected to yield working voltages) and one photocell/phototransistor per channel and you're finished. Leave the LM339s to fight another battle another day.
  • TheForsytheTheForsythe Posts: 12
    edited 2012-03-22 17:59
    That sounds really great. Now I have no need to build comparators which will save me some time. I was wondering if anyone could give me a smaple program of what it might look like if i used P0 - P12 for my inputs. If someone could help me get started with the program that would be really great! Thanks!
  • TheForsytheTheForsythe Posts: 12
    edited 2012-03-24 07:30
    Could anyone post a program of this for me? It would be such a big help.
  • ercoerco Posts: 20,256
    edited 2012-03-24 08:28
    What do you have so far?
  • TheForsytheTheForsythe Posts: 12
    edited 2012-03-24 13:47
    Not much. Ive tried a bunch of things but i can't seem to get close to a solution with anything.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-24 14:22
    Ive tried a bunch of things ...
    Start by posting one or two of the things you've tried. We're not going to do the work for you, but we're always willing to help folks find their own way if they put some of their own effort into it.

    -Phil
  • TheForsytheTheForsythe Posts: 12
    edited 2012-03-25 12:13
    I have absolutely no idea what I'm doing. I don't know the first thing about the Basic Stamp
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-25 12:23
    In that case, you need to start from the beginning. Here's a jump-off point for several excellent and freely-downloadable texts that will bring you up to speed:

    -Phil
  • TheForsytheTheForsythe Posts: 12
    edited 2012-03-25 12:54
    Thanks! I need to try and have this done by Friday. With absolutely no knowledge of the Basic Stamp do you think that is possible?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-25 13:00
    Sure. But get busy reading and doing the experiments. Time's awastin'! If you have specific questions, you can post them here.

    -Phil
  • ercoerco Posts: 20,256
    edited 2012-03-25 15:04
    Time's awastin'!

    +1.

    The time you saved not building comparators is well invested into learning about the Stamp.
  • TheForsytheTheForsythe Posts: 12
    edited 2012-03-27 12:10
    Okay. So i have made a ton of progress. I just can't get my program to hold either the 1 or 0 coming out of OUT15 and then changing when it gets a 1 from IN13. Here is my program:



    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    DEBUG "Elevator Capacity Check!", CR


    LightCount VAR Byte


    DO


    'DEBUG CLS


    LightCount = 0 ' Initialize counter to 0


    IF IN13 = 0 THEN Hold 'Wait for elevator door to close
    Counter:
    PAUSE 2000 'Pause for 2 seconds while people get settled


    IF IN0 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN1 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN2 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN3 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN4 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN5 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN6 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN7 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN8 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN9 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN10 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN11 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it
    IF IN12 = 0 THEN LightCount = LightCount + 1 ' PhotoCell on, count it


    DEBUG DEC LightCount, CR ' Output number of photocells that are not seeing light


    IF LightCount > 9 THEN OUT15 = 1 ELSE OUT15 = 0 ' Turn on LED when 10 or more photocells see light
    Hold:
    IF OUT15 = 1 THEN hold UNTIL IN13 = 1
    IF OUT15 = 0 THEN Hold
    IF IN13 = 0 THEN Hold
    LOOP



    If anyone could help me it would be greatly appreciated!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-27 12:31
    Yes, you have made a lot of progress. Congrats! I'm not exactly sure what IN13 is supposed to do, but I'm guessing that it's a reset for OUT15. In that case, try this, in lieu of your "Hold" block:
    DO : LOOP UNTIL IN13 = 1
    OUT15 = 0
    DO : LOOP WHILE IN13 = 1
    

    This will wait for IN13 to go high, turn off OUT15, then wait for IN13 to go back low. That last part is important if your whole program is in a loop, in case the reset button is held down too long.

    -Phil
  • TheForsytheTheForsythe Posts: 12
    edited 2012-03-27 13:21
    IN13 is a pushbutton switch. I need the program to hold OUT15 as high or low until the pushbutton is pressed. When it is pressed i need the program to go back to the beginning.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-27 13:23
    Okay, that's what I thought. Assuming it produces a "high" when pressed, what I suggested will work. But you need to put your entire program in a huge DO .. LOOP. You might also need to add some debouncing. A short PAUSE at the end would probably be enough.

    -Phil
Sign In or Register to comment.