Shop OBEX P1 Docs P2 Docs Learn Events
Multiple DO.......LOOP running at once — Parallax Forums

Multiple DO.......LOOP running at once

nrsmacnrsmac Posts: 42
edited 2011-12-19 03:29 in BASIC Stamp
Hi, I'm not great at programming with the bs2, So I was wondering. Do you know a way ro run multiple DO......LOOP commands at once? That would be great. Thanks!


nrsmac

Comments

  • FranklinFranklin Posts: 4,747
    edited 2011-12-10 12:47
    A bit more information on what you wish to accomplish would help but the stamp can only do one thing at a time so taking your question literally the answer is no.
  • $WMc%$WMc% Posts: 1,884
    edited 2011-12-10 13:30
    While the Stamp can only do one thing at a time.It runs pretty fast,So you can get the effect of multitasking.This is much like the way your PC has this effect.
    '
    You have some tunes playing,Stamp editor open,Debug window open,and the Parallax forums open.This all seems to be running at the same time in parallel,But it's not.
    '
    If you write clever code with the BS2 you can get similar results.
    '
    This is where some good reading comes into play.
    '
    http://www.parallax.com/Portals/0/Downloads/docs/books/sw/Web-SW-v2.1.pdf
    http://www.parallax.com/Portals/0/Downloads/docs/prod/stamps/web-BSM-v2.2.pdf
  • nrsmacnrsmac Posts: 42
    edited 2011-12-11 00:07
    I am trying to get some FREQOUT piezo commands along with some flashing LEDs.
  • ercoerco Posts: 20,256
    edited 2011-12-11 00:10
    Nested DO loops and nested FOR/NEXT loops work fine. That is, one loop running inside another loop.
  • SfrobotSfrobot Posts: 12
    edited 2011-12-11 05:14
    try put them in the same do..loop without huge pauses and they will work together, time between those 2 commands cant be realised!
  • nrsmacnrsmac Posts: 42
    edited 2011-12-11 08:42
    Thankyou guys for all of these suggestions. One small prob though.

    When I nest the blinking LED loop in with the FREQOUT command loop, no more code can run while the nested loop is running. The LEDs wil blink, but no sound will play.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-12-11 08:58
    You can't do the FREQOUT while you're doing something else. The reason the LED blinking works is that, once you set an I/O pin to some state, it stays that way until it's changed. You can do a FREQOUT after changing the LED's state. When the FREQOUT is done, you can change the LED's state again. It's like the FREQOUT works similar to a PAUSE. The Stamp can't do anything else until the specified time is up. When the FREQOUT is over, the sound stops until the next FREQOUT. If you keep the silent periods very short, you won't particularly notice them.
  • $WMc%$WMc% Posts: 1,884
    edited 2011-12-11 14:42
    nrsmac
    '
    Did you get the BS2/code working?
  • nrsmacnrsmac Posts: 42
    edited 2011-12-15 12:14
    No, I didn't. I can't get the LEDs to blink while one long FREQOUT command. Apparently it is imposible.
  • $WMc%$WMc% Posts: 1,884
    edited 2011-12-16 22:26
    If you'll post some code...It might help your post.
    '
    "Cheer up o' chap"....(Lame USA attempt to cheer up a Britt (UK))...sorry
    '
    There are other ways to reach the same goal. Often using different commands.
    '
    Ditch the Impossible out look...Think positive
    '
    Post Your code and we'll see what we can do with it.
  • nrsmacnrsmac Posts: 42
    edited 2011-12-17 03:30
    lol, im actually an american who is living in the UK. I agree with you, it is a lame attempt to cheer up a britt (I've tried it). It is very hard to cheer one of them up. Especially if the are from london (sorry british people). British people are awesome though. Ok, here is my code.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' Deck the halls with boughs of holly
    Notes DATA "C", "b", "A", "G", "F", "G", "A", "F",
    ' Fa la la la la la la la la
    "G", "A", "b", "G", "A", "G", "F", "E", "F",
    ' Tis the season to be jolly
    "C", "b", "A", "G", "F", "G", "A", "F",
    ' Fa la la la la la la la la
    "G", "A", "b", "G", "A", "G", "F", "E", "F",
    ' Don we now out gay apparel
    "G", "A", "b", "G", "A", "b", "C", "G",
    ' Fa la la la la la la la la
    "A", "B", "C", "D", "E", "F", "E", "D", "C",
    ' Toll the ancient yuletide carol
    "C", "b", "A", "G", "F", "G", "A", "F",
    ' Fa la la la la la la la la
    "D", "D", "D", "D", "C", "b", "A", "G", "F", "Q"

    Octaves DATA 7, 6, 6, 6, 6, 6, 6, 6,
    6, 6, 6, 6, 6, 6, 6, 6, 6,
    7, 6, 6, 6, 6, 6, 6, 6,
    6, 6, 6, 6, 6, 6, 6, 6, 6,
    6, 6, 6, 6, 6, 6, 7, 6,
    6, 6, 7, 7, 7, 7, 7, 7, 7,
    7, 6, 6, 6, 6, 6, 6, 6,
    7, 7, 7, 7, 7, 6, 6, 6, 6

    Durations DATA 4, 8, 4, 4, 4, 4, 4, 4,
    8, 8, 8, 8, 4, 8, 4, 4, 2,
    4, 8, 4, 4, 4, 4, 4, 4,
    8, 8, 8, 8, 4, 8, 4, 4, 2,
    4, 8, 4, 4, 4, 8, 4, 4,
    8, 8, 4, 8, 8, 4, 4, 4, 2,
    4, 8, 4, 4, 4, 4, 4, 4,
    8, 8, 8, 8, 4, 8, 4, 4, 2


    Dots DATA 1, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 1, 0, 0, 0, 0,
    1, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 1, 0, 0, 0, 0,
    1, 0, 0, 0, 1, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    1, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 1, 0, 0, 0, 0

    index VAR BYTE
    offset VAR NIB

    noteLetter VAR BYTE
    noteFreq VAR WORD
    noteDuration VAR WORD
    noteOctave VAR NIB
    noteDot VAR BIT

    DO UNTIL noteLetter = "Q"
    READ Notes + index, noteLetter

    LOOKDOWN noteLetter, [ "C", "d", "D", "e", "E",
    "F", "g", "G", "a", "A",
    "b", "B", "R", "Q" ], offset

    LOOKUP offset, [ 4186, 4435, 4699, 4978, 5274,
    5588, 5920, 6272, 6645, 7040,
    7459, 7902, 0, 0 ], noteFreq

    READ Octaves + index, noteOctave
    noteOctave = 8 - noteOctave
    noteFreq = noteFreq / (DCD noteOctave)

    READ Durations + index, noteDuration
    noteDuration = 1000 / noteDuration

    READ Dots + index, noteDot
    IF noteDot = 1 THEN noteDuration = noteDuration * 3 / 2

    FREQOUT 8, noteDuration, noteFreq

    index = index + 1
    LOOP

    END


    and this is what i am trying to put in the code during the long FREQOUT

    DO
    HIGH 2
    PAUSE 500
    LOW 2
    HIGH 3
    PAUSE 500
    LOW 3
    HIGH 4
    PAUSE 500
    LOW 4
    LOOP


    Thanks $WMc%!



    nrsmac
  • Mike GreenMike Green Posts: 23,101
    edited 2011-12-17 08:07
    You can't do it. The best that you could do would be to limit the (time) length of the FREQOUT to 1/2 second and do multiple FREQOUTs for longer notes, then toggle the LED during the brief pauses between FREQOUTs. Essentially, you'd be using a FREQOUT instead of the PAUSE 500 for the delay between LED changes. You could probably do the LED flashing and restarting the FREQOUT within 2-3 milliseconds on a BS2. Something like this:
    ledNumber = 2
    do while noteDuration > 500
      high ledNumber
      freqout 8, 500, noteFreq
      noteDuration = noteDuration - 500
      low ledNumber
      ledNumber = ledNumber + 1
      if ledNumber > 4 then ledNumber = 2
    loop
    high ledNumber
    freqout 8, noteDuration, noteFreq
    low ledNumber
    
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-12-17 09:10
    The way I read the program, there is no note longer than 500mS anyway. The dotted notes only apply to the shorter durations. If it is okay for the lights to change in sync with the music, you could add another auxiliary table with the light pattern to be associated with each note and read that from the table and OUT it to the light pins 2--4 just before the FREQOUT.

    By the way, you said you are not great at programming with the bs2, but my compliments on a very well organized program.
  • IroneIrone Posts: 116
    edited 2011-12-18 17:49
    I think Jessica has already done this. Copy this link and look here:
    http://www.youtube.com/watch?v=hYn3-6PPeZo
    ( I do not know how to make a hyperlink on this form )
  • nrsmacnrsmac Posts: 42
    edited 2011-12-19 01:37
    Thanks you guys for all of the suggestions. I was actually trying to adapt Jessica's code for my own needs. (A flashing LED christmas tree that also plays music.)
    I will try all of these suggestions. Thanks!
  • GadgetmanGadgetman Posts: 2,436
    edited 2011-12-19 03:29
    I assume that on IO-pins 2, 3 and 4 you have LEDs?

    Why not just use the TOGGLE command?
    Keep a variable with the current pin-number
    Pseudo-code to insert before or after the FREQOUT:

    TOGGLE LED-Pin
    INC LED-Pin
    IF LED-PIN = 5 THEN LED-PIN == 2
    TOGGLE LED-Pin


    Something like that, at least...
    (Years since I coded for a BS2... )
Sign In or Register to comment.