{"id":545,"date":"2023-12-02T01:19:44","date_gmt":"2023-12-01T23:19:44","guid":{"rendered":"https:\/\/wiki.dreid.ch\/?p=545"},"modified":"2023-12-02T01:41:29","modified_gmt":"2023-12-01T23:41:29","slug":"https-go-musiconerd-com-code-gen-basic","status":"publish","type":"post","link":"https:\/\/wiki.dreid.ch\/?p=545","title":{"rendered":"https:\/\/go.musiconerd.com\/code-gen-basic"},"content":{"rendered":"<p>\/*<br \/>\n  Made by Gustavo Silveira, 2023.<br \/>\n  &#8211; This Sketch reads the Arduino&#8217;s digital and analog ports and send midi notes and midi control change<br \/>\n<iframe loading=\"lazy\" title=\"I made an ARDUINO MIDI CONTROLLER CODE GENERATOR\" width=\"770\" height=\"433\" src=\"https:\/\/www.youtube.com\/embed\/ZjzAyPGLrOA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe><br \/>\n  http:\/\/www.musiconerd.com<br \/>\n  http:\/\/www.youtube.com\/musiconerd<br \/>\n  http:\/\/facebook.com\/musiconerdmusiconerd<br \/>\n  http:\/\/instagram.com\/musiconerd\/<br \/>\n  http:\/\/www.gustavosilveira.net<br \/>\n  gustavosilveira@musiconerd.com<\/p>\n<p>  If you are using for anything that&#8217;s not for personal use don&#8217;t forget to give credit.<\/p>\n<p>  PS: Just change the value that has a comment like &#8221; \/\/ &#8221;<\/p>\n<p>*\/<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ Choosing your board<br \/>\n\/\/ Define your board, choose:<br \/>\n\/\/ &#8220;ATMEGA328&#8221; if using ATmega328 &#8211; Uno, Mega, Nano&#8230;<br \/>\n\/\/ &#8220;ATMEGA32U4&#8221; if using with ATmega32U4 &#8211; Micro, Pro Micro, Leonardo&#8230;<br \/>\n\/\/ &#8220;TEENSY&#8221; if using a Teensy board<br \/>\n\/\/ &#8220;DEBUG&#8221; if you just want to debug the code in the serial monitor<br \/>\n\/\/ you don&#8217;t need to comment or uncomment any MIDI library below after you define your board<\/p>\n<p>#define ATMEGA32U4 1  \/\/ put here the uC you are using, like in the lines above followed by &#8220;1&#8221;, like &#8220;ATMEGA328 1&#8221;, &#8220;DEBUG 1&#8221;, etc.<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ Are you using buttons?<br \/>\n#define USING_BUTTONS 1  \/\/ comment if not using buttons<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ Are you using potentiometers?<br \/>\n\/\/#define USING_POTENTIOMETERS 1  \/\/ comment if not using potentiometers<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ LIBRARIES<br \/>\n\/\/ &#8212; Defines the MIDI library &#8212; \/\/<\/p>\n<p>\/\/ if using with ATmega328 &#8211; Uno, Mega, Nano&#8230;<br \/>\n#ifdef ATMEGA328<br \/>\n#include <MIDI.h>  \/\/ by Francois Best<br \/>\n\/\/MIDI_CREATE_DEFAULT_INSTANCE();<\/p>\n<p>\/\/ if using with ATmega32U4 &#8211; Micro, Pro Micro, Leonardo&#8230;<br \/>\n#elif ATMEGA32U4<br \/>\n#include &#8220;MIDIUSB.h&#8221;<\/p>\n<p>#endif<\/p>\n<p>#ifdef USING_POTENTIOMETERS<br \/>\n\/\/ incluir a biblioteca ResponsiveAnalogRead<br \/>\n#include <ResponsiveAnalogRead.h>  \/\/ [https:\/\/github.com\/dxinteractive\/ResponsiveAnalogRead](https:\/\/github.com\/dxinteractive\/ResponsiveAnalogRead)<\/p>\n<p>#endif<br \/>\n\/\/ &#8212;- \/\/<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ BUTTONS<br \/>\n#ifdef USING_BUTTONS<\/p>\n<p>const int N_BUTTONS = 8;                                \/\/  total numbers of buttons<br \/>\nconst int BUTTON_ARDUINO_PIN[8] = {10, 16, 14, 15, 18, 19, 20, 21};  \/\/ pins of each button connected straight to the Arduino<\/p>\n<p>int buttonCState[N_BUTTONS] = {};  \/\/ stores the button current value<br \/>\nint buttonPState[N_BUTTONS] = {};  \/\/ stores the button previous value<\/p>\n<p>\/\/#define pin13 1 \/\/ uncomment if you are using pin 13 (pin with led), or comment the line if not using<br \/>\nbyte pin13index = 12;  \/\/ put the index of the pin 13 of the buttonPin[] array if you are using, if not, comment<\/p>\n<p>\/\/ debounce<br \/>\nunsigned long lastDebounceTime[N_BUTTONS] = { 0 };  \/\/ the last time the output pin was toggled<br \/>\nunsigned long debounceDelay = 50;                   \/\/ the debounce time; increase if the output flickers<\/p>\n<p>#endif<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ POTENTIOMETERS<br \/>\n#ifdef USING_POTENTIOMETERS<\/p>\n<p>const int N_POTS = 0;                            \/\/ total numbers of pots (slide &#038; rotary)<br \/>\nconst int POT_ARDUINO_PIN[0] = {};  \/\/ pins of each pot connected straight to the Arduino<\/p>\n<p>int potCState[N_POTS] = { 0 };  \/\/ Current state of the pot<br \/>\nint potPState[N_POTS] = { 0 };  \/\/ Previous state of the pot<br \/>\nint potVar = 0;                 \/\/ Difference between the current and previous state of the pot<\/p>\n<p>int midiCState[N_POTS] = { 0 };  \/\/ Current state of the midi value<br \/>\nint midiPState[N_POTS] = { 0 };  \/\/ Previous state of the midi value<\/p>\n<p>const int TIMEOUT = 300;              \/\/ Amount of time the potentiometer will be read after it exceeds the varThreshold<br \/>\nconst int varThreshold = 20;          \/\/ Threshold for the potentiometer signal variation<br \/>\nboolean potMoving = true;             \/\/ If the potentiometer is moving<br \/>\nunsigned long PTime[N_POTS] = { 0 };  \/\/ Previously stored time<br \/>\nunsigned long timer[N_POTS] = { 0 };  \/\/ Stores the time that has elapsed since the timer was reset<\/p>\n<p>int reading = 0;<br \/>\n\/\/ Responsive Analog Read<br \/>\nfloat snapMultiplier = 0.01;                      \/\/ (0.0 &#8211; 1.0) &#8211; Increase for faster, but less smooth reading<br \/>\nResponsiveAnalogRead responsivePot[N_POTS] = {};  \/\/ creates an array for the responsive pots. It gets filled in the Setup.<\/p>\n<p>int potMin = 10;<br \/>\nint potMax = 1023;<\/p>\n<p>#endif<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ MIDI<br \/>\nbyte midiCh = 0;  \/\/ MIDI channel to be used &#8211; start with 1 for MIDI.h lib or 0 for MIDIUSB lib<br \/>\nbyte note = 36;   \/\/ Lowest note to be used<br \/>\nbyte cc = 1;      \/\/ Lowest MIDI CC to be used<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ SETUP<br \/>\nvoid setup() {<\/p>\n<p>  \/\/ Baud Rate<br \/>\n  \/\/ use if using with ATmega328 (uno, mega, nano&#8230;)<br \/>\n  \/\/ 31250 for MIDI class compliant | 115200 for Hairless MIDI<br \/>\n  Serial.begin(115200);  \/\/<\/p>\n<p>#ifdef DEBUG<br \/>\n  Serial.println(&#8220;Debug mode&#8221;);<br \/>\n  Serial.println();<br \/>\n#endif<\/p>\n<p>#ifdef USING_BUTTONS<br \/>\n  \/\/ Buttons<br \/>\n  \/\/ Initialize buttons with pull up resistors<br \/>\n  for (int i = 0; i < N_BUTTONS; i++) {\n    pinMode(BUTTON_ARDUINO_PIN[i], INPUT_PULLUP);\n  }\n\n#ifdef pin13  \/\/ initialize pin 13 as an input\n  pinMode(BUTTON_ARDUINO_PIN[pin13index], INPUT);\n#endif\n\n#endif\n\n#ifdef USING_POTENTIOMETERS\n  for (int i = 0; i < N_POTS; i++) {\n    responsivePot[i] = ResponsiveAnalogRead(0, true, snapMultiplier);\n    responsivePot[i].setAnalogResolution(1023);  \/\/ sets the resolution\n  }\n#endif\n}\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ LOOP\nvoid loop() {\n\n#ifdef USING_BUTTONS\n  buttons();\n#endif\n\n#ifdef USING_POTENTIOMETERS\n  potentiometers();\n#endif\n}\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ BUTTONS\n#ifdef USING_BUTTONS\n\nvoid buttons() {\n\n  for (int i = 0; i < N_BUTTONS; i++) {\n\n    buttonCState[i] = digitalRead(BUTTON_ARDUINO_PIN[i]);  \/\/ read pins from arduino\n\n#ifdef pin13\n    if (i == pin13index) {\n      buttonCState[i] = !buttonCState[i];  \/\/ inverts the pin 13 because it has a pull down resistor instead of a pull up\n    }\n#endif\n\n    if ((millis() - lastDebounceTime[i]) > debounceDelay) {<\/p>\n<p>      if (buttonPState[i] != buttonCState[i]) {<br \/>\n        lastDebounceTime[i] = millis();<\/p>\n<p>        if (buttonCState[i] == LOW) {<\/p>\n<p>          \/\/ Sends the MIDI note ON accordingly to the chosen board<br \/>\n#ifdef ATMEGA328<br \/>\n          \/\/ use if using with ATmega328 (uno, mega, nano&#8230;)<br \/>\n          MIDI.sendNoteOn(note + i, 127, midiCh);  \/\/ note, velocity, channel<\/p>\n<p>#elif ATMEGA32U4<br \/>\n          \/\/ use if using with ATmega32U4 (micro, pro micro, leonardo&#8230;)<br \/>\n          noteOn(midiCh, note + i, 127);  \/\/ channel, note, velocity<br \/>\n          MidiUSB.flush();<\/p>\n<p>#elif TEENSY<br \/>\n          \/\/do usbMIDI.sendNoteOn if using with Teensy<br \/>\n          usbMIDI.sendNoteOn(note + i, 127, midiCh);  \/\/ note, velocity, channel<\/p>\n<p>#elif DEBUG<br \/>\n          Serial.print(i);<br \/>\n          Serial.println(&#8220;: button on&#8221;);<br \/>\n#endif<\/p>\n<p>        } else {<\/p>\n<p>          \/\/ Sends the MIDI note OFF accordingly to the chosen board<br \/>\n#ifdef ATMEGA328<br \/>\n          \/\/ use if using with ATmega328 (uno, mega, nano&#8230;)<br \/>\n          MIDI.sendNoteOn(note + i, 0, midiCh);  \/\/ note, velocity, channel<\/p>\n<p>#elif ATMEGA32U4<br \/>\n          \/\/ use if using with ATmega32U4 (micro, pro micro, leonardo&#8230;)<br \/>\n          noteOn(midiCh, note + i, 0);  \/\/ channel, note, velocity<br \/>\n          MidiUSB.flush();<\/p>\n<p>#elif TEENSY<br \/>\n          \/\/do usbMIDI.sendNoteOn if using with Teensy<br \/>\n          usbMIDI.sendNoteOn(note + i, 0, midiCh);  \/\/ note, velocity, channel<\/p>\n<p>#elif DEBUG<br \/>\n          Serial.print(i);<br \/>\n          Serial.println(&#8220;: button off&#8221;);<br \/>\n#endif<br \/>\n        }<br \/>\n        buttonPState[i] = buttonCState[i];<br \/>\n      }<br \/>\n    }<br \/>\n  }<br \/>\n}<\/p>\n<p>#endif<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\/\/ POTENTIOMETERS<br \/>\n#ifdef USING_POTENTIOMETERS<\/p>\n<p>void potentiometers() {<\/p>\n<p>  for (int i = 0; i < N_POTS; i++) {  \/\/ Loops through all the potentiometers\n\n    reading = analogRead(POT_ARDUINO_PIN[i]);\n    responsivePot[i].update(reading);\n    potCState[i] = responsivePot[i].getValue();\n\n    potCState[i] = analogRead(POT_ARDUINO_PIN[i]);  \/\/ reads the pins from arduino\n\n    midiCState[i] = map(potCState[i], potMin, potMax, 0, 127);  \/\/ Maps the reading of the potCState to a value usable in midi\n    \/\/midiCState[i] = map(potCState[i], 0, 4096, 0, 127);  \/\/ Maps the reading of the potCState to a value usable in midi - use for ESP32\n\n    if (midiCState[i] < 0) {\n      midiCState[i] = 0;\n    }\n    if (midiCState[i] > 127) {<br \/>\n      midiCState[i] = 0;<br \/>\n    }<\/p>\n<p>    potVar = abs(potCState[i] &#8211; potPState[i]);  \/\/ Calculates the absolute value between the difference between the current and previous state of the pot<br \/>\n    \/\/Serial.println(potVar);<\/p>\n<p>    if (potVar > varThreshold) {  \/\/ Opens the gate if the potentiometer variation is greater than the threshold<br \/>\n      PTime[i] = millis();        \/\/ Stores the previous time<br \/>\n    }<\/p>\n<p>    timer[i] = millis() &#8211; PTime[i];  \/\/ Resets the timer 11000 &#8211; 11000 = 0ms<\/p>\n<p>    if (timer[i] < TIMEOUT) {  \/\/ If the timer is less than the maximum allowed time it means that the potentiometer is still moving\n      potMoving = true;\n    } else {\n      potMoving = false;\n    }\n\n    if (potMoving == true) {  \/\/ If the potentiometer is still moving, send the change control\n      if (midiPState[i] != midiCState[i]) {\n\n        \/\/ Sends the MIDI CC accordingly to the chosen board\n#ifdef ATMEGA328\n        \/\/ use if using with ATmega328 (uno, mega, nano...)\n        MIDI.sendControlChange(cc + i, midiCState[i], midiCh);  \/\/ cc number, cc value, midi channel\n\n#elif ATMEGA32U4\n        \/\/use if using with ATmega32U4 (micro, pro micro, leonardo...)\n        controlChange(midiCh, cc + i, midiCState[i]);  \/\/  (channel, CC number,  CC value)\n        MidiUSB.flush();\n\n#elif TEENSY\n        \/\/do usbMIDI.sendControlChange if using with Teensy\n        usbMIDI.sendControlChange(cc + i, midiCState[i], midiCh);  \/\/ cc number, cc value, midi channel\n\n#elif DEBUG\n        Serial.print(\"Pot: \");\n        Serial.print(i);\n        Serial.print(\" \");\n        Serial.println(midiCState[i]);\n\/\/Serial.print(\"  \");\n#endif\n\n        potPState[i] = potCState[i];  \/\/ Stores the current reading of the potentiometer to compare with the next\n        midiPState[i] = midiCState[i];\n      }\n    }\n  }\n}\n\n#endif\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ if using with ATmega32U4 (micro, pro micro, leonardo...)\n#ifdef ATMEGA32U4\n\n\/\/ Arduino (pro)micro midi functions MIDIUSB Library\nvoid noteOn(byte channel, byte pitch, byte velocity) {\n  midiEventPacket_t noteOn = { 0x09, 0x90 | channel, pitch, velocity };\n  MidiUSB.sendMIDI(noteOn);\n}\n\nvoid noteOff(byte channel, byte pitch, byte velocity) {\n  midiEventPacket_t noteOff = { 0x08, 0x80 | channel, pitch, velocity };\n  MidiUSB.sendMIDI(noteOff);\n}\n\nvoid controlChange(byte channel, byte control, byte value) {\n  midiEventPacket_t event = { 0x0B, 0xB0 | channel, control, value };\n  MidiUSB.sendMIDI(event);\n}\n#endif\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* Made by Gustavo Silveira, 2023. &#8211; This Sketch reads the Arduino&#8217;s digital [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-545","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=\/wp\/v2\/posts\/545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=545"}],"version-history":[{"count":3,"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=\/wp\/v2\/posts\/545\/revisions"}],"predecessor-version":[{"id":548,"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=\/wp\/v2\/posts\/545\/revisions\/548"}],"wp:attachment":[{"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.dreid.ch\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}