====== Teensy4Malinette ====== * Porteur du projet : fenshu * Date : 09 > 12/2014 * Licence : libre ! * Contexte : Développement pour le projet [[:projets:malinette:]] * Fichiers : ... * Lien : ... ===== Description ===== {{ :projets:teensy4malinette:img_20141023_114620.jpg?600 |}} **Utiliser Teensy 2.0 en mode hid (joystick) ou midi** \\ L'objectif est de proposer une carte avec des capteurs interchangeables qui soit reconnue facilement par les ordinateurs, pour éviter l'installation de drivers ou logiciel arduino... les protocole HID et MIDI sont plutot reconnu facilement par nos ordinateurs sans drivers. ===== Matériaux ===== * Carte teensy (2.0) * Capteurs * Pure-data ou pas (en mode MIDI) ===== Installation ===== * [[https://www.pjrc.com/teensy/td_download.html]] ===== Mode HID joystick ===== - Choisir mouse,keyboard, joystick dans Tools > USB Type - Ouvrir l'exemple File > Examples > Teensy > USB_Joystick > Complete [[https://www.pjrc.com/teensy/td_joystick.html]] **Les Entrées analogiques : route dans pd-extended** \\ Ouvrir l'aide de l'objet Hid et cliquer sur bang pour connaitre le n° de port DEVICE de la carte teensy, ensuite cliquer start et les valeurs bougeront... si tout va bien... {{ :projets:teensy4malinette:screen_shot_10-23-14_at_11.52_am.png |}} * F1 = 4 * ... * ... * ... * :!: **Attention Hid ne parait pas stable.**.. trop d'info ? Interval a mettre resample ? ===== Mode MIDI ===== L'objectif est de creer une sorte de firmata pour teensy 2.0 en midi... afin de permettre son utilisation dans la malinette ainsi que dans d'autres soft musicaux.... {{ :projets:teensy4malinette:wiring_pinout2h.png |}} **Note :** la pin 4 en pwm ne marche pas .... ??? \\ A télécharger : {{:projets:teensy4malinette:in_out_teensy.pd|patch pd qui envoie et recoit du midi}}. **Code Arduino** pour configurer les pins de la teensy en midi ++++ teensy4malinette.ino (midi) | // PIN CONFIGURATION : //------------------------------------------------------------------------------------------ // 5 analog input : a0,a1,a2,a3,a4 (21,20,19,18,17) CCIN : 10 11 12 13 14 // 6 digital input : 16,15,14,13,12,11 NOTEIN : 60 61 62 63 64 65 // OUT ::: // 4 digital 0 1 2 3 NOTEOUT : 0 1 2 3 4 // 4 pwm : 4 5 9 10 (warning only teensy pwm pin = 4,5,9,10,12,14,15) CCOUT:102 103 104 105 // 3 servo : 6 7 8 CCOUT:106 107 108 // !! COMMENT : pin 4 pwm marche pas ! digout allumé au debut...? // MIDI CHANNEL : 1 // ---------------------- #include #include //servo library // the MIDI channel number to send messages const int channel = 1; //analog in // the MIDI continuous controller for each analog input const int controllerA0 = 10; //CCin n° const int controllerA1 = 11; const int controllerA2 = 12; const int controllerA3 = 13; const int controllerA4 = 14; // store previously sent values, to detect changes int previousA0 = -1; int previousA1 = -1; int previousA2 = -1; int previousA3 = -1; int previousA4 = -1; elapsedMillis msec = 0; // digital in Bounce button0 = Bounce(16, 5); Bounce button1 = Bounce(15, 5); // 5 = 5 ms debounce time Bounce button2 = Bounce(14, 5); // which is appropriate for good Bounce button3 = Bounce(13, 5); // quality mechanical pushbuttons Bounce button4 = Bounce(12, 5); Bounce button5 = Bounce(11, 5); // DIGITAL OUT int digout1 = 0; //pin definition int digout2 = 1; int digout3 = 2; int digout4 = 3; int valdigout1 = 0; int valdigout2 = 0; int valdigout3 = 0; int valdigout4 = 0; // PWM int pwm1 = 4; //pin definition int pwm2 = 5; int pwm3 = 9; int pwm4 = 10; int valpwm1 = 0; // variable to store the pwm int valpwm2 = 0; int valpwm3 = 0; int valpwm4 = 0; //servo Servo myservo1; // create servo object to control a servo : a maximum of eight servo objects can be created Servo myservo2; Servo myservo3; int pos1 = 0; // variable to store the servo position int pos2 = 0; int pos3 = 0; // =============SETUPPPPPP ============================= void setup() { //digital IN BUTTON pinMode(16, INPUT_PULLUP); pinMode(15, INPUT_PULLUP); pinMode(14, INPUT_PULLUP); pinMode(13, INPUT_PULLUP); pinMode(12, INPUT_PULLUP); pinMode(11, INPUT_PULLUP); usbMIDI.setHandleControlChange(OnControlChange); //servo myservo1.attach(6); // attaches the servo on pin 20 (F1) myservo2.attach(7); // attaches the servo on pin 20 (F1) myservo3.attach(8); // attaches the servo on pin 20 (F1) usbMIDI.setHandleNoteOn(OnNoteOn); //digitalout pinMode(digout1, OUTPUT); pinMode(digout2, OUTPUT); pinMode(digout3, OUTPUT); pinMode(digout4, OUTPUT); // pwm pinMode(pwm1, OUTPUT); pinMode(pwm2, OUTPUT); pinMode(pwm3, OUTPUT); pinMode(pwm4, OUTPUT); } // ==========LOOOOOOPP ============================= void loop() { // pour ecouter le midi pour les sorties usbMIDI.read(1); //update button (digital in) button0.update(); button1.update(); button2.update(); button3.update(); button4.update(); button5.update(); if (button0.fallingEdge()) { usbMIDI.sendNoteOn(60, 99, channel); // 60 = C4 } if (button1.fallingEdge()) { usbMIDI.sendNoteOn(61, 99, channel); // 61 = C#4 } if (button2.fallingEdge()) { usbMIDI.sendNoteOn(62, 99, channel); // 62 = D4 } if (button3.fallingEdge()) { usbMIDI.sendNoteOn(63, 99, channel); // 63 = D#4 } if (button4.fallingEdge()) { usbMIDI.sendNoteOn(64, 99, channel); // 64 = E4 } if (button5.fallingEdge()) { usbMIDI.sendNoteOn(65, 99, channel); // 64 = f4 } //------------ if (button0.risingEdge()) { usbMIDI.sendNoteOff(60, 0, channel); // 60 = C4 } if (button1.risingEdge()) { usbMIDI.sendNoteOff(61, 0, channel); // 61 = C#4 } if (button2.risingEdge()) { usbMIDI.sendNoteOff(62, 0, channel); // 62 = D4 } if (button3.risingEdge()) { usbMIDI.sendNoteOff(63, 0, channel); // 63 = D#4 } if (button4.risingEdge()) { usbMIDI.sendNoteOff(64, 0, channel); // 64 = E4 } if (button5.risingEdge()) { usbMIDI.sendNoteOff(65, 0, channel); // 65 = f4 } // only check the analog inputs 50 times per second, // to prevent a flood of MIDI messages if (msec >= 20) { msec = 0; int n0 = analogRead(A0) / 8; int n1 = analogRead(A1) / 8; int n2 = analogRead(A2) / 8; int n3 = analogRead(A3) / 8; int n4 = analogRead(A4) / 8; // only transmit MIDI messages if analog input changed if (n0 != previousA0) { usbMIDI.sendControlChange(controllerA0, n0, channel); previousA0 = n0; } if (n1 != previousA1) { usbMIDI.sendControlChange(controllerA1, n1, channel); previousA1 = n1; } if (n2 != previousA2) { usbMIDI.sendControlChange(controllerA2, n2, channel); previousA2 = n2; } if (n3 != previousA3) { usbMIDI.sendControlChange(controllerA3, n3, channel); previousA3 = n3; } if (n4 != previousA4) { usbMIDI.sendControlChange(controllerA4, n4, channel); previousA4 = n4; } } // MIDI Controllers should discard incoming MIDI messages. // http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash while (usbMIDI.read()) { // ignore incoming messages } } // mapping NOTEOUT ON : digitalout void OnNoteOn(byte channel, byte note, byte velocity) { Serial.print("Note On, ch="); Serial.print(channel, DEC); Serial.print(", note="); Serial.print(note, DEC); Serial.print(", velocity="); Serial.print(velocity, DEC); Serial.println(); if (note== 0) { int valdigout1=velocity/127-1; digitalWrite(digout1, valdigout1); } if (note== 1) { int valdigout2=velocity/127-1; digitalWrite(digout2, valdigout2); } if (note== 2) { int valdigout3=velocity/127-1; digitalWrite(digout3, valdigout3); } if (note== 3) { int valdigout4=velocity/127-1; digitalWrite(digout4, valdigout4); } } // mapping CC OUT :: PWM & SERVO void OnControlChange(byte channel, byte control, byte value) { //PWM if (control== 102) { int valpwm1=map(value, 1, 127, 0, 255); analogWrite(pwm1, valpwm1); } if (control== 103) { int valpwm2=map(value, 1, 127, 0, 255); analogWrite(pwm2, valpwm2); } if (control== 104) { int valpwm3=map(value, 1, 127, 0, 255); analogWrite(pwm3, valpwm3); } if (control== 105) { int valpwm4=map(value, 1, 127, 0, 255); analogWrite(pwm4, valpwm4); } //SERVO if (control== 106) { int pos1=map(value, 1, 127, 0, 180); myservo1.write(pos1); } if (control== 107) { int pos2=map(value, 1, 127, 0, 180); myservo2.write(pos2); } if (control== 108) { int pos3=map(value, 1, 127, 0, 180); myservo3.write(pos3); } } ++++ ==== Servo midi ==== Controler un servo en midi ++++ teensy4malinette.ino (servo) | #include Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; // variable to store the servo position void setup() { myservo.attach(20); // attaches the servo on pin 20 (F1) usbMIDI.setHandleNoteOn(myNoteOn); } void loop() { usbMIDI.read(1); } void myNoteOn(byte channel, byte note, byte velocity) { int pos=map(note, 1, 127, 0, 180); myservo.write(pos); } ++++ ===== Idée ===== idée de shield idéal pour une malinette avec 3in 3out... {{:projets:malinette:teensy4malinette:ecran518.png?nolink|}} {{tag>[teensy malinette]}}