trouble sending usbMIDI IN to Serial OUT

Status
Not open for further replies.

charbot

Member
Hi- I built a midi controller using the teensy 2.0 and am having trouble reading usbMIDI in and echoing it over to the Serial1.
This is what I have been trying (in many variations) :
Code:
  if (usbMIDI.read() > 1)
    {
      inByte= usbMIDI.read();  
        Serial1.write(inByte);
    }
None seemed to worked so I tried this, which I got/modified this from arduino midi library sketch-
Code:
if (usbMIDI.read())
    {
        
        Serial1.write(usbMIDI.getType(),
                   usbMIDI.getData1(),
                   usbMIDI.getData2(),
                   usbMIDI.getChannel());
    }
- doesnt compile. so I tried something like this:
Code:
if (usbMIDI.read() > 1)
    {
        // Thru on A has already pushed the input message to out A.
        // Forward the message to out B as well.
        inByte=usbMIDI.getType();
        Serial1.write(inByte);
                   
         inByte=usbMIDI.getData1();
          Serial1.write(inByte);         
         inByte=usbMIDI.getData2();
           inByte=usbMIDI.getChannel(),
           Serial1.write(inByte);        
    }
- compiles but doesnt work

Sending usbMIDI OUT works perfectly in my controller code and the 'Blink w/ NoteOn' demo sketch works fine, so I know that hardware/ cables are good and usb midi is being received.
Seems like a fair straight forward thing...Clearly im missing something.
thanks in advance

here is the complete code, just in case:
Code:
// control pins output table in array form
// connect 74HC4067 S0~S3 to:     PortA =Arduino Mega D29-D26;   PortD= UNO- D7~D4; and Teensy- D22,D23,D12,D11 ...... respectively
const byte CCnums [] PROGMEM {16,21,17,22,18,23,24,19,25,20,
                              26,31,27,32,28,33,34,29,35,30,
                              36,41,37,42,38,43,44,39,45,40,
                              46,51,47,52,48,53,54,49,55,50,
                              59,58,57,56,63,62,60,61,64,65,66,67,
                              70,68,69,71,72,74,73};  

byte CCvals [59] {};
byte SavedCCvals [59] {};


#include <EEPROM.h>
#include <Wire.h>
//#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

// midi globals
byte CHANNEL= EEPROM.read ( 24 );  
#define CC    0xB0
byte inByte = 0;
byte cmd =0;
byte lastCmd =0; // for running status, do'y send command if same as last  time
byte messageCnt = 0;
byte ctrlSend = 0 ;

bool note=0;
byte velocity=0;
byte velocityDest=0;
//byte aftertouchDest;
byte LFOwave;
byte algorithm;
byte incomingMidiByte;

byte previousMillis = 0;
byte preset = 0;


const char Saw [] {"Saw" };
const char Squ [] {"Square"};
const char Tri [] {"Triangle"};
const char Noi [] {"Noise"};
const char *LFOwaveShape [] {Saw, Squ, Tri, Noi};

#define MUXSEL0  12
#define MUXSEL1  11
#define MUXSEL2  23
#define MUXSEL3  22

const byte controlPins[] PROGMEM = {B00000000, 
                                    B10000000,
                                    B01000000,
                                    B11000000,
                                    B00100000,
                                    B10100000,
                                    B01100000,
                                    B11100000,
                                    B00010000,
                                    B10010000,
                                    B01010000,
                                    B11010000,
                                    B00110000,
                                    B10110000,
                                    B01110000,
                                    B11110000 }; 

byte buttonState5[16] = {0};        // current state of the button
boolean lastButtonState5[16] = {0};
byte buttonCounter5[16] = {1};
byte buttonState6[16] = {0};        // current state of the button
boolean lastButtonState6[16] = {0};
boolean buttonCounter6[16] = {0};
 
