I am currently working on a project controlling MIDI inputs/outputs using an Arduino Uno. I am getting some odd outputs and have narrowed down my problem to be within Serial.print() and/or Serial.println() (have tried both). Current Code:
#include <MIDI.h>
#define LED1 7
#define LED2 6
void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI); //Listen to all channles (note, cannot send data)
Serial.begin(31250); //31250 - standard MIDI baud rate
Serial.print("MIDI input test -- 03/20/13 --\n--Display Note, channel(1 or 16), note, velocity\n");
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
BoardOn(3);
}
void BoardOn(byte num) { // Set an initation blink and an 'On' LED
for (byte i=0;i<num;i++) {
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
delay(30);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
delay(30);
}
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
}
unsigned long t=0; //time in milliseconds
void loop() {
int type; //type of messege, note, velocty, channel, data 1, data 2
byte note, v, ch, d1, d2;
if( MIDI.read() ) { //If messege incoming
byte type = MIDI.getType();
switch( type ) {
case NoteOn:
note = MIDI.getData1();
v = MIDI.getData2();
ch = MIDI.getChannel();
if (v > 0){
Serial.print(String("Note on: ch=") + ch + ", note=" + note + ", velocity=" + v + "\n");
} else {
Serial.print(String("Note off: ch=") + ch + ",note=" + note + "\n");
}
break;
case NoteOff:
note = MIDI.getData1();
v = MIDI.getData2();
ch = MIDI.getChannel();
Serial.print(String("Note off: ch=") + ch + ", note=" + note + ", velocity=" + v + "\n");
break;
default:
//d1 = MIDI.getData1();
//d2 = MIDI.getData2();
//Serial.println(String("Messege, type=") + type + ", data = " + d1 + " " + d2);
break;
}
t=millis();
}
if( (millis() - t) > 10000 ) {
t += 10000;
Serial.print("Inactive for 10seconds or more.\n");
}
}
This prints out everytime I press down or let up a note (using an electronic keyboard). I get outputs that are correct, although the issue is my output adding random characters to the begging of every new line.
Example Output:
MIDI input test -- 03/20/13 --
--Display Note, channel(1 or 16), note, velocity
4PNote on: ch=1, note=52, velocity=800
4 Note off: ch=1,note=520
5PNote on: ch=1, note=53, velocity=800
5 Note off: ch=1,note=530
+PNote on: ch=1, note=43, velocity=800
+ Note off: ch=1,note=430
HPNote on: ch=1, note=72, velocity=800
H Note off: ch=1,note=720
The odd thing is the 'random' characters are consistent with the note I press, as you can see, every On has a corresponding Off with the same leading character. I have narrowed down my error search to the Serial.print() function. I have replaced everything within the print() to something consistent, and it still does the same thing.
IMPORTANT:
As you may know, Arduino does not support MIDI baud rate of 31250 in the serial monitor, I am montering my COM port using pySerial and a small Python program.
test.py
#!/usr/bin/env python3.3
#encoding: utf-8
import sys
import re
import serial
def main():
ser = serial.Serial('COM9',31250) #enable for non-command line argument version
ser.timeout = 10 #Set timeout so readline() cannot block forever
ser.setRTS(True)
ser.setRTS(False)
ser.close() #be safe, close the port before opening (rids Windows errors...)
ser.open() #Open the port between arduino serial and python
while 1:
d1 = ser.readline(); #d1 will be a BYTE class
data = d1.decode('utf-8','ignore') #convert BYTE class into str
print(data)
ser.close()
I do not believe the .decode is the problem as if I only print the readline() result, I get the encoded data, along with the 'random' character and then my Arduino print statement.
I tried to provide as much info as possible, if anyone has heard of a problem with Serial.print() or sees some newb mistake I am making, I would greatly appreciate the help
Thanks once again