That's fantastic! That's much clearer to me now and easier to add any extras I may need down the line! I've added in some extra code as I wanted to use GM midi to access some percussion sounds from a synth chip to create a metronome triggered by the sequencer or master clock. Below is my updated code for anyone who may need it. Thanks immensely blh64 and Deva_Rishi for your time and help!
#define drumchan 9
// general midi drum notes
#define note_snaredrum 37 // rimshot!
// MIDI clock as per Little Scale
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
static uint8_t clk = 0;
// Variables
int tempoled = 13;
int play_flag = 0;
byte data;
// Setup
void setup() {
Serial.begin(31250);
pinMode(tempoled, OUTPUT);
}
// Program
void loop() {
if (Serial.available() > 0) {
data = Serial.read();
if (data == midi_start) { // if midi in start
Serial.write(midi_start); // send midi out start
play_flag = 1;
clk = 0;
}
if (data == midi_continue) { // if midi in continue
Serial.write(midi_continue); // send midi out continue
play_flag = 1;
}
if (data == midi_stop) { // if midi in stop
Serial.write(midi_stop); // send midi out stop
digitalWrite(tempoled, LOW);
play_flag = 0;
}
if ((data == midi_clock) && (play_flag == 1)) {
if (clk == 0) {
digitalWrite(tempoled, HIGH);
noteOn(drumchan, note_snaredrum, 100);
}
if (clk == 7) {
digitalWrite(tempoled, LOW); // this should light it up for 25% of the time
noteOff(drumchan, note_snaredrum, 0);
}
clk = (clk + 1) % 24;
Serial.write(midi_clock); // send midi clock to out... Provides tempo!
}
}
}
// Send a MIDI note-on message. Like pressing a piano key
// channel ranges from 0-15
void noteOn(byte channel, byte note, byte velocity) {
midiMsg( (0x90 | channel), note, velocity);
}
// Send a MIDI note-off message. Like releasing a piano key
void noteOff(byte channel, byte note, byte velocity) {
midiMsg( (0x80 | channel), note, velocity);
}
// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
Serial.write(cmd);
Serial.write(data1);
Serial.write(data2);
}
![]()