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….
// 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 <Bounce.h>
#include <Servo.h> //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);
}
}