byte muxValues1[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};// holds incoming values from 74HC4067  
byte prevMuxValues1[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; 
byte muxValues2[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
byte prevMuxValues2[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; 
byte muxValues3[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; 
byte prevMuxValues3[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; 
byte muxValues4[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
byte prevMuxValues4[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; 
byte muxValues5[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; 
byte prevMuxValues5[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; 
byte muxValues6[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};
byte prevMuxValues6[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,}; 

///////               MUX channel = 0  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15 
const byte CCBankOP1[]  PROGMEM = { 0, 0, 16, 21, 17, 22, 18, 23, 24, 19, 25, 20,  0,  0,  0,  0}; 
const byte CCBankOP2[]  PROGMEM = { 0, 0, 26, 31, 27, 32, 28, 33, 34, 29, 35, 30,  0,  0,  0,  0}; 
const byte CCBankOP3[]  PROGMEM = { 0, 0, 36, 41, 37, 42, 38, 43, 44, 39, 45, 40,  0,  0,  0,  0}; 
const byte CCBankOP4[]  PROGMEM = { 0, 0, 46, 51, 47, 52, 48, 53, 54, 49, 55, 50,  0,  0,  0,  0}; 
const byte CCBankCOM[]  PROGMEM = { 0, 0, 59, 58, 57, 56, 63, 62, 60, 61, 64, 65, 66, 67,  0,  0};
const byte CCBankLFO[]  PROGMEM = {74, 0,  0,  0,  0, 73,  0,  0,  0,  0, 70, 68, 69, 71, 72, 76}; 
const byte ledPin5[] PROGMEM =     {0, 0,  3, 24,  4, 10,  9,  0,  2,  1,  0,  0,0,0,0,0,}; //pins that control button LEDs
const byte ledPin6[] PROGMEM =    {14, 0,  0,  0,  0, 15,  0,  0,  0,  0,  0,  0,0,0,0,0,}; 
 
typedef struct {
  uint8_t status;   // first  byte   : status message (NOTE_ON, NOTE_OFF or CC (controlchange) and midi channel (0-15)
  uint8_t data1;    // second byte   : first value (0-127), controller number or note number
  uint8_t data2;    // third  byte   : second value (0-127), controller value or velocity
} 
t_midiMsg;          // We call this data type 't_midiMsg'

//////////////////////////////////////////////////////////////////////////////////////////


void setup(){
  Serial1.begin(31250);
 
 pinMode(MUXSEL0,OUTPUT);
  pinMode(MUXSEL1,OUTPUT);
  pinMode(MUXSEL2,OUTPUT);
  pinMode(MUXSEL3,OUTPUT);
  
  pinMode (15, OUTPUT);
  pinMode (14, OUTPUT);
  pinMode (10, OUTPUT);
  pinMode (4, OUTPUT);
  pinMode (24, OUTPUT);
  pinMode (3, OUTPUT);
  pinMode (2, OUTPUT);
  pinMode (1, OUTPUT);
  pinMode (0, OUTPUT);
  pinMode (9, OUTPUT);  
  pinMode (A0,INPUT); 
  pinMode (A5,INPUT);
  
  delay(100); // needed for OLED on teensy
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64
  display.display();      // Show image buffer on the display hardware.
  display.invertDisplay(true);
 display.clearDisplay();   // Clear the buffer.
  
  for (byte i = 2; i < 10; i++){ 
    if ( EEPROM.read ( i-2 ) != 0xff ){            // If Eeprom slot is not blank...
      buttonCounter5[i] = EEPROM.read(i-2);      // Remember Button States from when it was last powered
      }
   }  
  for (byte i = 0; i < 16; i++){ 
    if ( EEPROM.read ( i+8 ) != 0xff ){            // If Eeprom slot is not blank...
      buttonCounter6[i] = EEPROM.read(i+8);      // Remember Button States from when it was last powered
      }
   } 
delay(1000);
display.invertDisplay(false);
delay(100);
display.invertDisplay(true);
delay(100);
display.invertDisplay(false);
delay(100);
display.invertDisplay(true);
delay(100);
display.invertDisplay(false);
delay(100);
display.invertDisplay(true);
delay(100);
display.invertDisplay(false);
delay(100);
display.invertDisplay(true);
delay(100);
display.invertDisplay(false);

ReadPots();
MidiSendAll();    
}
 
//////////////////////////////////////////////////////////////////////////////////////////


void setPin(int outputPin){   // function to select pin on 74HC4067
  byte b = pgm_read_byte_near(controlPins + outputPin);

  if (b & 0x80) digitalWrite(MUXSEL0,HIGH);
  else digitalWrite(MUXSEL0,LOW);
  if (b & 0x40) digitalWrite(MUXSEL1,HIGH);
  else digitalWrite(MUXSEL1,LOW);
  if (b & 0x20) digitalWrite(MUXSEL2,HIGH);
  else digitalWrite(MUXSEL2,LOW);
  if (b & 0x10) digitalWrite(MUXSEL3,HIGH);
  else digitalWrite(MUXSEL3,LOW);
}
 
//////////////////////////////////////////////////////////////////////////////////////////


void loop(){
  
  MidiMerge();
  usbMidiMerge();
  
  unsigned long currentMillis = millis();  //check the pots only every x millis
  if (currentMillis - previousMillis >= 100) {
     previousMillis = currentMillis;
     ReadAndSend();
  } 
} 
//////////////////////////////////////////////////////////////////////////////////////////
void ReadAndSend(){
  for (byte i = 2; i < 12; i++){
      setPin(i); // choose an input pin on the 74HC4067
      muxValues1[i]=map(analogRead(A2), 0,1024,0,128); // read the vlaue on that pin and store in array
      muxValues2[i]=map(analogRead(A1), 0,1024,0,128);
      muxValues3[i]=map(analogRead(A4), 0,1024,0,128); 
      muxValues4[i]=map(analogRead(A3), 0,1024,0,128);
      
      if (muxValues1[i] != prevMuxValues1[i]){
        t_midiMsg msg;
        msg.status = CC;                              //For OP boards #1-4 
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankOP1 + i);                  // Get the controller number from the array above.
        msg.data2   = muxValues1[i];                    // Get the value of the analog input from the analogVal array.
       // CCvals[] =   muxValues1[i];
        Serial1.write((uint8_t *)&msg, sizeof(msg));    // Send the MIDI message.
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankOP1 + i), muxValues1[i], CHANNEL); //// Send the usbMIDI message.
        prevMuxValues1[i] = muxValues1[i];        
      }
      
      if (muxValues2[i] != prevMuxValues2[i]){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankOP2 + i);                 
        msg.data2   = muxValues2[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankOP2 +i), muxValues2[i], CHANNEL);
        prevMuxValues2[i] = muxValues2[i];                                     
      }
      
      if (muxValues3[i] != prevMuxValues3[i]){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankOP3 +i);;                  
        msg.data2   = muxValues3[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));   
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankOP3 +i), muxValues3[i], CHANNEL);
        prevMuxValues3[i] = muxValues3[i];        
      }
      
      if (muxValues4[i] != prevMuxValues4[i]){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankOP4 +i);                 
        msg.data2   = muxValues4[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankOP4 +i), muxValues4[i], CHANNEL);
        prevMuxValues4[i] = muxValues4[i];                
      }
     
   }
    
    
    for (byte i = 2; i < 10; i++){                   //For COM Board #5 - read Switches 
      setPin(i);
      buttonState5[i] = map(analogRead(A0), 0,1024,0,2);
      if (buttonState5[i] != lastButtonState5[i]) {
        if (buttonState5[i] == 0) {
         buttonCounter5[i]++;
         EEPROM.write (i-2, buttonCounter5[i]);
        }
      }
      lastButtonState5[i] = buttonState5[i];
      if (buttonCounter5[i] % 2 == 0) {             //every other state change
        buttonCounter5[i] = 0;
        muxValues5[i]= 127;
        digitalWrite(pgm_read_byte_near(ledPin5 +i), HIGH);
      }   
      else {
        muxValues5[i]= 0;
        digitalWrite(pgm_read_byte_near(ledPin5 +i), LOW);  
      }  
      if (muxValues5[i] != prevMuxValues5[i]){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankCOM +i);                 
        msg.data2   = muxValues5[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankCOM +i), muxValues5[i], CHANNEL);
        prevMuxValues5[i] = muxValues5[i];                
      }
   }
    
    
    for (byte i = 10; i < 14; i++){                //COM Board #5 -Read Pots
      setPin(i);
      muxValues5[i]=map(analogRead(A0), 0,1024,0,128); 
      if (muxValues5[i] != prevMuxValues5[i]){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankCOM +i);                 
        msg.data2   = muxValues5[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankCOM +i), muxValues5[i], CHANNEL);
        
        if (prevMuxValues5[13] != muxValues5[13]){
          algorithm = map (muxValues5[13], 0,128,0,8);
        }
        prevMuxValues5[i] = muxValues5[i];
      }
   }
   
  
   for (byte i = 14; i < 16; i++){                //For COM Board #5 -read last 2 menu Switches (no Midi messages sent, no LEDS) 
      setPin(i);
      buttonState5[i] = map(analogRead(A0), 0,1024,0,2);
      if (buttonState5[i] != lastButtonState5[i]) {
        if (buttonState5[i] == 1) {
         buttonCounter5[i]++;
          if (buttonState5[14] == 1) { 
             MidiSendAll();
          }
        }
      }
      lastButtonState5[i] = buttonState5[i];
      
      if (buttonCounter5[15] % 2 == 0){
        buttonCounter5[15]= 0;
        mainScreen();  
      }  
      else {
        Menu();
      }
   }  
  
  
  for (byte i = 5; i < 6; i++){                   //For LFO Board #6 - read Switches 
      setPin(i);          // other switch is mux #0  for (i=0; i<6; i+5){
      buttonState6[i] = map(analogRead(A5), 0,1024,0,2);
      if (buttonState6[i] != lastButtonState6[i]) {
        if (buttonState6[i] == 0) {
         buttonCounter6[i]++;
         EEPROM.write (i+8, buttonCounter6[i]);
        }
      }
      lastButtonState6[i] = buttonState6[i];
      
      if (buttonCounter6[i] % 2 == 0) {             //every other state change
         buttonCounter6[i] = 0;
         muxValues6[i]= 127;
         digitalWrite(pgm_read_byte_near(ledPin6 +i), HIGH);
      }   
      else {
        muxValues6[i]= 0;
        digitalWrite(pgm_read_byte_near(ledPin6 +i), LOW);
      }  
      if (muxValues6[i] != prevMuxValues6[i]){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankLFO +i);                 
        msg.data2   = muxValues6[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankLFO +i), muxValues6[i], CHANNEL);
        prevMuxValues6[i] = muxValues6[i];                
      }
   }
    
    
    for (byte i = 10; i < 15; i++){                //LFO Board #6 -Read Pots
      setPin(i);
      muxValues6[i]=map(analogRead(A5), 0,1024,0,128); 
      if (muxValues6[i] != prevMuxValues6[i]){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankLFO +i);                 
        msg.data2   = muxValues6[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankLFO +i), muxValues6[i], CHANNEL);
        
        if (prevMuxValues6[12] != muxValues6[12]){
          LFOwave = map (muxValues6[12], 0,128,0,4);
          }
        prevMuxValues6[i] = muxValues6[i];                                     
        }
     }
   
   
   for (byte i = 1; i < 5; i++){                //For LFO Board #6 -read  menu Switches (no Midi messages sent, no LEDS) 
      setPin(i);
      buttonState6[i] = map(analogRead(A5), 0,1024,0,2);
      if (buttonState6[i] != lastButtonState6[i]) {
        if (buttonState6[i] == 1) {
           buttonCounter6[i]++;
           if (buttonState6[1] == 1) {
              SavePreset();
             } 
           if (buttonState6[2] == 1 && buttonState6[1] == 0) {
              LoadPreset3();
           }
           if (buttonState6[3] == 1 && buttonState6[1] == 0) {
              LoadPreset2();
           }
          if (buttonState6[4] == 1 && buttonState6[1] == 0) {
              LoadPreset1();
           }
         }
      }
      lastButtonState6[i] = buttonState6[i];
      if (buttonCounter6[3] % 2 == 0) {          
        // do something
      }   
      if (buttonCounter6[2] % 2 == 0){
        // do something else?  
      }  
      else {
        // do something
      }
   }


   for (byte i=0; i< 1; i++){
       setPin(i);          // other switch is mux #0  for (i=0; i<6; i+5){//For LFO Board #6 - read Switches 
      buttonState6[i] = map(analogRead(A5), 0,1024,0,2);
      if (buttonState6[i] != lastButtonState6[i]) {
        if (buttonState6[i] == 0) {
         buttonCounter6[i]++;
         EEPROM.write (i+8, buttonCounter6[i]);
        }
      }
      lastButtonState6[i] = buttonState6[i];
      
      if (buttonCounter6[i] % 2 == 0) {             //every other state change
         buttonCounter6[i] = 0;
         muxValues6[i]= 127;
         digitalWrite(pgm_read_byte_near(ledPin6 + i), HIGH);
      }   
      else {
        muxValues6[i]= 0;
        digitalWrite(pgm_read_byte_near(ledPin6 + i), LOW);
      }  
      if (muxValues6[i] != prevMuxValues6[i]){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankLFO +i);                 
        msg.data2   = muxValues6[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankLFO +i), muxValues6[i], CHANNEL);
        prevMuxValues6[i] = muxValues6[i];                
      }
  }
}
//////////////////////////////////////////////////////////////////////////////////////////

