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?
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Just copy + pasting the 2 posts from the other forum post:
@allObjects' Post
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?