Joyfultalk
Member
Hi there, I was successfully able to control 5 motors using midi notes sequenced with a midi sequencer using the teensy lc board. The only problem I'm having is that when 2 or more midi notes are transmitted to the teensy at the same time they seem to cancel each other out and 0 voltage is sent to the motors. It seems like a polyphony problem in the code. I was wondering what else I need to add to the code and where I need to add it to fix this problem.
Code
// 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
}
}
Code
// 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
}
}