void MidiSendAll (){
   preset = 0;
   byte z = 0;
   for (byte i = 2; i < 12; i++){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankOP1 + i);                 
        msg.data2   = muxValues1[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));       
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankOP1 + i), muxValues1[i], CHANNEL); 
        CCvals[z]= muxValues1[i];
        z++;
   }
     
   for (byte i = 2; i < 12; i++){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankOP2 + i);                 
        msg.data2   = muxValues2[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankOP2 +i), muxValues2[i], CHANNEL);
        CCvals[z]= muxValues2[i];
        z++;
   }     
      
   for (byte i = 2; i < 12; i++){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankOP3 +i);;                  
        msg.data2   = muxValues3[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));   
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankOP3 +i), muxValues3[i], CHANNEL);
        CCvals[z]= muxValues3[i];
        z++;}
     
   for (byte i = 2; i < 12; i++){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankOP4 +i);                 
        msg.data2   = muxValues4[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankOP4 +i), muxValues4[i], CHANNEL);
        CCvals[z]= muxValues4[i];
        z++;
   }

   for (byte i = 2; i < 14; i++){ 
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankCOM +i);                 
        msg.data2   = muxValues5[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankCOM +i), muxValues5[i], CHANNEL);
        CCvals[z]= muxValues5[i];
        z++;
   }

   for (byte i = 10; i < 15; i++){ 
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankLFO +i);                 
        msg.data2   = muxValues6[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankLFO +i), muxValues6[i], CHANNEL);
        CCvals[z]= muxValues6[i];
        z++;
   }
 
 
   for (byte i=0; i< 1; i++){
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankLFO +i);                 
        msg.data2   = muxValues6[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankLFO +i), muxValues6[i], CHANNEL);
        CCvals[z]= muxValues6[i];
        z++;
}

   for (byte i = 5; i < 6; i++){    
        t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCBankLFO +i);                 
        msg.data2   = muxValues6[i];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));    
        usbMIDI.sendControlChange(pgm_read_byte_near(CCBankLFO +i), muxValues6[i], CHANNEL);
        CCvals[z]= muxValues6[i];
        z++;
}


}
 
