This is where I'm stuck… I'm trying to get it to work with the example code provided by Sparkfun-- replacing delay() with mills(), except it doesn't work the same. Where am I going wrong? (I combined the Sparkfun's example code with BlinkWithoutDelay).
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
int val = 0;
byte note = 60; //The MIDI note value to be played
byte resetMIDI = 4; //Tied to VS1053 Reset line
byte ledPin = 13; //MIDI traffic inidicator
int instrument = 0;
// Variables will change:
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
Serial.begin(57600);
//Setup soft serial for MIDI control
mySerial.begin(31250);
//Reset the VS1053
pinMode(resetMIDI, OUTPUT);
digitalWrite(resetMIDI, LOW);
delay(100);
digitalWrite(resetMIDI, HIGH);
delay(100);
talkMIDI(0xB0, 0x07, 120); //0xB0 is channel message, set channel volume to near max (127)
}
void loop()
{
Serial.println("Basic Instruments");
talkMIDI(0xB0, 0, 0x00); //Default bank GM1
//Change to different instrument
for(instrument = 0 ; instrument < 127 ; instrument++) {
Serial.print(" Instrument: ");
Serial.println(instrument, DEC);
talkMIDI(0xC0, instrument, 0); //Set instrument number. 0xC0 is a 1 data byte command
//Play notes from F#-0 (30) to F#-5 (90):
for (note = 60 ; note < 70 ; note++) {
Serial.print("N:");
Serial.println(note, DEC);
// ****************************************************
// HERE IS WHERE DELAY() IS REPLACED WITH MILLIS():
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0, note, 60);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
noteOff(0, note, 60);
}
}
delay(100); //Delay between instruments
}
}
//Send a MIDI note-on message. Like pressing a piano key
//channel ranges from 0-15
void noteOn(byte channel, byte note, byte attack_velocity) {
talkMIDI( (0x90 | channel), note, attack_velocity);
}
//Send a MIDI note-off message. Like releasing a piano key
void noteOff(byte channel, byte note, byte release_velocity) {
talkMIDI( (0x80 | channel), note, release_velocity);
}
//Plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that data values are less than 127
void talkMIDI(byte cmd, byte data1, byte data2) {
digitalWrite(ledPin, HIGH);
mySerial.write(cmd);
mySerial.write(data1);
//Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes
//(sort of: http://253.ccarh.org/handout/midiprotocol/)
if( (cmd & 0xF0) <= 0xB0)
mySerial.write(data2);
}
This is what the section of the noteOn/noteOff section of Sparkfun's example code looks like:
for (note = 60 ; note < 70 ; note++) {
Serial.print("N:");
Serial.println(note, DEC);
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0, note, 60);
delay(50);
//Turn off the note with a given off/release velocity
noteOff(0, note, 60);
delay(50);
}
delay(100); //Delay between instruments
}