Hi Friends,
I'm attempting to receive MIDI IN data on my Arduino to automate the brightness of an LED.
I don't have very much experience with coding and this project is a complete introduction to MIDI for me. I based my project off the information from this Instructable. http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/step9/Receive-MIDI-Messages-with-Arduino/
I can get the basic idea to work. As I change cc value the LED will dim. However, rather frequently the LED will bounce up to full brightness. This leads me to think that there's a way better way of receiving MIDI Serial Data. I've also noticed that the higher delay time I use, the less likely the LED is to misfire.
I found this Arduino Library Arduino MIDI Library: MIDI_Class Class Reference and was pretty excited at first, but then noticed that the Third Data Byte is conspicuously missing. Perhaps it goes by another name? Not sure how to go about using it for this project.
So, I'm looking for general guidance on how to accurately receive and implement MIDI Serial CC data. In the end this project will use three CCs and 3 LEDs.
Thanks!
PS. My circuit is good to go.
int led = 11;
byte firstByte;
byte secondByte;
byte thirdByte;
void setup(){
pinMode (11, OUTPUT);
Serial.begin(31250);
analogWrite(11, 100);
}
void checkMIDI(){
do{
if (Serial.available()){
firstByte = Serial.read();//read first byte
secondByte = Serial.read();//read next byte
thirdByte = Serial.read();//read final byte
}
}
while (Serial.available() > 24);//when three bytes available
}
void loop(){
checkMIDI();
if (firstByte == 176 && secondByte == 48){
analogWrite(led, thirdByte * 2);
}
delay(50);
}
I've also tried using the Interrupt Code mentioned in the Instructable with zero results.
int led = 11;
byte firstByte;
byte secondByte;
byte thirdByte;
void setup(){
pinMode (11, OUTPUT);
analogWrite(11, 100);
Serial.begin(31250);
cli();//stop interrupts
//set timer2 interrupt every 128us
TCCR2A = 0;// set entire TCCR2A register to 0
TCCR2B = 0;// same for TCCR2B
TCNT2 = 0;//initialize counter value to 0
// set compare match register for 7.8khz increments
OCR2A = 255;// = (16*10^6) / (7812.5*8) - 1 (must be <256)
// turn on CTC mode
TCCR2A |= (1 << WGM21);
// Set CS11 bit for 8 prescaler
TCCR2B |= (1 << CS11);
// enable timer compare interrupt
TIMSK2 |= (1 << OCIE2A);
sei();//allow interrupts
}
ISR(TIMER2_COMPA_vect) {//checks for incoming midi every 128us
do{
if (Serial.available()){
firstByte = Serial.read();//read first byte
secondByte = Serial.read();//read next byte
thirdByte = Serial.read();//read final byte
}
}
while (Serial.available() > 24);//when three bytes available
}
void loop(){
if (firstByte == 176 && secondByte == 48){
analogWrite(led, thirdByte * 2);
}
}