//////////////////////////////////////////////////////////////////////////////////////////

  
void ReadPots(){
  byte z = 0;
  for (byte i = 2; i < 12; i++){
      setPin(i); // choose an input pin on the 74HC4067
      muxValues1[i]=map(analogRead(A2), 0,1024,0,128); // read the vlaue on that pin and store in array
      CCvals[z]= muxValues1[i];
      z++;
      
      muxValues2[i]=map(analogRead(A1), 0,1024,0,128);
      CCvals[z]= muxValues2[i];
       z++;
      
      muxValues3[i]=map(analogRead(A4), 0,1024,0,128); 
      CCvals[z]= muxValues4[i];
        z++;
      
      muxValues4[i]=map(analogRead(A3), 0,1024,0,128);
      CCvals[z]= muxValues1[i];
        z++;
    }

    for (byte i = 2; i < 10; i++){                   //For COM Board #5 - read Switches 
      setPin(i);
      buttonState5[i] = map(analogRead(A0), 0,1024,0,2);
      if (buttonState5[i] != lastButtonState5[i]) {
        if (buttonState5[i] == 0) {
         buttonCounter5[i]++;
         EEPROM.write (i-2, buttonCounter5[i]);
        }
      }
      lastButtonState5[i] = buttonState5[i];
      if (buttonCounter5[i] % 2 == 0) {             //every other state change
        buttonCounter5[i] = 0;
        muxValues5[i]= 127;
        digitalWrite(pgm_read_byte_near(ledPin5 +i), HIGH);
      }   
      else {
        muxValues5[i]= 0;
        digitalWrite(pgm_read_byte_near(ledPin5 +i), LOW);  
      }  
      CCvals[z]= muxValues5[i];
      z++;
    }
    
    for (byte i = 10; i < 14; i++){                //COM Board #5 -Read Pots
      setPin(i);
      muxValues5[i]=map(analogRead(A0), 0,1024,0,128); 
      CCvals[z]= muxValues5[i];
      z++;
     }

    
    for (byte i=0; i< 1; i++){
       setPin(i);          // other switch is mux #0  for (i=0; i<6; i+5){//For LFO Board #6 - read Switches 
      buttonState6[i] = map(analogRead(A5), 0,1024,0,2);
      if (buttonState6[i] != lastButtonState6[i]) {
        if (buttonState6[i] == 0) {
         buttonCounter6[i]++;
         EEPROM.write (i+8, buttonCounter6[i]);
        }
      }
      lastButtonState6[i] = buttonState6[i];
      
      if (buttonCounter6[i] % 2 == 0) {             //every other state change
         buttonCounter6[i] = 0;
         muxValues6[i]= 127;
         digitalWrite(pgm_read_byte_near(ledPin6 +i), HIGH);
      }
    else {
        muxValues6[i]= 0;
        digitalWrite(pgm_read_byte_near(ledPin6 +i), LOW); 
    }
    CCvals[z]= muxValues6[i];
      z++;
  }

   for (byte i = 5; i < 6; i++){                   //For LFO Board #6 - read Switches 
      setPin(i);          // other switch is mux #0  for (i=0; i<6; i+5){
      buttonState6[i] = map(analogRead(A5), 0,1024,0,2);
      if (buttonState6[i] != lastButtonState6[i]) {
        if (buttonState6[i] == 0) {
         buttonCounter6[i]++;
         EEPROM.write (i+8, buttonCounter6[i]);
        }
      }
      lastButtonState6[i] = buttonState6[i];
      
      if (buttonCounter6[i] % 2 == 0) {             //every other state change
         buttonCounter6[i] = 0;
         muxValues6[i]= 127;
         digitalWrite(pgm_read_byte_near(ledPin6 +i), HIGH);
      }   
      else {
        muxValues6[i]= 0;
        digitalWrite(pgm_read_byte_near(ledPin6 +i), LOW);
      }
      CCvals[z]= muxValues6[i];
      z++;  
   }

  for (byte i = 10; i < 15; i++){                //LFO Board #6 -Read Pots
      setPin(i);
      muxValues6[i]=map(analogRead(A5), 0,1024,0,128); 
      CCvals[z]= muxValues6[i];
      z++;
  }
}

//////////////////////////////////////////////////////////////////////////////////////////


void MidiMerge(){
   while(Serial1.available()){
    inByte = Serial1.read();
     if ( inByte != 0xFE   ){  // dump active sensing
      
       if( (inByte & 0x80) == 0x80 ){  //a midi command?
          cmd = inByte; // current cmd including midi channel
        //  channel = inByte & 0x0f ; // save the midi channel to use for controllers
          messageCnt = 1;
          sendMidi(inByte);
          ctrlSend = 0 ; //don't send ctrl now
      }
      
       else{  
          if (messageCnt == 1){
            messageCnt = 2;
            sendMidi(inByte);
          }
          else if(messageCnt == 2){
            messageCnt = 3;
            sendMidi(inByte);
            ctrlSend = 1 ; //can send ctrl now
            //velocity = inByte;
          }   
          else if(messageCnt == 3){ // running status command
            messageCnt = 2;
            sendMidi(cmd);           
            sendMidi(inByte);
            ctrlSend = 0 ; //don't send ctrl now            
         }     
       }
     }  
   }
 }

//////////////////////////////////////////////////////////////////////////////////////////


void sendMidi(byte midiByte){
       
  if((midiByte & 0x80) == 0x80){  //a command?
      lastCmd = midiByte ; // update new command running status
      Serial1.write(midiByte); //  send command data byte
  }
  else{
       Serial1.write(midiByte); // just send data byte 
  }
}

