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
Receiver Teensy sketch
Rx receiving but triggering all else statements no note on
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.
// [url]http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash[/url]
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
}
}
}
Last edited: