Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 8 of 8

Thread: Sending string data over Serial1 to trigger usb.MIDIsendNote

  1. #1
    Junior Member
    Join Date
    Jul 2016
    Posts
    4

    Sending string data over Serial1 to trigger usb.MIDIsendNote

    I have two Teensy 3.2's connected through Xbee in AT transparent mode, I can see commands come through on the Teensy connected to my laptop in the arduino serial monitor coming from my battery powered Teensy which has four push buttons connected using pullup resistors, but when I try to run an if else if statement from the Serial1 string the on Teensy connected to my laptop receiving the serial data in the Teensy only acts on the else if statement and only sends out the MIDI notes for usb.MIDINoteOff and it doesn't send the usb.MIDINoteOn also it is sending all three of the MIDI notes off commands at the same time, I am including a screenshot of a program called MIDI monitor it will be next to an open serial monitor that shows it is receiving the char I am sending to Serial1 I am holding the button down for a few seconds each time and each time I press or release the button for button 1, 2, and 4, in the example it sends all three note off messages for MIDI notes C3, D3 and E3. Am I using the if else statement incorrectly or is the problem possibly with the way I am reading the char data? I am lost and have been wracking my brain trying to figure out why it will not work. I have the Xbee's connected to the Teensy's in Pins 0&1 for Serial1 on both I am including the schemating for the Teensy I have that is used for note input. I running on Mac OS x 10.11.5 Arduino IDE v1.6.8 and Teensyduino 1.28 both Teenys's in MIDI board mode. I am including both sketches in this post. They will both be prefaced by bold font. I would be appreciative if anyone can see what I am doing wrong, or has any suggestions.

    Thank you.
    Ryan

    Transmitter and buttons sketch
    Transmit Working sending serial data, but receiver Teensy is sending all MIDI note offs
    Code:
    /* Buttons to USB MIDI Example
    
       You must select MIDI from the "Tools > USB Type" menu
    
       To view the raw MIDI data on Linux: aseqdump -p "Teensy MIDI"
    
       This example code is in the public domain.
    */
    
    #include <Bounce.h>
    
    // the MIDI channel number to send messages
    const int channel = 6;
    
    // Create Bounce objects for each button.  The Bounce object
    // automatically deals with contact chatter or "bounce", and
    // it makes detecting changes very simple.
    Bounce button0 = Bounce(0, 10);
    Bounce button1 = Bounce(1, 10);  // 5 = 5 ms debounce time
    Bounce button2 = Bounce(2, 10);  // which is appropriate for good
    Bounce button3 = Bounce(3, 10);  // quality mechanical pushbuttons
    Bounce button4 = Bounce(4, 10);
    Bounce button5 = Bounce(5, 10);  // if a button is too "sensitive"
    Bounce button6 = Bounce(6, 10);  // to rapid touch, you can
    Bounce button7 = Bounce(7, 10);  // increase this time.
    Bounce button8 = Bounce(8, 10);
    Bounce button9 = Bounce(9, 10);
    Bounce button10 = Bounce(10, 10);
    Bounce button11 = Bounce(11, 10);
    
    void setup() {
      // Configure the pins for input mode with pullup resistors.
      // The pushbuttons connect from each pin to ground.  When
      // the button is pressed, the pin reads LOW because the button
      // shorts it to ground.  When released, the pin reads HIGH
      // because the pullup resistor connects to +5 volts inside
      // the chip.  LOW for "on", and HIGH for "off" may seem
      // backwards, but using the on-chip pullup resistors is very
      // convenient.  The scheme is called "active low", and it's
      // very commonly used in electronics... so much that the chip
      // has built-in pullup resistors!
    
      pinMode(2, INPUT_PULLUP);  // C3
      pinMode(3, INPUT_PULLUP);  // D3
      pinMode(4, INPUT_PULLUP);  // E3
      pinMode(5, INPUT_PULLUP);  // F3
      pinMode(6, INPUT_PULLUP);  // G3
      pinMode(7, INPUT_PULLUP);  // A4
      pinMode(8, INPUT_PULLUP);  // B4
      pinMode(9, INPUT_PULLUP);  // C4
      pinMode(10, INPUT_PULLUP); //
      pinMode(11, INPUT_PULLUP); // Teensy 2.0 LED, may need 1k resistor pullup
      Serial1.begin(38400);  //Using Serial1 to communicate to another teensy reading at 38400 Baud because it's faster than MIDI is at 31250
    }
    
    void loop() {
      // Update all the buttons.  There should not be any long
      // delays in loop(), so this runs repetitively at a rate
      // faster than the buttons could be pressed and released.
      button0.update();
      button1.update();
      button2.update();
      button3.update();  // Using Pins 2-9
      button5.update();
      button6.update();
      button7.update();
      button8.update();
      button9.update();
    
      // Check each button for "falling" edge.
      // Send a MIDI Note On message when each button presses
      // Update the Joystick buttons only upon changes.
      // falling = high (not pressed - voltage from pullup resistor)
      //           to low (pressed - button connects pin to ground)
     
    
      if (button2.fallingEdge()) {
        //usbMIDI.sendNoteOn(60, 99, channel);  // 60 = C3
        Serial1.println ('a'); // a = 60 C4
      }
      if (button3.fallingEdge()) {
        //usbMIDI.sendNoteOn(62, 99, channel);  // 62 = D3
        Serial1.println ('b');  //This code is readable through Serial Monitor on the Teensy acroos the wireless network, and reading on and off notes... Fuck yeah Mødē!!!
      }
      if (button4.fallingEdge()) {
        //usbMIDI.sendNoteOn(64, 99, channel);  // 64 = E3
        Serial1.println ('c'); // c = 64 E3
        
      }
      if (button5.fallingEdge()) {
        //usbMIDI.sendNoteOn(65, 99, channel);  // 65 = F3
        Serial1.println ('d');
      }
      if (button6.fallingEdge()) {
        //usbMIDI.sendNoteOn(67, 99, channel);  // 67 = G3
        Serial1.println ('e');
      }
      if (button7.fallingEdge()) {
        usbMIDI.sendNoteOn(69, 99, channel);  // 69 = A4
        Serial1.println ('f');
      }
      if (button8.fallingEdge()) {
        //usbMIDI.sendNoteOn(71, 99, channel);  // 71 = B4
        Serial1.println ('g');
      }
      if (button9.fallingEdge()) {
        usbMIDI.sendNoteOn(72, 99, channel);  // 72 = C4
        Serial1.println ('h');
      }
      if (button10.fallingEdge()) {
        usbMIDI.sendNoteOn(70, 99, channel);  // 70 = A#5
      }
      if (button11.fallingEdge()) {
        usbMIDI.sendNoteOn(71, 99, channel);  // 71 = B5
      }
    
      // Check each button for "rising" edge
      // Send a MIDI Note Off message when each button releases
      // For many types of projects, you only care when the button
      // is pressed and the release isn't needed.
      // rising = low (pressed - button connects pin to ground)
      //          to high (not pressed - voltage from pullup resistor)
     
     
      if (button2.risingEdge()) {
        //usbMIDI.sendNoteOff(60, 0, channel);  // 60 = C3
        Serial1.println ('i');
      }
      if (button3.risingEdge()) {
        //usbMIDI.sendNoteOff(62, 0, channel);  // 62 = D3
        Serial1.println ('j'); //This code is readable through Serial Monitor on the Teensy acroos the wireless network, working with MIDI Wirelessy FuCkYeAh!
      }
      if (button4.risingEdge()) {
        //usbMIDI.sendNoteOff(64, 0, channel);  // 64 = E3
        Serial1.println ('k');
      }
      if (button5.risingEdge()) {
        //usbMIDI.sendNoteOff(65, 0, channel);  // 65 = F3
        Serial1.println ('l');
      }
      if (button6.risingEdge()) {
        //usbMIDI.sendNoteOff(67, 0, channel);  // 67 = G3
        Serial1.println ('m');
      }
      if (button7.risingEdge()) {
        //usbMIDI.sendNoteOff(69, 0, channel);  // 69 = A4
        Serial1.println ('n');
      }
      if (button8.risingEdge()) {
        //usbMIDI.sendNoteOff(71, 0, channel);  // 71 = B4
        Serial1.println ('o');
      }
      if (button9.risingEdge()) {
        //usbMIDI.sendNoteOff(72, 0, channel);  // 72 = C4
        Serial1.println ('p');
      }
      if (button10.risingEdge()) {
        usbMIDI.sendNoteOff(70, 0, channel);  // 70 = A#5
      }
      if (button11.risingEdge()) {
        usbMIDI.sendNoteOff(71, 0, channel);  // 71 = B5
      }
    
      // MIDI Controllers should discard incoming MIDI messages.
      // http://forum.pjrc.com/threads/24179-...ses-midi-crash
      while (usbMIDI.read()) {
        // ignore incoming messages
      }
    }



    Receiver Teensy sketch
    Rx receiving but triggering all else statements no note on

    Code:
    String inData; // Buffer to store incoming commands from serial port
    #include <MIDI.h>
    #define HWSERIAL Serial1
    #define channel 6
    const int on = 100;
    const int off = 0;
    const int c3 = 60;
    const int d3 = 62;
    const int e3 = 64;
    const int f3 = 65;
    const int g3 = 67;
    const int a4 = 69;
    const int b4 = 71;
    const int c4 = 72;
     
    
    void setup() {
        Serial.begin(38400);
        HWSERIAL.begin(38400);
        Serial1.println("Serial conection started, waiting for instructions...");
        String inData = 0;
    }
    
    void loop() {
        while (HWSERIAL.available() > 0)
        {
            char recieved = HWSERIAL.read();
            inData += recieved; 
    
            // Process message when new line character is recieved
            if (recieved == '\n')
            {
                //Serial.print("MIDIrx ");
                Serial.print(inData);
    
                // You can put some if and else here to process the message juste like that:
          if (HWSERIAL.read() == 'a') {
          //if (Serial.read() == 'a') {
          usbMIDI.sendNoteOn(c3, on, channel);
          }
          else if (HWSERIAL.read() == 'i'); {
          //else if (Serial.read() == 'i'); {
          usbMIDI.sendNoteOff(c3, off, channel);
          }
          
          if (HWSERIAL.read() == 'b') {
          usbMIDI.sendNoteOn(d3, on, channel);
          } 
          else if (HWSERIAL.read() == 'j'); {
          usbMIDI.sendNoteOff(d3, off, channel);
          } 
          
          if (HWSERIAL.read() == 'c') {
          usbMIDI.sendNoteOn(e3, on, channel);
          } 
          else if (HWSERIAL.read() == 'k'); {
          usbMIDI.sendNoteOff(e3, off, channel);
          } 
          
          if (HWSERIAL.read() == 'd') {
          usbMIDI.sendNoteOn(f3, on, channel);
          }
          else if (HWSERIAL.read() == 'l') {
            usbMIDI.sendNoteOff(f3, off, channel);
          }
    
          
                if(inData == "+++\n"){ // "\n" at the end of the string.
                  Serial1.println("OK. Press h for help.");
                }   
    
    
                //inData = ""; // Clear recieved buffer
            }
        }
    }
    Click image for larger version. 