//////////////////////////////////////////////////////////////////////////////////////////


void ctrlMessage(byte ccCmd, byte ctrl, byte value) {
  //if(ctrl== VelocityDest){ value = value + velocity;}
  sendMidi(ccCmd);
  sendMidi(ctrl);
  sendMidi(value);
}

//////////////////////////////////////////////////////////////////////////////////////////


void usbMidiMerge(){
  if (usbMIDI.read()){
    inByte = usbMIDI.read();
    if ( inByte != 0xFE   ){  // dump active sensing
      
      if( (inByte & 0x80) == 0x80 ){  //a midi command?
          cmd = inByte; // current cmd including midi channel
        //  channel = inByte & 0x0f ; // save the midi channel to use for controllers
          messageCnt = 1;
          sendMidi(inByte);
          ctrlSend = 0 ; //don't send ctrl now
      }
      
      else{  
        if (messageCnt == 1){
            messageCnt = 2;
            sendMidi(inByte);
        }
        else if(messageCnt == 2){
            messageCnt = 3;
            sendMidi(inByte);
            ctrlSend = 1 ; //can send ctrl now
        }   
        else if(messageCnt == 3){ // running status command
            messageCnt = 2;
            sendMidi(cmd);           
            sendMidi(inByte);
            ctrlSend = 0 ; //don't send ctrl now            
        }     
      }
    }  
  }
}

