Shop OBEX P1 Docs P2 Docs Learn Events
Oscillate led's at different frequencies — Parallax Forums

Oscillate led's at different frequencies

replaysMikereplaysMike Posts: 3
edited 2007-05-11 18:21 in BASIC Stamp
I'm pretty new to pbasic - does anyone know an easy way to oscillate 4 led's at seperate frequencies? (BS2)

This should be trivial, but I can't wrap my head around a simple way to do it. Tried pulsout but it doesnt seem to work when I do it on multiple pins - maybe my code is wrong.

I'd like to turn 4 led's on and off, each at 30 Hz, 25 Hz, 20 Hz, and 15 Hz.

Thanks!

Comments

  • replaysMikereplaysMike Posts: 3
    edited 2007-05-11 03:07
    DO
    IF cnt // 2 = 0 THEN TOGGLE 0
    IF cnt // 3 = 0 THEN TOGGLE 1
    IF cnt // 6 = 0 THEN TOGGLE 2
    IF cnt // 10 = 0 THEN TOGGLE 3
    cnt = cnt + 1
    PAUSE 5
    IF cnt >= 1000 THEN cnt=0
    LOOP

    This is passable but not very accurate. Anyone got a better idea?
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-05-11 04:49
    Mike -

    I'm not sure exactly what you're trying to accomplish, but please keep the following in mind. The PBASIC Stamp is a single tasking device, meaning it can only do one thing at a time. You may have to use a PWM co-processor to accomplish what you are trying to do.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-05-11 05:42
    The ratio of frequencies is 6:5:4:3, and the least common multiple of those is 60. 6*10, 5*12, 4*15, 3*20 So the period ratios is 10:12:15:20.

    cnt VAR byte
    DIRA = $f
    DO
      cnt = cnt + 1 // 60
      outa = (cnt // 10 / 5 << 3) + (cnt // 12 / 6 << 2) + (cnt // 15 / 8 << 1) + (cnt // 20 / 10)
      ' delay here to set main frequency
    LOOP
    



    The statement for outa puts all 4 bits right on the port that has the LEDs at the same time. It avoids IF-THEN constructs, which can jitter the overall timing of the loop.
    I'm not sure it will run fast enough on a BS2 to get 30 hertz. It will be close. The output for the period 15 is a little asymmetric due to the odd number.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-05-11 05:43
    Mike,

    You're on the right track. The first thing you have to do is compute the least common multiple of the four frequencies (doubled) that you're trying to output (i.e. 60, 50, 40, and 30). I come up with 600 Hz for that. This means that your DO loop will have to execute 600 times per second to produce your four frequencies accurately. Considering that the BS2 executes 4000 instructions per second, this will almost certainly be a show stopper; but let's press ahead anyway. Now, divide 600 by each doubled frequency to get 10, 12, 15, and 20. These are the moduli (i.e. periods, or numbers after the //)) relative to the overall loop rate for each of your frequencies. Now you can do what you did above, except using 600 instead of 1000 as your terminal count. I'd leave out the PAUSE for now, since you probably won't reach the rate you need using this method.

    But even if you don't attain the top-end speed you're looking for, the relative frequencies of the four outputs will be correct.

    -Phil

    Update: Tracy, you slipped under me with a much better solution!
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-05-11 16:38
    Where else could two people come up with LCMs synergistically within minutes of one another? I was already thinking along those lines yesterday, programming a controller for solenoid valves that will be sampling for ozone on different schedules. A much lower frequency of course, period of minutes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • replaysMikereplaysMike Posts: 3
    edited 2007-05-11 18:21
    Heheh Bruce - yea I know I come from a programming background, so I'm trying to find a way to do this in a single-tasking manner. My brain wasn't fully working and I knew I had to find the common multiple in the four frequencies I wanted, but wondered if there was a way within PBasic and the stamp itself to accomplish this.

    Thanks guys for the responses, that really does help. My other option was to use a CD4060 IC which can oscillate up to 8 led's all at different frequencies - the problem only being they are each a 2:1 ratio, so if my fastest led can only blink at 30Hz, then the next is 15Hz, 7.5Hz, etc. Would work ok because I only need 4 led's to blink, any more and it probably wouldnt work for me.

    I'm using this with IR led's and a video camera, so I can distinguish which LED is which (#1, #2, #3, #4 etc) in the video. It's a small computer vision project for determining 3D geometry. I only have 30 frames per second to work with so 30Hz is the fastest I can use - actually its probably more like 25Hz because I need to distinguish it from always on led's.

    Thanks guys!
Sign In or Register to comment.