Understanding Programming Arduino via Unity

I have read around on this topic and will be getting hands on shortly and want to make sure I am not reinventing the wheel for the particular approach I want to use and what pitfalls I may encounter. I will be using MIDI triggers feeding their MIDI note and MIDI channel info and data from a Arduino GY-521 MPU-6050 Module 3 axial gyroscope accelerometer stance tilt module. I will be creating a standalone application in Unity to set up the MIDI note and channel as well as sensitivity per trigger and route the MPU-6050 data into one or more of the MIDI controllers for expression based on velocity and tilt etc.

I have a good idea how to program this in the Arduino IDE and C. I want the Unity UI to be able to reprogram the Arduino microcontrollers. So I create the new setup, turn a trigger on or off and adjust the sensitivity of the triggers and alter the MIDI note and channel and which expression controller the accelerometer controls. I then have to send the new program to the Arduino and this is where it gets murky on me and I need clarification. Say I recreate the functions and variables so they are basic copies of the format Arduino uses. I complete the reconfiguring in the Unity app and I then hit an UPLOAD button and the new program is loaded to the Arduino. This program, I assume is sent on the USB port to the Arduino board as serial data.

1.How do I format this data to be read by and reprogram the Arduino? 2. Am I thinking about this in the correct manner? 3.I am also using two BT modules. Can this be done to both left and right modules at once (if I use USB then I can only do either left or right or use two USB out ports on the computer to each separate Arduino BT module talking to the main Arduino boards)

There is a difference between uploading a new program to an Arduino and uploading new data for an existing program to use.

I suspect (but I'm by no means certain) that you want your PC to send data to the Arduino rather than to completely change the program on it.

On the other hand if you actually want to reprogram the Arduino you will need to write a shell script (or similar) that calls the various programming components - GCC, AvrDude etc - that the Arduino calls.

...R

Robin2: There is a difference between uploading a new program to an Arduino and uploading new data for an existing program to use.

I suspect (but I'm by no means certain) that you want your PC to send data to the Arduino rather than to completely change the program on it.

On the other hand if you actually want to reprogram the Arduino you will need to write a shell script (or similar) that calls the various programming components - GCC, AvrDude etc - that the Arduino calls.

...R

Understood. What I am heading towards function wise is to set up the original Arduino with variables I can access to change via the upload from the user or call a function that needs executing. For example the trigger sensitivity may need adjusting by the user as they may be heavy or light and I want to adjust it so they have the full range of sensitivity regardless by turning up or down the trigger sensitivity. So in the Arduino program I set up a variable and a function. Say I name the vars trigSens1, trigSens2 ... trigSens16. In a function these vars are used to set the range of sensitivity and use them to multiply the trigger value as read by the ADC with. Since I use the trigSens vars as a multiplier I will use the values of 0.01 to 1.0 with 0.5 being a standard sensitivity whilst lesser values dampen or diminish the trigger sensitivity and higher values adjust lighter hit values upward so no matter the style of triggering the sensitivity has a full range. By this I mean that for example some fellow is a heavy hitter all the time and his velocity is always above 120. I can then send a multiplier to change the trigger readings to a broader range of say 64 to 127. Vice versa in that a consistently light hitter is getting velocity readings below 100 always. I can then send a trigness var to the TriggerSensitivity function to adjust it upwards.

Now, say I get ten of these units in a jam session and they all have BlueTooth modules. I assume these have a unique identifier. But I am also assuming this identifier is the same based on factory settings for all units until it is set differently via programming. Is this something I can do by setting a var for btID (BlueTooth ID) and uploading that from the Unity interface.

As well assuming I can set vars and call functions in a pre-written and loaded sketch on the Arduino. What is the protocol for doing so? Do I simply pass the new var value and if so by what method? For calling a function in the Arduino sketch what is the protocol there?

I wrote a demo program using Python to communicate with an Arduino here. It might give you a flavour of how to pass data to an Arduino. It could be simplified if you don't need to be able to send every byte value from 0 to 255.

As far as unique IDs are concerned these could be included in the program that is uploaded to each Arduino - very simple if you don't have very many Arduinos and if the users don't need to change the IDs.

The Arduino also has non-volatile EEPROM memory that could be used to store a unique ID that could be user programmable. It could also be used to store settings that you need to retain when the device is powered off. Just be conscious that the EEPROM memory has a limited number of write-cycles - about 100,000 I think.

...R

Thanks for the link to some code to parse and get my head around. I will look at it and come back to this thread with any questions.