Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Include the Control Surface library
- #include <Control_Surface.h>
- USBMIDI_Interface midi;
- #define TOLERANCE 25
- typedef struct {
- MIDIAddress note;
- uint8_t analogIn;
- uint8_t tolerance = TOLERANCE;
- bool playing = false;
- } JoyNote;
- typedef struct {
- MIDIAddress controller;
- uint8_t analogIn;
- uint8_t tolerance = TOLERANCE;
- uint16_t lastSent;
- } JoyController;
- #define UP_BUTTON 2
- #define DOWN_BUTTON 3
- #define NUM_NOTES 7
- #define NUM_CONTROLLERS 3
- JoyNote joyNotes[NUM_NOTES];
- JoyController joyControl[NUM_CONTROLLERS];
- using namespace MIDI_Notes;
- // The velocity of the note events
- const uint8_t velocity = 0x7F;
- // Variabile per cambiare ottava.
- int actualOctave = 3;
- #define MIN_OCTAVE 2
- #define MAX_OCTAVE 6
- void setup() {
- //Serial.begin(115200);
- //while (!Serial) ;
- pinMode(UP_BUTTON, INPUT_PULLUP);
- pinMode(DOWN_BUTTON, INPUT_PULLUP);
- initNotesAndControllers();
- midi.begin();
- }
- // ---------------------------------- Loop ---------------------------------- //
- // ========================================================================== //
- void loop() {
- midi.update(); // read and handle or discard MIDI input
- // Inviamo il messaggio per le note, se necessario
- // for(uint8_t i=0; i<NUM_NOTES; i++){
- for(uint8_t i=0; i<3; i++){ // SOLO PER TEST
- uint16_t analogVal = abs(512 - analogRead(joyNotes[i].analogIn));
- uint8_t tolValue = joyNotes[i].tolerance;
- // Joypad azionato e la nota deve essere ancora riprodotta
- if(analogVal >= tolValue && !joyNotes[i].playing ){
- midi.sendNoteOn(joyNotes[i].note, velocity);
- joyNotes[i].playing = true;
- }
- // Joypad in posizione di riposo e la nota non deve essere più riprodotta
- if(analogVal < tolValue && joyNotes[i].playing) {
- midi.sendNoteOff(joyNotes[i].note, velocity);
- joyNotes[i].playing = false;
- }
- }
- // Inviamo il messaggio per i controller, se necessario
- //for(uint8_t i=0; i<NUM_CONTROLLERS; i++){
- for(uint8_t i=0; i<2; i++){ // SOLO PER TEST
- uint16_t analogVal = abs(512 - analogRead(joyControl[i].analogIn));
- uint8_t tolValue = joyControl[i].tolerance;
- uint8_t midiValue = map(analogVal, 0, 512, 0 ,127);
- // Joypad azionato, inviamo il messaggio MIDI (una sola volta)
- if(analogVal >= tolValue && joyControl[i].lastSent != midiValue ){
- midi.sendCC(joyControl[i].controller, midiValue );
- joyControl[i].lastSent = midiValue;
- }
- }
- // Incremento-decremento dell'ottava (per un'escursione totale di NUM_OCTAVES - 1 ottave)
- static uint32_t upDebounce, downDebounce;
- if(digitalRead(UP_BUTTON) == LOW && millis() - upDebounce > 250){
- upDebounce = millis();
- if(actualOctave < MAX_OCTAVE){
- actualOctave++ ;
- updateOctave(actualOctave);
- }
- }
- if(digitalRead(DOWN_BUTTON) == LOW && millis() - downDebounce > 250){
- downDebounce = millis();
- if(actualOctave > MIN_OCTAVE){
- actualOctave-- ;
- updateOctave(actualOctave);
- }
- }
- }
- /*
- static const uint8_t A6 = 24; // D4
- static const uint8_t A7 = 25; // D6
- static const uint8_t A8 = 26; // D8
- static const uint8_t A9 = 27; // D9
- static const uint8_t A10 = 28; // D10
- static const uint8_t A11 = 29; // D12
- */
- // {0, 1, 2, 3, 4, 5, 6 }
- enum {Do3, Mi3, Sol3, Fa3, Si4, Re4, La4};
- void initNotesAndControllers(){
- joyNotes[Do3].note = {note(C, octave), CHANNEL_1};
- joyNotes[Do3].analogIn = A0;
- // E' necessario assegnare solo se diversa da quella di default
- // Es. per correggere singolo joypad "sfasato"
- joyNotes[Do3].tolerance = TOLERANCE + 1; // +- single adjustment
- joyNotes[Mi3].note = {note(E, actualOctave), CHANNEL_1};
- joyNotes[Mi3].analogIn = A1;
- joyNotes[Sol3].note = {note(G, actualOctave), CHANNEL_1};
- joyNotes[Sol3].analogIn = A2;
- joyNotes[Fa3].note = {note(F, actualOctave), CHANNEL_1};
- joyNotes[Fa3].analogIn = A3;
- joyNotes[Si4].note = {note(B, actualOctave +1), CHANNEL_1};
- joyNotes[Si4].analogIn = A4;
- joyNotes[Re4].note = {note(D, actualOctave +1), CHANNEL_1};
- joyNotes[Re4].analogIn = A5;
- joyNotes[La4].note = {note(A, actualOctave +1), CHANNEL_1};
- joyNotes[La4].analogIn = A6;
- joyControl[0].controller = {MIDI_CC::General_Purpose_Controller_1, CHANNEL_1};
- joyControl[0].analogIn = A7;
- joyControl[0].tolerance = 10;
- joyControl[1].controller = {MIDI_CC::General_Purpose_Controller_2, CHANNEL_1};
- joyControl[1].analogIn = A8;
- joyControl[1].tolerance = 10;
- joyControl[2].controller = {MIDI_CC::General_Purpose_Controller_3, CHANNEL_1};
- joyControl[2].analogIn = A9;
- joyControl[2].tolerance = 10;
- }
- void updateOctave( uint8_t octave){
- joyNotes[Do3].note = {note(C, octave), CHANNEL_1};
- joyNotes[Mi3].note = {note(E, octave), CHANNEL_1};
- joyNotes[Sol3].note = {note(G, octave), CHANNEL_1};
- joyNotes[Fa3].note = {note(F, octave), CHANNEL_1};
- joyNotes[Si4].note = {note(B, octave+1), CHANNEL_1};
- joyNotes[Re4].note = {note(D, octave+1), CHANNEL_1};
- joyNotes[La4].note = {note(A, octave+1), CHANNEL_1};
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement