Joyfultalk
Member
Hi, I'm working on a project to transmit midi to control some dc motors. I have successfully been able to do this which is great. The problem I'm having is the teensy is receiving all midi channels and I won't the teensy to only receive one midi channel at a time, ie. channel 16 only. What do I need to add to the code to be able to do this. Here is the sketch I'm using
// use PWM pins 3, 4, 6, 9, 10
int pins[] = {3, 4, 6, 9, 10};
#define numNotes 5
// motor channel 0 will be triggered by lowNote
// motor channel 1 will be triggered by lowNote + 1 etc.
int lowNote = 60; // MIDI note 60 (C-2) // change to use different note triggers
// Teensy MIDI callbacks
void OnNoteOn(byte channel, byte note, byte velocity);
void OnNoteOff(byte channel, byte note, byte velocity);
// initialization
void setup() {
// output pins
for (int i = 0; i < 5; i++) {
pinMode(pins, OUTPUT);
}
// init MIDI callbacks
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleNoteOff(OnNoteOff);
}
// main loop
void loop() {
while (usbMIDI.read()) {
}
// nothing to do here...
}
// OnNoteOn() callback
// When a new "note on" message is recieved
void OnNoteOn(byte channel, byte note, byte velocity) {
// check if the note is in range for our possible motor outputs
if (note >= lowNote && note < lowNote + numNotes) {
// adjust for note # offset
int notePin = note - lowNote;
// linear map from MIDI's 7 bits of resolution to Teensy's 12
analogWrite(pins[notePin], velocity << 5 );
}
}
// OnNoteOff() callback
// When a new "note off" message is received
void OnNoteOff(byte channel, byte note, byte velocity) {
// check that it is in the range
if (note >= lowNote && note < lowNote + numNotes) {
int notePin = note - lowNote; // offset
analogWrite(pins[notePin], 0); // turn it off
}
}
// use PWM pins 3, 4, 6, 9, 10
int pins[] = {3, 4, 6, 9, 10};
#define numNotes 5
// motor channel 0 will be triggered by lowNote
// motor channel 1 will be triggered by lowNote + 1 etc.
int lowNote = 60; // MIDI note 60 (C-2) // change to use different note triggers
// Teensy MIDI callbacks
void OnNoteOn(byte channel, byte note, byte velocity);
void OnNoteOff(byte channel, byte note, byte velocity);
// initialization
void setup() {
// output pins
for (int i = 0; i < 5; i++) {
pinMode(pins, OUTPUT);
}
// init MIDI callbacks
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleNoteOff(OnNoteOff);
}
// main loop
void loop() {
while (usbMIDI.read()) {
}
// nothing to do here...
}
// OnNoteOn() callback
// When a new "note on" message is recieved
void OnNoteOn(byte channel, byte note, byte velocity) {
// check if the note is in range for our possible motor outputs
if (note >= lowNote && note < lowNote + numNotes) {
// adjust for note # offset
int notePin = note - lowNote;
// linear map from MIDI's 7 bits of resolution to Teensy's 12
analogWrite(pins[notePin], velocity << 5 );
}
}
// OnNoteOff() callback
// When a new "note off" message is received
void OnNoteOff(byte channel, byte note, byte velocity) {
// check that it is in the range
if (note >= lowNote && note < lowNote + numNotes) {
int notePin = note - lowNote; // offset
analogWrite(pins[notePin], 0); // turn it off
}
}