Shop OBEX P1 Docs P2 Docs Learn Events
MIDI OUT from inbound on/off switches — Parallax Forums

MIDI OUT from inbound on/off switches

VJ AnomoleeVJ Anomolee Posts: 15
edited 2007-05-07 02:33 in Learn with BlocklyProp
Hello everyone,
This is my first time posting in this forum. So not entirely sure im posting this in the right category
My question is How can i use a momentary contact switches (its actually going to be 14 laser to photocell switches) to send a MIDI note on and then off value out over MIDI (which will then trigger a video on my laptop-and off value stops the video).
-As far as wiring i can probably use Tom Igoe's schematic from the physical computing book.
(see attached pics)

But my main hurdle is the Basic code for taking in 14 different laser to photoresistor on off switches and translating that to a MIDI note on off msgs to send out via the wired MIDI port.

Any help at all would be greatly appreciated.
350 x 172 - 8K
400 x 263 - 14K

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-05-05 17:41
    Hi VJ, a starting point would be to look at the INS command in the Pbasic help file under Memory and Variables. This command can take your 14 inputs at once and produce a number between 0 and 16383 for conversion/manipulation to a midi note.

    Jeff T.
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-05 17:46
    Hi thanks for the quick reply,
    And after reading back over it i just realized that i forgot to mention that each of the fourteen will trigger a different midi note.
    is that still possible using the INS command?
  • edited 2007-05-05 18:19
    Go to the www.parallax.com page, and click the "Or Search Parallax with Google..." link. Then, enter MIDI.

    One link I found there that might be useful was a Nuts and Volts volume 4 PDF with a MIDI article:

    http://www.parallax.com/html_pages/downloads/nvcolumns/Nuts_Volts_Downloads.asp

    Andy

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-05-05 18:26
    Hi VJ, yes it is possible, each bit of the 14 will represent a note for example INS=%00000000001000 would represent note·4 or bit3

    For individual·notes then use a lookup table with 14 note values.

    index=INS

    LOOKUP index,[noparse][[/noparse]note1,note2,note3, etc. etc.], note_value

    SEROUT note_value

    Jeff T.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-05 18:30
    A lot depends on what behaviour you want. For example, do you want the first input found to generate a MIDI note or do you want a specific MIDI note generated whenever a particular input transitions from off to on? Do you want a specific MIDI note to be generated periodically whenever a particular input is found on whether it was on previously or not? What's the period involved? Do you want the note turned off when the input is off? You get the idea.

    Typically, if you want a different MIDI note issued for any off to on transition, you have two word variables "oldVal" and "newVal". You initialize "oldVal" to zero and do:
    oldVal = 0
    do                                            ' repeat indefinitely
       newVal = INS                        ' get the current input state
       newBits = !oldVal & newVal   ' a bit is 1 if oldVal bit was 0 and newVal bit is 1
       oldVal = newVal                    ' save new state for next time
       for i = 0 to 13
          if newBits & 1 = 1 then      ' check from pin 0 to pin 13
             gosub sendMIDI              ' send MIDI command for note "i" (0-13)
          endif
          newBits = newBits / 2        ' on to next higher I/O pin
       next
    loop
    
    
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-05 19:32
    Posted Today 12:24 PM (GMT -7)
    How are you coding the MIDI out? For the closures you will need another chip (or chips) to handle that many inputs.
    - Stephen


    -Is this true Unsoundcode, Mike Green?
    Does that mean it wont work with this Basic stamp homework board - Rev. C that i have?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-05 19:40
    The Stamps have 16 I/O pins in addition to the programming/debugging port that can be used for serial I/O with some limitations. With 14 sensors, that leaves 2 I/O pins. If you're transmitting MIDI, that takes 1 I/O pin which leaves a "spare" for something else. You can certainly do this (but not much else) with a Stamp (and the Homework Board). If you have any visions of expansion, you will need to offload the reading of the sensors from the Stamp itself to an external device like the 74HC165 which is an 8-bit input shift register that is cascadable to nearly any length. There are several Nuts and Volts columns (on the Parallax website downloads area) that discuss expanding the I/O capability of the Stamp including using the 74HC165.
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-05 19:49
    Ok thats good news then.
    no time or need to upgrade the I/O
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-05-05 20:30
    Fourteen pins does not limit you to 14 notes , the inclusion of a loop and a counter counting iterations through the loop can measure the length of note (time the input is high) which can be used to adjust the midi output.

    Jeff T
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-05 20:35
    ok im lost now..
    did i mention im new to basic and the stamp?

    *cracks open the manual*

    Post Edited (VJ Anomolee) : 5/5/2007 8:41:33 PM GMT
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-05-05 21:07
    Sorry VJ didn't mean to confuse things. The Nuts and Volts article Andy refered should really get you going in the right direction. http://www.parallax.com/dl/docs/cols/nv/vol4/col/nv94.pdf

    Jeff T.
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-06 20:10
    ugh! stayed up most of the night and still pulling my hair out. freaked.gif
    Anyone have some complete MIDI out code for generating notes out?
    Ive tried all the versions i found online.
    as well as the nuts + volts download you guys mentioned.
    all of these wouldnt compile to the chip or they simply did nothing when connecting to MIDI in (on several differnt MIDI keyboards).
    such as tom igoe's code.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' Simple MIDI out:
    HIGH 15 ' power LED on
    ' declare variables:
    i VAR BYTE
    main:
    FOR i = 36 TO 96 ' a good middle range of MIDI notes
    SEROUT 6, 12, [noparse][[/noparse]144, i, 64]
    PAUSE 500
    SEROUT 6, 12, [noparse][[/noparse]128, i, 0]
    NEXT
    GOTO main

    Please help. cry.gif

    Post Edited (VJ Anomolee) : 5/6/2007 8:29:35 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-06 20:24
    You have to be more specific about your problem. In particular, how do you have things hooked up? What is the actual code you're trying to compile? Make sure you have the current version of the Stamp Editor installed. Exactly what error message are you getting and on which line?

    The references we've cited have working MIDI out code. The question is: "What's different about your setup that it doesn't work?".
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-06 20:43
    ok im using the wiring diagram from the nuts and volts issue cited.(attached)
    as follows into the homework board-{ pin 5 of the midi connector is going to 220ohm res. going to pin 15(stamp), pin 4(MIDI) to 220ohm resistor to Vdd, and pin 2 (MIDI) to Vss}
    MIDI connector(female) has Male to male MIDI going to MIDI in of Yamaha DX7.check this link for MIDI info on it-
    http://www.maths.abdn.ac.uk/~bensondj/dx7/manuals/dx7-midi.txt
    (do i need to specify some of this info to the stamp???)

    Homework board has usbto232 adaptor to usb on the Macbook Pro
    Running latest version of MacBS2

    Communication with the stamp + compiling successful for code i put in previous post.
    Audio does come out of the DX7 when pushing keys down just not via the MIDI in from the stamp
    Double checked all wiring to ensure no shorts.
    Here is the code on the stamp currently: (compiled with no problems)
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    ' Simple MIDI out:
    ' declare variables:
    i VAR BYTE
    main:
    FOR i = 36 TO 96 ' a good middle range of MIDI notes
    SEROUT 15, 12, [noparse][[/noparse]144, i, 64]
    PAUSE 500
    SEROUT 15, 12, [noparse][[/noparse]128, i, 0]
    NEXT
    GOTO main

    Post Edited (VJ Anomolee) : 5/6/2007 8:56:30 PM GMT
    589 x 385 - 36K
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-06 21:03
    One possibility is that the Homework board already has the 220 ohm resistor built-in. It may be that the extra 220 ohms is preventing the MIDI interface from operating (since there's already 470 ohms present along with an LED for the optoisolator). It should still work though ... hmmm.

    Try leaving out that 220 ohm resistor. Leave the other one (to +5V) there.

    Post Edited (Mike Green) : 5/6/2007 9:09:03 PM GMT
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-06 21:13
    k will try that.
    here is a few pics of the wiring.

    Post Edited (VJ Anomolee) : 5/6/2007 11:09:08 PM GMT
    640 x 480 - 125K
    640 x 480 - 119K
    640 x 480 - 121K
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-06 21:15
    Ok Mike, that didnt work either.
    -really feel like there is something missing in the code
    ???
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-06 21:32
    For the simple test you're doing, the code looks correct. You've got the right pin # (15) and the correct Baud constant for MIDI (12). The Nuts and Volts article shows a Baud constant of $8000+12, but the $8000 would not prevent the thing from working. You might try swapping MIDI 4 and 5 connections. It may be that the article shows the connections for the plug end rather than the socket end and you're using a socket. That's all I can think of without digging in with a scope and logic analyzer.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-05-06 22:15
    Your observation on the baud Mike, I was reading an article about a BS2 midi interface and the baud was set to 32780 (which is $8000 + 12) . I had never seen the Stamp baud declared like this and couldn't figure out why.

    Maybe worth trying, I will look for the article and if I find it again will post the link.

    Jeff T.

    EDIT this link contains a link to code for the BS2 by Jeff Mann http://www.audiomulch.com/midipic/

    Post Edited (Unsoundcode) : 5/6/2007 10:22:10 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-06 22:21
    The $8000 specifies "open mode" (see the SEROUT statement description in the PBasic manual). This switches the pin to ground for a logic low and leaves the pin in a high impedance state for a logic high. It's intended for networking several Stamps together. The way the MIDI hardware is set up, it'll work either way (the pin can be at +5V or high impedance for a logic high).
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-05-06 22:26
    Ok thanks, I need to look at that a little closer. Jeff Mann's comments mentioned 31.25kb, 8n1, non-inverted, open collector.

    Jeff T
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-06 22:26
    here are the pics with the 220ohm resistor removed from the data side

    *wish i had an arduino*

    Post Edited (VJ Anomolee) : 5/6/2007 11:09:58 PM GMT
    640 x 480 - 179K
    640 x 480 - 120K
    640 x 480 - 94K
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-05-06 22:29
    Are you doing some sort of mirror effect on the photos you’re uploading? I was trying to evaluate your circuit but gave up since everything is a mirror image. Makes it too hard to follow.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-06 22:59
    ha.
    nope no mirror effect. thats just the way it takes the pic
    i will reverse them for you though- stand by for pic edits

    edit- pics flipped horizontally

    Post Edited (VJ Anomolee) : 5/6/2007 11:11:12 PM GMT
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-06 23:14
    OK how can i change jeff mann's code to send a note off/on instead of CC data out?
    -and this wont compile- i get a !Symbol is already defined! error when trying to run this- "FOR" (right under doit) is highlighted
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    'constants, shouldn't change these midibaudmode CON 32780 '31.25kb, 8n1, non-inverted, open collector
    'for 14-bit controllers - controller 32 is lsb of controller 0, etc. controllerLSBoffset CON 32
    'MIDI status bytes:
    controller CON %10110000 + midichannel
    noteon CON %10010000 + midichannel
    pitchbend CON %11100000 + midichannel

    'declare variables
    value VAR WORD 'holds the 16-bit value read from the pot
    PIN VAR NIB 'which pin/pot we are reading at the moment
    statusbyte VAR BYTE 'MIDI status; controller, noteon, or pitchbend
    data1 VAR BYTE 'first data byte, eg. controller or note number
    data2 VAR BYTE 'second MIDI data byte, eg. value or velocity

    'user configuration - adjust these to suit you
    'DANGER - check your circuitry and configure pins accordingly!!
    'set unused pins to outputs
    DIRS = %1111111111111111 'all outs
    OUTS = %0000000000000000 'all low
    midichannel CON 1 'MIDI transmit channel
    midioutpin CON 0 'pin connected to MIDI out connector
    'check the MIDI spec for a list of controller numbers you can use
    controlleroffset CON 0 'add to pot's pin # to get MIDI controller #
    hipot CON 15 'highest pin with a pot to measure
    lowpot CON 14 'lowest pin with a pot to measure


    doit:
    DEBUG HOME
    FOR PIN = lowpot TO hipot
    'read the value of the pot
    HIGH PIN
    PAUSE 1
    RCTIME PIN, 1, value
    DEBUG "pot ", DEC PIN, " reads ", DEC value, CR
    value = value >> 1 'drop the least significant bit

    'send most significant 7 bits as continuous controller msg
    statusbyte = controller 'sending controller message
    data1 = PIN + controlleroffset 'controller #
    data2 = value.highbyte
    SEROUT midioutpin, midibaudmode, [noparse][[/noparse]statusbyte, data1, data2]

    ''uncomment this section to send 14-bit controller data
    ''send least significant 7 bits
    'data1 = data1 + controllerLSBoffset
    'data2 = value.lowbyte
    'data2 = data2 >> 1 'convert to 7 bits
    ''use running status byte (can leave it out if unchanged)
    'serout midioutpin, midibaudmode, [noparse][[/noparse]data1, data2]
    NEXT
    GOTO doit

    Post Edited (VJ Anomolee) : 5/6/2007 11:19:48 PM GMT
  • VJ AnomoleeVJ Anomolee Posts: 15
    edited 2007-05-07 00:37
    Wow this seems like way too much code for the simple task im trying to accomplish.
    Can i not just add the note on/off (SEROUT) command IF a certain resistance is measured (laser on photocell is blocked) with RCTIME on the photoresistors ?
    using this code:

    'What is a microcontroller- test photoresistor.bs2
    'read photoresistor in RC-time circuit using RCTIME command.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    time VAR Word
    DO
    High 2
    PAUSE 100
    RCTIME 2, 1, time
    DEBUG HOME, "time = ", DEC5 time

    LOOP
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-07 01:11
    You're getting the error message because "pin" is reserved in version 2.5 of the PBasic syntax. The program may have been written before this was changed. The easiest thing to do is to change "pin" to "pinx".

    You could certainly do an RCTIME as you posted. Keep in mind that each RC measurement cycle (charge, then discharge and measure) will take some milliseconds and you want to do 14 of them. That may be fine for your setup or the time to go through all the measurements may be too long. You'll have to decide.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-05-07 02:33
    Hi VJ, Jeff Mann's code might really fit well with your photo resistors eventually but for now I would stick with the simple code you posted last.

    I used your code·wired·to the midi input·on the·game adapter of my soundcard·. Once I had the·software configured it played fine.

    Is there something you have to configure on the keyboard maybe?

    Jeff T.
Sign In or Register to comment.