• Hi there,

    Im looking for some help with coding for a project im working on if some kind soul could help me with it, as i am new to coding and dont really know how to make it work. Its prob very easy to do for someone who knows what they are doing,

    Basically im trying to create a MIDI controller, although im having some difficulty coding the values to MIDI and sending it down the USB to MIDI converter. I have 2 pots and 2 switches to code. A kind friend give me the code for one of the pots although i cant seem to get it to work with the Espruino. The code is:

    function onInit() {
    
    Serial1.setup(31250, {tx:B6,rx:B7});
    
    Serial1.write(0x90);
    Serial1.write(0x3D);
    Serial1.write(0x00);
    Serial1.write(0x90);
    Serial1.write(0x3D);
    Serial1.write(0x44);
    }
    
    var Pot1=0;
    
    function getAcc() {
    
    var Pot1 = analogRead(C0);
    
    Pot1 = round(Pot1*128);
    
    Serial1.write(0x0B);
    Serial1.write(pot1);
    Serial1.write(0x9F);
    }
    
    onInit();
    setInterval(readPot1, 200);
    

    This code doesn't work for me for some reason- it keeps saying undefined. & I dont want to keep hassling my friend as he has helped me out loads to get to this point! Does someone know where i have gone wrong interpreting this code?

    The potentiometers are Linear btw.

    I have included a diagram of my wiring with my circuitry also to the Espruino.

    I know this will be useful
    http://www.midi.org/techspecs/midimessag­es.php

    If anyone could help me out with the switches or getting the pots working (Or both!) i would be soo greatful! How would i get this to work does anyone know? All i have to do is get the right code from the Voltage value to be turned to MIDI and then send down the MIDI to USB converter to the computer.

    [](http://IMG_1102.JPG)

    Thank you!

    Kind regards
    Connor

  • Please be sure to only post your new thread once - somehow we got three copies of this (gordon will remove the extra copies next time he's online).

    Also, you need to include the full URL of the image you're trying to include.

    = undefined

    is shown when you run any command in the console that does not have a return value - this is standard javascript. It doesn't indicate any problem, unless you were expecting it to return something.

    Serial.write() does not include a linebreak. So under most circumstances, you should be able to:

    
    Serial1.write(0x90);
    Serial1.write(0x3D);
    Serial1.write(0x00);
    Serial1.write(0x90);
    Serial1.write(0x3D);
    Serial1.write(0x44);
    
    

    into

    
    Serial1.write([0x90,0x3D,0x00,0x90,0x3D,­0x44]);
    
    

    or

    
    Serial1.write("\x90\x3D\x00\x90\x3D\x44"­);
    
    

    Also, it's Math.round(), not round().

  • How have you wired to the midi connector?
    Do you have a schematic?

  • Also, have you seen the module for MIDI?

  • Just copy + pasting the 2 posts from the other forum post:


    @allObjects' Post

    function onInit() {
    
    Serial1.setup(31250, {tx:B6,rx:B7});
    
    Serial1.write(0x90);
    Serial1.write(0x3D);
    Serial1.write(0x00);
    Serial1.write(0x90);
    Serial1.write(0x3D);
    Serial1.write(0x44);
    }
    
    var Pot1=0;
    
    function getAcc() {
    
    var Pot1 = analogRead(C0);
    
    Pot1 = round(Pot1*128);
    
    Serial1.write(0x0B);
    Serial1.write(pot1);
    Serial1.write(0x9F);
    }
    
    onInit();
    setInterval(readPot1, 200);
    

    Just to understand correctly This code..., is this what you have in the edit pane - right half view - of the Espruino Web IDE and you upload to Espruino?

    Instead providing the image reference as per your computer in the [](uri) post format, please upload the image with the Upload a file link in the (edit) post widget.

    Because your image is accessible by given link, I assume from our code and description:
    You read the voltage of the wiper with port C0 of a potentiometer that is connected at one end to ground GND and on the other to 3.3 volt of the Espruino board. You adjust by some calculation a value understood by you midi device, to which you send it then over the serial port. And you do this over and over again 5 times per second.

    Espruino execution complains about unknown references....

    round is not a globally defined function, but a method (function) of the Math object (library). Change change this to: Pot1 = Math.round(Pot1*128);.

    Next unknown reference is in line 22. (lower case) pot1, which is nowhere defined... and thought to have defined (upper case) Pot1 in line 17, and adjusted in line 19. Change lower case p in line 22 to upper case P. For line 20, add 20. console.log(Pot1); to see what you are actually sending to the Midi device.

    Btw: I assume you want to send values from 0..127 to your MIDI device. For that we need a slight adjustment of the calculation, for example Pot1 = Math.floor((Pot1 + 0.5)*128) - 1;. This calculation sends 0..127 and achieves a more 'just' and 'safe' mapping of the read decimal fraction... ;-) ...but I may be totally wrong here.

    Next unknown reference is the function readPot1 in line 26... Replace readPot1 by getAcc - or vice-versa. This will invoke getAcc() - or readPot1 - every 200 milliseconds the function that reads the value, adjusts it, and sends it to your device.

    Next thing to talk about is the sequence of onInit() and setInterval(). For development phase, the current sequence is ok: onInit() is invoked and then setInterval(); For IDE disconnected state though, you will save Espruino's state - see save() command in console pane - so that on (power on) reset (in IDE disconnected state), Espruino will run the complete code. On (power on) reset, Espruino will execute onInit() 'only' and thus - with current sequence - miss out on invoking setInterval(). Therefore, move your line 27. with the setInterval() as the last in your onInit() function (after line 10).

    What you are trying to send - with the given serial speed - has of course to fit into the 200 ms window... (plus some extra stuff, such reading the value, calculation, etc.). Assuming you have to send about 35 bits (max) - including start and stop bits and parity - you can decrease the interval down to - theoretically - 1..2 ms to get a more responsive behavior. With an interval so short, you may though make Espruino unavailable for other things you'd like it to do. Therefore, you will pick a compromise in reading interval from C0 and send only if the value has changed. But you keep that enhancement for a next development step - and experiment with success!

    PS: Did took a look a the MID message definitions... and wondered about what message(s) you want to send... (in the onInit() as well as in the getAcct() - or readPot1() ) .


    @Loop's Post

    Hello,
    Actually I while ago I wrote a midi input library for espruino. If someone wants to add midi output, it could be a base point.
    Also.. at the time I couldn't get it to work with direct midi, but only via a midi to serial bridge on my pc. I always wondered wether it was my photocoupler wrong, or if the uart on the arm chip couldn't do 31250. Can it? any ideas?

  • Just to add to this, I think @allObjects has covered a lot of potential issues with your code.

    Right now Espruino doesn't warn you when you use an undefined variable, so it's quite easy to accidentally use the wrong one when you're not used to programming - especially as JavaScript is case sensitive.

  • Hi there,

    Thanks for your help guys, il post my diagram here now!

  • For some reason i cannot upload the image, heres a written schematic.. for @alexanderbrevig

    Espruino Pins:

    Pin C0 -> Pot1 (potentiometer 1)
    Pin C1 -> Pot2 (potentiometer 2)

    Pin B6 -> tx:/I+ (Midi to USB converter)
    Pin B7 -> rx:/O+ (Midi to USB converter)

    Pin B8 -> Switch1
    Pin B9 -> Switch2

    Thanks alot for you help @Gordon for that from the other forum, il have a go at this later to see if i can get it working when i have a chance to properly read through it.

    Does anyone know how i can get the switches working?

  • What is the code you use for getting the switches' state? Depending how you connect your switches - pins B8 an dB9 against 3.3V or against GND grounbd, you have to set the pins' modes to either input_pulldown or input_pullup.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Help required with project plz! MIDI + Espruino + MIDI -> USB converter

Posted by Avatar for ConnorHaynes @ConnorHaynes

Actions