//////////////////////////////////////////////////////////////////////////////////////////


void mainScreen (){
  display.setTextSize(2);      //Print to display
   display.setTextColor(WHITE);
   display.setCursor(0,0);
   display.print(F(" Algorithm   #"));
   display.println(algorithm,DEC);
   display.setTextSize(1);      //Print to display
   display.setTextColor(WHITE);
   display.setCursor(0,45);
   display.print(F("LFO Wave: "));
   display.println(LFOwaveShape[LFOwave]);
   if (preset >= 1){
     display.setCursor(70,55);
     display.print(F("Preset "));
     display.println(preset);
   }
   else if (preset < 1){
     display.setCursor(90,55);
     display.print(F("Panel"));
   }
   display.display();
   display.clearDisplay();
}

//////////////////////////////////////////////////////////////////////////////////////////


void Menu (){
  setPin(13);
  //velocityDest = map (muxValues5[13], 0,128,26,73);
  CHANNEL = map (muxValues5[13], 0,128,1,17);
  EEPROM.write (24, CHANNEL);
  display.setTextSize(2);      //Print to display
  display.setTextColor(WHITE);
  display.setCursor(0,5);
  display.print(F(" Menu "));
  display.setCursor(0,25);
  display.setTextSize(1);
  //display.print(F(" Velocity Destination = CC#"));
 // display.print(velocity,DEC);
  //display.print(F(" MIDI Channel = #"));
  //display.print(CHANNEL);
  display.display();
  display.clearDisplay();
}

/////////////////////////////////////////////////////////////////////////////////
void SavePreset(){
  if (buttonState6[1] == 1){
    if (buttonState6[2] == 1){
      for (byte z = 0; z < 60; z++){
        EEPROM.write (z+150, CCvals[z]);
      }
    }
    if (buttonState6[3] == 1){
      for (byte z = 0; z < 60; z++){
        EEPROM.write (z+90, CCvals[z]);
      }
    }
    if (buttonState6[4] == 1){
      for (byte z = 0; z < 60; z++){
        EEPROM.write (z+30, CCvals[z]);
      } 
    }
  }
}

/////////////////////////////////////////////////////////////////////////////////




/////////////////////////////////////////////////////////////////////////////////


void LoadPreset1(){
  preset = 1;
  for (byte z = 0; z < 60; z++){
      CCvals[z] = EEPROM.read(z+30);
      t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCnums +z);                
        msg.data2   = CCvals[z];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));  
  }
}

/////////////////////////////////////////////////////////////////////////////////


void LoadPreset2(){
  preset = 2;
  for (byte z = 0; z < 60; z++){
      CCvals[z] = EEPROM.read(z+90);
      t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCnums +z);                
        msg.data2   = CCvals[z];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));  
  }
}

/////////////////////////////////////////////////////////////////////////////////


void LoadPreset3(){
  preset = 3;
  for (byte z = 0; z < 60; z++){
      CCvals[z] = EEPROM.read(z+150);
      t_midiMsg msg;
        msg.status = CC;                               
        msg.status = msg.status | (CHANNEL-1);          
        msg.data1   = pgm_read_byte_near(CCnums +z);                
        msg.data2   = CCvals[z];                    
        Serial1.write((uint8_t *)&msg, sizeof(msg));  
  }
}
 
Last edited:
Serial1.write(byte) transmits one byte.... that's why the second one won't compile.

Untested... but I believe it's something like this:
Code:
void setup() {
Serial1.begin(31250 );
}

void loop() {  
  if (usbMIDI.read()) {
    Serial1.write(128 + usbMIDI.getType()<<4 + usbMIDI.getChannel());
    Serial1.write(usbMIDI.getData1());
    Serial1.write(usbMIDI.getData2());
  }
}

