Shop OBEX P1 Docs P2 Docs Learn Events
Building a silent metronome.....need help with some stuff — Parallax Forums

Building a silent metronome.....need help with some stuff

lotrfreak16lotrfreak16 Posts: 5
edited 2011-03-18 14:10 in BASIC Stamp
I'm trying to build a silent metronome by using a dip switch and multiple outputs from the pins, and the dip switch to be all tied to the output of an LED with a 470 ohm resistor for current limiting. I currently have the times required for 6 different BPM (beats per minute) and have tested them individually using a DO LOOP statement. The complication is trying to fire all of the pins at the same time and not have complications with the PAUSE commands of the previous set messing up the timing of the second and subsequent pin firings. This code is what I have so far.....any suggestions on how to get them all to fire at once, but keep the right timings? I'll attach the BS2 file for those who wanna mess with it
' Silent Metronome
' Using an LED and a DIP switch, you can simulate many different BPM (Beats Per Minute) for use in music.

' {$STAMP BS2}
' {$PBASIC 2.5}

' run pins 8 through 13, bpm 69 through 160

DO

  HIGH 8
  PAUSE 20
  LOW 8
  PAUSE 840  ' these pause times create 69 bpm

LOOP

DO

  HIGH 9
  PAUSE 20
  LOW 9
  PAUSE 720  ' these pause times create 80 bpm

LOOP

DO

  HIGH 10
  PAUSE 20
  LOW 10
  PAUSE 570   ' these pause times create 100 bpm

LOOP

DO

  HIGH 11
  PAUSE 20
  LOW 11
  PAUSE 465  ' these pause times create 120 bpm

LOOP

DO

  HIGH 12
  PAUSE 20
  LOW 12
  PAUSE 385  ' these pause times create 144 bpm

LOOP


DO

  HIGH 13
  PAUSE 20
  LOW 13
  PAUSE 340  ' these pause times create 160 bpm

