Guys,
Here is an example i've written with this library. It will output a squarewave on pin 9 corresponding to any midi note entered into a keyboard.
It also has a keyboard buffer which allows note rollovers which took me days to get right as my 'C' skills are very rusty. I'm still new to all this.
Let me know how you get on.
#include <MIDI.h>
//variables setup
byte note;
byte velocity;
byte OPTO_PIN = 7;
byte noteBuf[5]={255,255,255,255,255}; // set up keyboard buffer for 5 notes
int squareout = 9; // sound is output on pin 9. Just connect a speaker to it and gnd.
int channel = 1; // MIDI channel to respond to (in this case channel 1)
// setup midi note value for timer 1 (16bit resolution)
unsigned int note_val[]={
34321,32395,30577,28860,27240,25711,24268,22906,21620,20407,19261,18180,17160,16197,15288,14429,13619,12855,12133,11452,10809,10203,9630
,9089,8579,8098,7643,7214,6809,6427,6066,5725,5404,5101,4814,4544,4289,4048,3821,3606,3404,3213,3032,2862,2701,2550,2406,2271,2144,2023,1910,1802,1701
,1606,1515,1430,1350,1274,1202,1135,1071,1011,954,900,850,802,757,714,674,636,600,567,535,505,476
};
//setup: declaring iputs and outputs and begin serial
void setup() {
pinMode(squareout, INPUT); // silence the squarewave out until we get our first note.
// now set up timer1
TCCR1A = _BV(COM1A0)
| _BV(COM1B0) // toggle OC1B on compare match
| _BV(WGM10)
| _BV(WGM11);
TCCR1B = _BV(WGM12)
| _BV(WGM13); // Fast PWM mode, OCR1A as TOP
OCR1B = 0; // toggle when the counter is zero
OCR1A = 34321; // default timer value (could be anything)
TCCR1B |= _BV(CS11); // set prescale to div 8 and start the timer
MIDI_Class MIDI; // instance the midi class
MIDI.begin (OPTO_PIN); // not sure I need this
MIDI.useExternalPowerPin(); // use different 5 volt supply for this implementation
}
//loop: wait for serial data, and interpret the message
void loop () {
if (MIDI.read(channel) > 0) // must be a real midi message
{
if (MIDI.getType() == NoteOn) // get midi not on message
{
note = MIDI.getData1();
velocity = MIDI.getData2();
if (velocity!=0)
{
note_insert(note);
}
else if (velocity==0) // this will be a note off message
{
note_delete(note);
}
}
if (MIDI.getType() == NoteOff) // my midi keyboard never sends this. It's alway noteon with velocity ==0
{
note = MIDI.getData1();
velocity = MIDI.getData2();
note_delete(note);
}
playNote();
}
}
void playNote() // play the note
{
int ploop;
if (noteBuf[0]!=255)
{
pinMode (squareout, OUTPUT);
OCR1A = note_val[noteBuf[ploop]-22];
}
else
{
pinMode (squareout, INPUT);
}
}
// insertion note into note_buff
void note_insert(byte note)
{
int nloop;
for(int nloop=4; nloop>0;nloop--)
{
noteBuf[nloop]=noteBuf[nloop-1];
}
noteBuf[0]=note;
}
// delete note into note_buff. It should first find the notes
// position and then delete it.
void note_delete(byte note)
{
int nloop, pos;
for (nloop=0; nloop<=4;nloop++)
{
if (noteBuf[nloop] == note) {
pos=nloop;
break;
}
}
for(nloop=pos; nloop<=4;nloop++)
{
noteBuf[nloop]=noteBuf[nloop+1];
}
noteBuf[nloop-1]=255;
}