But what do you mean "compiles but doesnt work"?
If it compiles then it's sending to Tx ...if by doesn't work you mean the MIDI monitor you have connected to the Tx doesn't get a valid MIDI message it's because nothing you have above is a valid MIDI message.
 
Last edited:
Thanks for the reply. oddson, as far as I can tell, your scheme looks good and midi info is in the correct order( i can only half-follow the bit shift stuff) , but unfortunately Im still not getting legit midi messages being sent. I set up an arduino to receive serial out from the teensy and print it to the serial monitor. Stuff is definitely being sent. Of course, I cant read it at 31200 but there is a repeatable flow of gibberish characters that appear in correlation with usbMidi keyboard presses. There was prob little doubt, but this kinda narrows it down to broken code.

This is my first time working w/ usbmidi, but Ive done lots of other serial midi projects. While it is not very sophisticated, something like this has always worked reliably and (painlessly) to forward the incoming messages to out:
Code:
if (Serial1.read() > 1)
    {
      inByte= Serial1.read();  
        Serial1.write(inByte);
    }

Surely the problem going to be something (embarrassingly) obvious and simple, but i still dont get why usbMidi is being uncooperative.
 
Last edited:
Serial1.write(byte) transmits one byte.... that's why the second one won't compile.


But what do you mean "compiles but doesnt work"?
If it compiles then it's sending to Tx ...if by doesn't work you mean the MIDI monitor you have connected to the Tx doesn't get a valid MIDI message it's because nothing you have above is a valid MIDI message.

I honestly dont get the second one at all either. Looks like the info would be all mixed up, as the channel is identified and apparently written last... but this from an "official" example from the arduino midi library and Ive seen various example of it in other code, so I assume it works... w/ serial at least.
 
Try <<4... I wasn't thinking clearly when I wrote that...

But if that doesn't work try sending an known good MIDI message (three bytes) and verify you whatever hardware/software you're using to determine when it's 'working'.
 
Last edited:
USB and Serial MIDI are not identical binary data. Serial MIDI uses 2 or 3 bytes for most messages. On USB all messages except sysex are packed into 4 byte containers. When you use MIDI normally, you get the messages, not the bytes. If you try to just move bytes between Serial and MIDI, it won't work because the encoding is different.
 
Paul... Ahh...Ok, that makes sense (abstractly ;) So status and channel are separate bytes?
Oddson, even with your changes, Im still not getting accurate messages thru. Ill see if i can try to track down exactly how and which parts of the message are getting messed up. I recon that the problem lies w/ the first line...but still a little confused and at a loss as for what to try next. I dont really understand the need for the bit shifting and the +128...admittedly its a little over my head.

Shouldnt usbMidi.GetType and usbMIDI.getChannel return a standard ready-to-go midi channel and type? Are there any documented examples for what Im trying to achieve? I know it is nothing new, but google and forum searches arent really yielding much useful info.

thanks!
 
Last edited:
getType was meant to agree with the old MIDI library which has since changed to an offset value for status that can be added to the channel to return the status byte...

until and unless Paul changes that you will need to sort out the status byte with bit manipulation... at which I suck...

the goal (as I understand things and I'm waiting for Paul or someone to confirm) is to generate a status byte and two data bytes as follows:

1tttcccc/0nnnnnnn/0vvvvvvv

t are three type bits
c is four channel bits
n is 7 data 1 (note value) bits
v is 7 data 2 (velocity) bits

You would still need to look for sysex and exclude these or handle them separately (and I think real-time can cause issues too).

As simple as what you want to do sounds, it's not trivial because the two midi libraries don't agree fully and there is way to pass generic MIDI messages between them... I figured you were already aware of this as you were trying to send data directly through the Tx without MIDI.h
 
Last edited:
There is a MIDI.send() that is generic enough you don't have to mess with Serial1.send() but you do need to change the Type message from 0-7 to the offset system used in the MIDI library
Code:
MIDI.send(type,d1,d2,chnl);

I was looking at this issue some time ago but I never got around to actually trying it in hardware.

https://forum.pjrc.com/threads/31708-Passing-regular-MIDI-from-host-to-host#7

The code in that post will not work (I'm pretty sure) as it does not send in the offset system used in MIDI.h.

You have to convert the byte as follows first:

0 -> 0x80
1 -> 0x90
2 -> 0xA0
...
7 -> 0xF0

And you can't send type 7 as sysEx messages can run more than 3 bytes.
 
Status
Not open for further replies.
Back
Top