LOOP

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-03-10 13:17
    attachment.php?attachmentid=78421&d=1297987572
  • lotrfreak16lotrfreak16 Posts: 5
    edited 2011-03-10 13:20
    Thanks Phil!
  • ercoerco Posts: 20,257
    edited 2011-03-10 13:23
    It's not clear what you're asking, especially the "all pins fire at once" part. You want to use different DIP switch settings to branch to your various timing routines, most of which use an LED on pin 11 but 100 bpm uses pin 10?

    Please clarify for help. BTW, a rotary selector switch, such as http://www.radioshack.com/product/index.jsp?productId=2062536 might be more intuitive than a dip switch.
  • lotrfreak16lotrfreak16 Posts: 5
    edited 2011-03-10 13:31
    Sorry I forgot to change the pin numbers
    It's meant to use different pins for each high low (going from pin 8 for the lowest to 13 for the highest), and the DIP switch would select which one to go. Just using a DIP switch because that's what I had available for cheap. What I meant by "firing all the pins at once" was just wanting to have each "high low" selection go all at once, so I could then choose which BPM I wanted to output to the LED using the switch.
    I'll go up and edit the code to change what it's saying.
  • ercoerco Posts: 20,257
    edited 2011-03-10 16:11
    So when you say "firing all the pins at once", I think you mean reading all of your DIP switch input pins at once? Use the INA or similar command to read 4 pins simultaneously and then branch accordingly. That's a quick command that won't noticeably slow your program down, you can read your switch first, branch to the appropriate timing routine, then loop back and read your pins again.
  • lotrfreak16lotrfreak16 Posts: 5
    edited 2011-03-10 18:52
    What I was originally thinking was an idea like this picture, with the pins being Vcc, and using HIGH LOW and PAUSE commands to simulate a metronome. I'm not sure what you're explaining using INA for the program. I'm trying to do this as a project for my college class, and I'm still a BS2 noob. Could you explain a little more your theory?
    389 x 385 - 7K
  • ercoerco Posts: 20,257
    edited 2011-03-10 19:46
    Brother, I think you need to read the Good Book and experience the many miracles documented therein.


    WAM, of course: http://www.parallax.com/Portals/0/Downloads/docs/prod/edu/28123-WAM-v3.0.pdf

    Per my last post, you'll just use the DIP switch as an input device to tell the Stamp which timing routine to use. It can only do one thing at a time. Read that book and work through it, focusing on inputs and outputs.
  • lotrfreak16lotrfreak16 Posts: 5
    edited 2011-03-17 19:52
    Well I have changed up my idea, and I think I'm just going to use one pin, but try to utilize a pushbutton to cycle through the BPM cycles. What would I need to add to this code to get a timing functionality into here?
    
    ' Silent Metronome
    ' Using an LED and a DIP switch, you can simulate many different BPM (Beats Per Minute) for use in music.
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' run pin 8, bpm 69 through 160
    
    DO
    
      IF (value = 0), THEN GOSUB bpm_69
      timeCounter = timeCounter + 1
      IF (value = 1), THEN GOSUB bpm_80
      timeCounter = timeCounter + 1
      IF (value = 2), THEN GOSUB bpm_100
      timeCounter = timeCounter + 1
      IF (value = 3), THEN GOSUB bpm_120
      timeCounter = timeCounter + 1
      IF (value = 4), THEN GOSUB bpm_144
      timeCounter = timeCounter + 1
      IF (value = 5), THEN GOSUB bpm_160
    
    LOOP
    
      bpm_69:
      DO
    
        HIGH 8
        PAUSE 20
        LOW 8
        PAUSE 840  ' these pause times create 69 bpm
    
      LOOP
    
      bpm_80:
      DO
    
        HIGH 8
        PAUSE 20
        LOW 8
        PAUSE 720  ' these pause times create 80 bpm
    
      LOOP
    
      bpm_100:
      DO
    
        HIGH 8
        PAUSE 20
        LOW 8
        PAUSE 570   ' these pause times create 100 bpm
    
      LOOP
    
      bpm_120:
      DO
    
        HIGH 8
        PAUSE 20
        LOW 8
        PAUSE 465  ' these pause times create 120 bpm
    
      LOOP
    
      bpm_144:
      DO
    
        HIGH 8
        PAUSE 20
        LOW 8
        PAUSE 385  ' these pause times create 144 bpm
    
      LOOP
    
      bpm_160:
      DO
    
        HIGH 8
        PAUSE 20
        LOW 8
        PAUSE 340  ' these pause times create 160 bpm
    
      LOOP
    
    
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-18 05:45
    Are you using a BoE or a Homework board? They have reset switch which can be handy.

    A quick way to do this would be to read the dip switches and derive a PAUSE based on that.
    Sort of like what you've alluded to above.
    Then you'd enter your DO...LOOP where you'd flash your LED and then PAUSE, based on
    that initial dip switch reading.
    The PAUSEs you'll have to sort of work out by trial and error (as
    one beat = one flash time + the PAUSE time + the instruction execution time
    to do that.)
    You'd be "trapped" in an endless DO...LOOP, but you'd get out of it by pushing
    the Reset button.
    If you had a long PAUSE at the beginning, it'd give you time to set switches
    before the Stamp reads them.

    To change BPMs on-the-fly would involve a more complex approach, re-reading
    the DIPs while not losing time in the process.

    Does that make sense?
  • ercoerco Posts: 20,257
    edited 2011-03-18 09:11
    Each of your bpm loops has a pause which you calibrate for timing. Replace the pause with a calibrated for/next loop which checks for a pushbutton press. If the button is pressed, jump to the next bpm loop. You'll need debounce and some recalibrating, but this would be the simplest method. You lack a tempo display, though, so you'll have to count your button presses and know what tempo is used.
  • ercoerco Posts: 20,257
    edited 2011-03-18 14:10
    You could also add a short routine before each loop to flash the same LED 1 or 2 or 3 or...8 times before getting into the tempo loop. That would indicate which timing routine (1-8) was active. You could have a simple label to say routine 1 was 69, 2 was 80, etc.
Sign In or Register to comment.