Hello,
This involves a couple different topics so I figured general programming guidance would cover all my bases.
I am writing code for an arduino mega that is meant to read MIDI files from an SD card and then spit out a digital output for each "note" through the digital output pins. Things run test code totally fine and my hardware/wiring is all tested and verified. But when I put it all together with my completed code (shown below) I keep receiving a reset byte (255) as my first read byte from the line starting with:
Serial.println(incomingByte); //I believe line 72 in the code below
when I am expecting a command byte. Maybe because the first byte of a midi song file defaults to start from a blank slate? Unsure. This obviously bypasses all of my if/else statements so I do not get any deeper into my loops like I need to in order to play the notes. Any suggestions? Maybe I ignore the very first byte of each song?/how would I do that? I am open to ideas, expert opinions welcome (and preferred) ![]()
//variables setup
#include <SD.h>
#include <SPI.h>
File songName;
byte incomingByte;
byte note;
byte velocity;
//MIDI pitch values for each "note"
int Aa [ ] = {0, 1, 12, 13, 24, 25, 36, 37, 48, 49, 60, 61, 72, 73, 84, 85, 96, 97, 108, 109, 120, 121}; //22 items in list
int Bb [ ] = {2, 2, 14, 14, 26, 26, 38, 38, 50, 50, 62, 62, 74, 74, 86, 86, 98, 98, 110, 110, 122, 122};
int Cc [ ] = {3, 4, 15, 16, 27, 28, 39, 40, 51, 52, 63, 64, 75, 76, 87, 88, 99, 100, 111, 112, 123, 124};
int Dd [ ] = {5, 6, 17, 18, 29, 30, 41, 42, 53, 54, 65, 66, 77, 78, 89, 90, 101, 102, 113, 114, 125, 126};
int Ee [ ] = {7, 7, 19, 19, 31, 31, 43, 43, 55, 55, 67, 67, 79, 79, 91, 91, 103, 103, 115, 115, 127, 127};
int Ff [ ] = {8, 9, 20, 21, 32, 33, 44, 45, 56, 57, 68, 69, 80, 81, 92, 93, 104, 105, 116, 117, 128, 129};
int Gg [ ] = {10, 11, 22, 23, 34, 35, 46, 47, 58, 59, 70, 71, 82, 83, 94, 95, 106, 107, 118, 119, 130, 131};
const char* songsList [ ] = {"firstmid.mid","second.mid"}; //songlist on SD card
int songNums = 1; //number of songs on SD card
//create all motor outputs (relay signals (HIGH/LOW))
const int NoteA = 2;
const int NoteB = 3;
const int NoteC = 4;
const int NoteD = 5;
const int NoteE = 6;
const int NoteF = 7;
const int NoteG = 8;
int statusLed = LED_BUILTIN; // select the pin for the LED
int action=2; //0 =note off ; 1=note on ; 2= nada
//setup: declaring iputs and outputs and begin serial
void setup() {
pinMode(statusLed,OUTPUT); // declare the LED's pin as output
pinMode(NoteA,OUTPUT);
pinMode(NoteB,OUTPUT);
pinMode(NoteC,OUTPUT);
pinMode(NoteD,OUTPUT);
pinMode(NoteE,OUTPUT);
pinMode(NoteF,OUTPUT);
pinMode(NoteG,OUTPUT);
//start serial with midi baudrate 31250 or 38400 for debugging
Serial.begin(38400);
digitalWrite(statusLed,HIGH);
if (!SD.begin(53)) {//chip select pin on arduino mega is pin # 53
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}
//loop: wait for serial data, and interpret the message
void loop () {
digitalWrite(statusLed, LOW);
for (int i = 0; i <= songNums; i++){
songName = SD.open(songsList[i]);
if (songName) {
Serial.println("song:");
}
// read from the file until there's nothing else in it:
while (songName.available()) {
Serial.println(songsList[i]);
// read the incoming byte:
incomingByte = Serial.read(); ///COME ONNNNNNNNNNNNNNNBNBNNNNNNNNNNNNN FUUUUUCKKKKKKKK SOOOOO CLOOOOSSSEEEEEEEEEE
Serial.println(incomingByte);
// wait for as status-byte, channel 1, note on or off
if (incomingByte== 144){ // note on message starting starting
action=1;
}
else if (incomingByte== 128){ // note off message starting
action=0;
}
else if (incomingByte== 208){ // aftertouch message starting
//not implemented yet
}
else if (incomingByte== 160){ // polypressure message starting
//not implemented yet
}
else if ( (action==0)&&(note==0) ){ // if we received a "note off", we wait for which note (databyte)
note=incomingByte;
playNote(note, 0);
digitalWrite(statusLed, LOW);
note=0;
velocity=0;
action=2;
}
else if ( (action==1)&&(note==0) ){ // if we received a "note on", we wait for the note (databyte)
note=incomingByte;
}
else if ( (action==1)&&(note!=0) ){ // ...and then the velocity
velocity=incomingByte;
playNote(note, velocity);
digitalWrite(statusLed, LOW);
note=0;
velocity=0;
action=0;
}
else{
//nada
}
// close the file:
songName.close();
}
}
}
void playNote(byte note, byte velocity){
int value=LOW;
if (velocity >10){
value=HIGH;
}
else{
value=LOW;
}
for (int count = 0; count <= 21; count++){
if(note == Aa[count]){
digitalWrite(NoteA, value);
digitalWrite(statusLed, HIGH);
}
if(note == Bb[count]){
digitalWrite(NoteB, value);
digitalWrite(statusLed, HIGH);
}
if(note == Cc[count]){
digitalWrite(NoteC, value);
digitalWrite(statusLed, HIGH);
}
if(note == Dd[count]){
digitalWrite(NoteD, value);
digitalWrite(statusLed, HIGH);
}
if(note == Ee[count]){
digitalWrite(NoteE, value);
digitalWrite(statusLed, HIGH);
}
if(note == Ff[count]){
digitalWrite(NoteF, value);
digitalWrite(statusLed, HIGH);
}
if(note == Gg[count]){
digitalWrite(NoteG, value);
digitalWrite(statusLed, HIGH);
}
else {
}
}
}





