Shop OBEX P1 Docs P2 Docs Learn Events
Bs2 help, led & servo!! — Parallax Forums

Bs2 help, led & servo!!

derekj032derekj032 Posts: 7
edited 2010-12-14 16:45 in BASIC Stamp
I am trying to fix my code so that I can have my servo spin 180 degree counter and also 180 clockwise along with a blinking LED Christmas tree. I can't seem to figure out what I am doing wrong in my do..loop to run these programs at the same time. My goal is to have my Christmas Tree LED's blinking separate from the indicator light , but constant while my servo program is running.

This is what I have, can someone please point me in the right direction. THANKS


counter VAR Word

PAUSE 1000

DO

HIGH 12



FOR counter = 500 TO 1000 STEP 4
PULSOUT 14, counter
PAUSE 30

NEXT

LOW 12

FOR counter = 1000 TO 500 STEP 4
PULSOUT 14, counter
PAUSE 30
NEXT

LOOP

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-13 20:47
    This code should toggle pin 12 every 16th time through each of the FOR..NEXT loops, I think it would make it flash about 37 times a minute.
    counter   VAR   Word
    counter2 VAR   NIB
    
    PAUSE 1000
    
    DO
    
     FOR counter = 500 TO 1000  STEP 4  ' 125 iterations or about 2.5 seconds
       PULSOUT 14, counter
       PAUSE 20
       IF counter2 = 0 THEN TOGGLE 12
       counter2 = counter2 + 1
     NEXT
    
     FOR counter = 1000 TO 500   STEP 4
       PULSOUT 14, counter
       PAUSE 20
       IF counter2 = 0 THEN TOGGLE 12
       counter2 = counter2 + 1
     NEXT
    
    LOOP
    

    Rich H
  • derekj032derekj032 Posts: 7
    edited 2010-12-13 21:09
    What has taken me the last 3 weeks or more to figure out has taken you 5 minutes!! Thanks

    If my tree has neg and pos wires why doesn't it light up when I plug it in place of the LED in p12?
  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-13 21:28
    The pins on a Basic Stamp can only supply a very small amount of current. It's enough for an LED but if you have a whole string of them you will need to use something else to switch the power. If it is a small tree with not very many LEDs you could use a transistor hooked up to Pin 12 (or any IO Pin) to switch the power.

    More details are needed in order to make a good recommendation.

    Don't try hooking up the tree directly to the stamp, you could burn out the pin - if you haven't already.

    Rich H
  • derekj032derekj032 Posts: 7
    edited 2010-12-13 22:05
    http://www.vellemanusa.com/us/enu/product/view/?id=351133

    This is the tree I am using but I am only using half, the half with the power supply connection. Instead of using the battery supply there is a + - place on the top of the tree.
  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-13 22:59
    That's very helpful. That tree draws very little current but it looks like it needs 9 volts. The BS2 can only supply 5 volts.

    All you need is an NPN transistor and a 1K ohm resistor. The pin (12?) on the BS2 would have the resistor between it and the Base of the transistor. The Source of the transistor would be connected to the negative (-) of the tree and the Drain of the transistor would connect to ground. The positive (+) of the tree would go to the 9 volt battery and the negative (-) of the 9 volt battery needs to be connected to the same ground as the transistor and BS2.

    When the pin goes high on the BS2 current will flow through the transistor which will turn it "on" and complete the circuit for the tree.

    Rich H
  • derekj032derekj032 Posts: 7
    edited 2010-12-14 11:35
    Okay I am hooking this up now. When I was reading through my book it mentions something about the Vin able to produce voltage directly from the power source which can be 6-9 volts.
  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-14 12:09
    Vin is "Voltage in" it is the same voltage as the battery or power supply that you are using to power the board. If you are using a 9 volt supply then you could connect the positive (+) of the tree to that. If your power supply is less than 9 volts then you should use a separate battery for the tree.
  • derekj032derekj032 Posts: 7
    edited 2010-12-14 12:17
    I have it hooked up to the 9v but the LED's are staying on.
  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-14 13:00
    If you were to modify the tree like in the attached diagram then you could use the existing transistors to control each set of lights on the tree. The traces need to be cut where the red lines are.

    attachment.php?attachmentid=76339&d=1292361550

    As for the lights staying on, you may want to test the BS2's pin for proper operation by hooking up a test LED and resistor. It may have been damaged earlier. Also, if you could post a picture it would help immensely.

    Rich H
  • bytedude1bytedude1 Posts: 9
    edited 2010-12-14 14:58
    You need longer pause times.
  • derekj032derekj032 Posts: 7
    edited 2010-12-14 15:18
    Picture 016.jpg
    Picture 006.jpg
    Picture 001.jpg
    Picture 004.jpg
    Picture 005.jpg
    Picture 013.jpg
    I hope this helps to see where I'm at.
    1024 x 768 - 125K
    1024 x 768 - 141K
    1024 x 768 - 137K
    1024 x 768 - 70K
    1024 x 768 - 147K
    1024 x 768 - 115K
  • derekj032derekj032 Posts: 7
    edited 2010-12-14 15:31
    I got it to work. I put my original LED (that I was using before actually hooking up the tree) back in place jumped it to the neg of the tree, removed the indicator light and it works great!

    I will finish putting the tree on the rotating servo and post it working!

    Many Thanks for your help!!!
  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-14 16:12
    bytedude1 wrote: »
    You need longer pause times.

    Please explain.

    Rich H
  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-14 16:23
    derekj032 wrote: »
    I got it to work. I put my original LED (that I was using before actually hooking up the tree) back in place jumped it to the neg of the tree, removed the indicator light and it works great!

    I'm glad you got it to work, I don't quite understand what you did (indicator light?) but if it is doing what you want then that is what's important. I hope you can post a video when you get it all together.

    Rich H
  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-14 16:31
    You could move your servo connector to the servo header to save room on the breadboard.

    attachment.php?attachmentid=76348&d=1292373123

    Pin 14 is the second row.

    Rich H
  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-14 16:45
    Is this the indicator light you were referring to?

    attachment.php?attachmentid=76349&d=1292373408

    It looks to be connected to the same pin as the servo. A servo pulse is only 1.5 milliseconds long on average, not enough to light up the LED. Or is it? I haven't tried. At any rate, if it was visible it would be very dim and I can't see how it would be used as an indicator.

    Rich H
Sign In or Register to comment.