Name:	Serial and MIDI Monitor.png 
Views:	61 
Size:	536.5 KB 
ID:	7504Click image for larger version. 

Name:	Xbee TX Circuit Board.png 
Views:	62 
Size:	42.7 KB 
ID:	7505
    Last edited by RyanMode; 07-02-2016 at 12:07 AM. Reason: Add Code tags

  2. #2
    Senior Member
    Join Date
    Nov 2015
    Location
    Wales
    Posts
    579
    Welcome to the forum Ryan,

    Everytime you call HWSERIAL.read() your program reads the oldest value recieved by HWSERIAL, returns it and then deletes it. This is the basic principle of FIFO (first in first out)

    So say you send 'a'

    If you do this on the recieve side: if (HWSERIAL.read() == 'b') {}
    This returns 'a' but by reading it we have removed this value from the HWSERIAL FIFO

    Now later on when we do: else if (HWSERIAL.read() == 'a') {}
    The program will hang until a new character is received

    What you want to do is only call the read once. Store the value in a variable. Then use that variable to compare against:
    Code:
    char incomingCommand = HWSERIAL.read();
    
    if(incomingCommand == 'b') {
    
    } else if (incomingCommand == 'a') {
    
    }

    Just my two cents here but I recommend you use a switch case block to make your code more readable:
    Code:
    char incomingCommand = HWSERIAL.read();
    
    switch(incomingCommand) {
        case 'a' :
              // Process 'a'
              break;
        case 'b' :
              // Process 'b'
              break;
        case default:
              // Unknown Command!
              break;
    }
    On yet another side note your commands come in like this where C is a command
    C/nC/nC/nC/nC/n
    Currently your code will miss out that first C command as it is waiting for a /n. Use Serial.print() instead of Serial.println() on the sender side and don't bother with the new line stuff. This will double your bandwidth and simplify your code
    Last edited by Xenoamor; 07-01-2016 at 10:24 AM.

  3. #3
    Junior Member
    Join Date
    Jul 2016
    Posts
    4
    Quote Originally Posted by Xenoamor View Post
    Welcome to the forum Ryan,

    Everytime you call HWSERIAL.read() your program reads the oldest value recieved by HWSERIAL, returns it and then deletes it. This is the basic principle of FIFO (first in first out)

    So say you send 'a'

    If you do this on the recieve side: if (HWSERIAL.read() == 'b') {}
    This returns 'a' but by reading it we have removed this value from the HWSERIAL FIFO

    Now later on when we do: else if (HWSERIAL.read() == 'a') {}
    The program will hang until a new character is received

    What you want to do is only call the read once. Store the value in a variable. Then use that variable to compare against:
    Code:
    char incomingCommand = HWSERIAL.read();
    
    if(incomingCommand == 'b') {
    
    } else if (incomingCommand == 'a') {
    
    }

    Just my two cents here but I recommend you use a switch case block to make your code more readable:
    Code:
    char incomingCommand = HWSERIAL.read();
    
    switch(incomingCommand) {
        case 'a' :
              // Process 'a'
              break;
        case 'b' :
              // Process 'b'
              break;
        case default:
              // Unknown Command!
              break;
    }
    On yet another side note your commands come in like this where C is a command
    C/nC/nC/nC/nC/n
    Currently your code will miss out that first C command as it is waiting for a /n. Use Serial.print() instead of Serial.println() on the sender side and don't bother with the new line stuff. This will double your bandwidth and simplify your code
    Thank you for the response, I tried changing the sending Teensy to only have Serial1.print but when I took the ln off it no longer sent through serial, or sent the usb.MIDI command? Is there anything else I would need to do? With the serial I tried to change the char incomingCommand = HWSERIAL.read my serial monitor went blank I am including the code from the receive Teensy is this like what you meant? In regards to writing a switch case could write multiple cases with processes to keep going from a-m?
    Thank you for the help.

    Receive Teensy Edit 1 sketch

    Code:
    // Buffer to store incoming commands from serial port
    String inData;
    #define HWSERIAL Serial1  //Teensy Requires you to use Serial1, Serial2, or Serial3 instaed of normal serial, it has 3 URT Serial Ports 
    #define channel 6  //Global MIDI Channel 6
    const int on = 100;
    const int off = 0;
    const int c3 = 60;
    const int d3 = 62;
    const int e3 = 64;
    const int f3 = 65;
    const int g3 = 67;
    const int a4 = 69;
    const int b4 = 71;
    const int c4 = 72;
     
    
    void setup() 
    {
        Serial.begin(38400);  //This is the baudrate at which the Xbee radios operate which is fater than MIDI baud at 31250 bps but Teensy uses USB to send the MIDI to the computer so it will downsample internally
        HWSERIAL.begin(38400);  //This is the baudrate at which the Xbee radios operate which is fater than MIDI baud at 31250 bps but Teensy uses USB to send the MIDI to the computer so it will downsample internally
        Serial1.println("Serial conection started");
    }
    
    void loop() 
    {
        while (HWSERIAL.available() > 0)
        {
              char incomingCommand = HWSERIAL.read();
            //char recieved = HWSERIAL.read();
           // inData += recieved; 
    
            // Process message when new line character is recieved
            //if (recieved == '\n')
            {
                //Serial.print("MIDIrx ");
                Serial.print(inData);
    
                // You can put some if and else here to process the message juste like that:
          if (incomingCommand == 'a') {
          //if (Serial.read() == 'a') {
          usbMIDI.sendNoteOn(c3, on, channel);
          }
          else if (incomingCommand == 'i'); {
          //else if (Serial.read() == 'i'); {
          usbMIDI.sendNoteOff(c3, off, channel);
          }
          
          if (incomingCommand == 'b') {
          usbMIDI.sendNoteOn(d3, on, channel);
          } 
          else if (incomingCommand == 'j'); {
          usbMIDI.sendNoteOff(d3, off, channel);
          } 
          
          if (incomingCommand == 'c') {
          usbMIDI.sendNoteOn(e3, on, channel);
          } 
          else if (incomingCommand == 'k'); {
          usbMIDI.sendNoteOff(e3, off, channel);
          } 
          
          if (incomingCommand == 'd') {
          usbMIDI.sendNoteOn(f3, on, channel);
          }
          else if (incomingCommand == 'l') {
            usbMIDI.sendNoteOff(f3, off, channel);
          }
    
          
                if(inData == "+++\n") // Add "\n" at the end of the string.
                {
                    Serial1.println("OK.");
                }   
    
                inData = ""; // Clear recieved buffer
            }
        }
    }
    Last edited by RyanMode; 07-02-2016 at 12:08 AM. Reason: Add code tags

  4. #4
    Senior Member
    Join Date
    Nov 2015
    Location
    Wales
    Posts
    579
    Hey Ryan, use the code tags to wrap your code next time just to ease our eyes a little!

    Here's my envisioning of what you're trying to do. Where possible stick to good naming and indentation conventions
    Code:
    #define HWSERIAL Serial1 //Teensy Requires you to use Serial1, Serial2, or Serial3 instaed of normal serial, it has 3 URT Serial Ports
    #define MIDI_CHANNEL 6 //Global MIDI Channel 6
    
    // I don't know about the application but these would normally be unsigned
    const int ON = 100;
    const int OFF = 0;
    const int c3 = 60;
    const int d3 = 62;
    const int e3 = 64;
    const int f3 = 65;
    const int g3 = 67;
    const int a4 = 69;
    const int b4 = 71;
    const int c4 = 72;
    
    
    void setup() {
    	//This is the baudrate at which the Xbee radios operate which is faster than MIDI baud at 31250 bps but Teensy uses USB to send the MIDI to the computer so it will downsample internally
    	Serial.begin(38400);
    	HWSERIAL.begin(38400);
    
    	// Shouldn't this following command be on Serial not Serial1? I may well be wrong, I don't know your application
    	HWSERIAL.println("Serial connection started");
    }
    
    void loop() {
    
    	while (HWSERIAL.available() > 0) {
    
    		char incomingCommand = HWSERIAL.read();
    
    		switch(incomingCommand) {
    		    case 'a' :
    		          usbMIDI.sendNoteOn(c3, ON, MIDI_CHANNEL);
    		          break;
    		    case 'b' :
    		          usbMIDI.sendNoteOn(d3, ON, MIDI_CHANNEL);
    		          break;
    		    case 'c' :
    		          usbMIDI.sendNoteOn(e3, ON, MIDI_CHANNEL);
    		          break;
    		    case 'd' :
    		          usbMIDI.sendNoteOn(f3, ON, MIDI_CHANNEL);
    		          break;
    		    case 'i' :
    		          usbMIDI.sendNoteOff(c3, OFF, MIDI_CHANNEL);
    		          break;
    		    case 'j' :
    		          usbMIDI.sendNoteOff(d3, OFF, MIDI_CHANNEL);
    		          break;
    		    case 'k' :
    		          usbMIDI.sendNoteOff(e3, OFF, MIDI_CHANNEL);
    		          break;
    		    case 'l' :
    		          usbMIDI.sendNoteOff(f3, OFF, MIDI_CHANNEL);
    		          break;
    		    case default:
    		          // Unknown Command!
    		          break;
    		}
    
    	}
    
            // This following stuff looks pretty important. Can't say personally as I haven't touched the MIDI library
            // MIDI Controllers should discard incoming MIDI messages.
            while (usbMIDI.read()) {} // ignore incoming messages
    
    }
    Last edited by Xenoamor; 07-01-2016 at 01:18 PM.

  5. #5
    Junior Member
    Join Date
    Jul 2016
    Posts
    4
    Quote Originally Posted by Xenoamor View Post
    Hey Ryan, use the code tags to wrap your code next time just to ease our eyes a little!

    Here's my envisioning of what you're trying to do. Where possible stick to good naming and indentation conventions
    Code:
    #define HWSERIAL Serial1 //Teensy Requires you to use Serial1, Serial2, or Serial3 instaed of normal serial, it has 3 URT Serial Ports
    #define MIDI_CHANNEL 6 //Global MIDI Channel 6
    
    // I don't know about the application but these would normally be unsigned
    const int ON = 100;
    const int OFF = 0;
    const int c3 = 60;
    const int d3 = 62;
    const int e3 = 64;
    const int f3 = 65;
    const int g3 = 67;
    const int a4 = 69;
    const int b4 = 71;
    const int c4 = 72;
    
    
    void setup() {
    	//This is the baudrate at which the Xbee radios operate which is faster than MIDI baud at 31250 bps but Teensy uses USB to send the MIDI to the computer so it will downsample internally
    	Serial.begin(38400);
    	HWSERIAL.begin(38400);
    
    	// Shouldn't this following command be on Serial not Serial1? I may well be wrong, I don't know your application
    	HWSERIAL.println("Serial connection started");
    }
    
    void loop() {
    
    	while (HWSERIAL.available() > 0) {
    
    		char incomingCommand = HWSERIAL.read();
    
    		switch(incomingCommand) {
    		    case 'a' :
    		          usbMIDI.sendNoteOn(c3, ON, MIDI_CHANNEL);
    		          break;
    		    case 'b' :
    		          usbMIDI.sendNoteOn(d3, ON, MIDI_CHANNEL);
    		          break;
    		    case 'c' :
    		          usbMIDI.sendNoteOn(e3, ON, MIDI_CHANNEL);
    		          break;
    		    case 'd' :
    		          usbMIDI.sendNoteOn(f3, ON, MIDI_CHANNEL);
    		          break;
    		    case 'i' :
    		          usbMIDI.sendNoteOff(c3, OFF, MIDI_CHANNEL);
    		          break;
    		    case 'j' :
    		          usbMIDI.sendNoteOff(d3, OFF, MIDI_CHANNEL);
    		          break;
    		    case 'k' :
    		          usbMIDI.sendNoteOff(e3, OFF, MIDI_CHANNEL);
    		          break;
    		    case 'l' :
    		          usbMIDI.sendNoteOff(f3, OFF, MIDI_CHANNEL);
    		          break;
    		    case default:
    		          // Unknown Command!
    		          break;
    		}
    
    	}
    
            // This following stuff looks pretty important. Can't say personally as I haven't touched the MIDI library
            // MIDI Controllers should discard incoming MIDI messages.
            while (usbMIDI.read()) {} // ignore incoming messages
    
    }

    Thank you for the help that looks much cleaner, when I compiled and uploaded the sketch I lost serial communication on the receiving Teensy, is there anything on the sending sketch I would need to edit in order to work with the updated switch case sketch you posted?

    Thank you.
    Ryan

  6. #6
    Senior Member
    Join Date
    Nov 2015
    Location
    Wales
    Posts
    579
    Can you post your latest version of your sender sourcecode?

  7. #7
    Junior Member
    Join Date
    Jul 2016
    Posts
    4
    Here is the latest of my sender source, I tried editing it like you suggested to remove the ln from Serial1.println to only be Serial1.print and I lost serial communication. Is the updated sketch you sent dependent on me using just print instead of println, I would like to increse the bandwidth that sounds awesome. This code is based off of the Teensy sketch Buttons to MIDI with the serial parts added to it, I think that might be where I am making the errors.

    Thank you

    Code:
    /* Buttons to USB MIDI Example
    
       You must select MIDI from the "Tools > USB Type" menu
    
       To view the raw MIDI data on Linux: aseqdump -p "Teensy MIDI"
    
       This example code is in the public domain.
    */
    
    #include <Bounce.h>
    
    // the MIDI channel number to send messages
    const int channel = 6;
    
    // Create Bounce objects for each button.  The Bounce object
    // automatically deals with contact chatter or "bounce", and
    // it makes detecting changes very simple.
    Bounce button0 = Bounce(0, 10);
    Bounce button1 = Bounce(1, 10);  // 5 = 5 ms debounce time
    Bounce button2 = Bounce(2, 10);  // which is appropriate for good
    Bounce button3 = Bounce(3, 10);  // quality mechanical pushbuttons
    Bounce button4 = Bounce(4, 10);
    Bounce button5 = Bounce(5, 10);  // if a button is too "sensitive"
    Bounce button6 = Bounce(6, 10);  // to rapid touch, you can
    Bounce button7 = Bounce(7, 10);  // increase this time.
    Bounce button8 = Bounce(8, 10);
    Bounce button9 = Bounce(9, 10);
    Bounce button10 = Bounce(10, 10);
    Bounce button11 = Bounce(11, 10);
    
    void setup() {
      // Configure the pins for input mode with pullup resistors.
      // The pushbuttons connect from each pin to ground.  When
      // the button is pressed, the pin reads LOW because the button
      // shorts it to ground.  When released, the pin reads HIGH
      // because the pullup resistor connects to +5 volts inside
      // the chip.  LOW for "on", and HIGH for "off" may seem
      // backwards, but using the on-chip pullup resistors is very
      // convenient.  The scheme is called "active low", and it's
      // very commonly used in electronics... so much that the chip
      // has built-in pullup resistors!
    
      pinMode(2, INPUT_PULLUP);  // C3
      pinMode(3, INPUT_PULLUP);  // D3
      pinMode(4, INPUT_PULLUP);  // E3
      pinMode(5, INPUT_PULLUP);  // F3
      pinMode(6, INPUT_PULLUP);  // G3
      pinMode(7, INPUT_PULLUP);  // A4
      pinMode(8, INPUT_PULLUP);  // B4
      pinMode(9, INPUT_PULLUP);  // C4
      pinMode(10, INPUT_PULLUP); //
      pinMode(11, INPUT_PULLUP); // Teensy 2.0 LED, may need 1k resistor pullup
      Serial1.begin(38400);  //Using Serial1 to communicate to another teensy reading at 38400 Baud because it's faster than MIDI is at 31250
    }
    
    void loop() {
      // Update all the buttons.  There should not be any long
      // delays in loop(), so this runs repetitively at a rate
      // faster than the buttons could be pressed and released.
      button0.update();
      button1.update();
      button2.update();
      button3.update();  // Using Pins 2-9
      button5.update();
      button6.update();
      button7.update();
      button8.update();
      button9.update();
    
      // Check each button for "falling" edge.
      // Send a MIDI Note On message when each button presses
      // Update the Joystick buttons only upon changes.
      // falling = high (not pressed - voltage from pullup resistor)
      //           to low (pressed - button connects pin to ground)
     
    
      if (button2.fallingEdge()) {
        //usbMIDI.sendNoteOn(60, 99, channel);  // 60 = C3
        Serial1.println ('a'); // a = 60 C4
      }
      if (button3.fallingEdge()) {
        //usbMIDI.sendNoteOn(62, 99, channel);  // 62 = D3
        Serial1.println ('b');  //This code is readable through Serial Monitor on the Teensy acroos the wireless network, and reading on and off notes... Fuck yeah Mødē!!!
      }
      if (button4.fallingEdge()) {
        //usbMIDI.sendNoteOn(64, 99, channel);  // 64 = E3
        Serial1.println ('c'); // c = 64 E3
        
      }
      if (button5.fallingEdge()) {
        //usbMIDI.sendNoteOn(65, 99, channel);  // 65 = F3
        Serial1.println ('d');
      }
      if (button6.fallingEdge()) {
        //usbMIDI.sendNoteOn(67, 99, channel);  // 67 = G3
        Serial1.println ('e');
      }
      if (button7.fallingEdge()) {
        usbMIDI.sendNoteOn(69, 99, channel);  // 69 = A4
        Serial1.println ('f');
      }
      if (button8.fallingEdge()) {
        //usbMIDI.sendNoteOn(71, 99, channel);  // 71 = B4
        Serial1.println ('g');
      }
      if (button9.fallingEdge()) {
        usbMIDI.sendNoteOn(72, 99, channel);  // 72 = C4
        Serial1.println ('h');
      }
      if (button10.fallingEdge()) {
        usbMIDI.sendNoteOn(70, 99, channel);  // 70 = A#5
      }
      if (button11.fallingEdge()) {
        usbMIDI.sendNoteOn(71, 99, channel);  // 71 = B5
      }
    
      // Check each button for "rising" edge
      // Send a MIDI Note Off message when each button releases
      // For many types of projects, you only care when the button
      // is pressed and the release isn't needed.
      // rising = low (pressed - button connects pin to ground)
      //          to high (not pressed - voltage from pullup resistor)
     
     
      if (button2.risingEdge()) {
        //usbMIDI.sendNoteOff(60, 0, channel);  // 60 = C3
        Serial1.println ('i');
      }
      if (button3.risingEdge()) {
        //usbMIDI.sendNoteOff(62, 0, channel);  // 62 = D3
        Serial1.println ('j'); //This code is readable through Serial Monitor on the Teensy acroos the wireless network, working with MIDI Wirelessy FuCkYeAh!
      }
      if (button4.risingEdge()) {
        //usbMIDI.sendNoteOff(64, 0, channel);  // 64 = E3
        Serial1.println ('k');
      }
      if (button5.risingEdge()) {
        //usbMIDI.sendNoteOff(65, 0, channel);  // 65 = F3
        Serial1.println ('l');
      }
      if (button6.risingEdge()) {
        //usbMIDI.sendNoteOff(67, 0, channel);  // 67 = G3
        Serial1.println ('m');
      }
      if (button7.risingEdge()) {
        //usbMIDI.sendNoteOff(69, 0, channel);  // 69 = A4
        Serial1.println ('n');
      }
      if (button8.risingEdge()) {
        //usbMIDI.sendNoteOff(71, 0, channel);  // 71 = B4
        Serial1.println ('o');
      }
      if (button9.risingEdge()) {
        //usbMIDI.sendNoteOff(72, 0, channel);  // 72 = C4
        Serial1.println ('p');
      }
      if (button10.risingEdge()) {
        usbMIDI.sendNoteOff(70, 0, channel);  // 70 = A#5
      }
      if (button11.risingEdge()) {
        usbMIDI.sendNoteOff(71, 0, channel);  // 71 = B5
      }
    
      // MIDI Controllers should discard incoming MIDI messages.
      // http://forum.pjrc.com/threads/24179-...ses-midi-crash
      while (usbMIDI.read()) {
        // ignore incoming messages
      }
    }
    Last edited by RyanMode; 07-02-2016 at 12:06 AM.

  8. #8
    Senior Member
    Join Date
    Nov 2015
    Location
    Wales
    Posts
    579
    I can't see anything wrong with your code. Here's what I feel should work though:

    Code:
    #include <Bounce.h>
    
    #define HWSERIAL Serial1
    
    Bounce button2 = Bounce(2, 10);
    Bounce button3 = Bounce(3, 10);
    Bounce button4 = Bounce(4, 10);
    Bounce button5 = Bounce(5, 10);
    
    void setup() {
    
      pinMode(2, INPUT_PULLUP);  // C3
      pinMode(3, INPUT_PULLUP);  // D3
      pinMode(4, INPUT_PULLUP);  // E3
      pinMode(5, INPUT_PULLUP);  // F3
    
      HWSERIAL.begin(38400);
    
    }
    
    void loop() {
    
      // C3
      if(button2.update()) {
         if(button2.fallingEdge()) {
            HWSERIAL.print('a');
         } else if (button2.risingEdge()) {
            HWSERIAL.print('i');
         }
      }
    
      // D3
      if(button3.update()) {
         if(button3.fallingEdge()) {
            HWSERIAL.print('b');
         } else if (button3.risingEdge()) {
            HWSERIAL.print('j');
         }
      }
    
      // E3
      if(button4.update()) {
         if(button4.fallingEdge()) {
            HWSERIAL.print('c');
         } else if (button4.risingEdge()) {
            HWSERIAL.print('k');
         }
      }
    
      // F3
      if(button5.update()) {
         if(button5.fallingEdge()) {
            HWSERIAL.print('d');
         } else if (button5.risingEdge()) {
            HWSERIAL.print('l');
         }
      }
    
    }
    Last edited by Xenoamor; 07-02-2016 at 04:58 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •