Sparkfun社製MIDIシールドを使って、MIDI出力のメトロノームを作成しました。
2つあるボリュームのA1の方をテンポ、A0の方を拍子の選択に使っています。
また、シールド上の緑のLEDをテンポに合わせて点滅させています。
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
static const unsigned ledGreenPin = 6;
static const unsigned ledRedPin = 7;
static const unsigned analogPin1 = 1;
static const unsigned analogPin2 = 0;
static const unsigned closeNote = 42;
static const unsigned openNote = 46;
int val1 = 0;
int val2 = 0;
int tempo = 0;
int count = 0;
unsigned noteNo = closeNote;
int delay_h = 500;
void setup()
{
pinMode(ledGreenPin, OUTPUT);
pinMode(ledRedPin, OUTPUT);
digitalWrite(ledGreenPin, HIGH);
digitalWrite(ledRedPin, HIGH);
count = 0;
MIDI.begin(MIDI_CHANNEL_OFF);
}
void loop()
{
val1 = analogRead(analogPin1);
val2 = analogRead(analogPin2);
val1 = val1/6;
val1 += 30;
val2 = val2/114;
delay_h = 500 * 60 / val1;
if (val2==0) {
noteNo = closeNote;
} else if (count==0) {
noteNo = openNote;
} else {
noteNo = closeNote;
}
count++;
digitalWrite(ledGreenPin, LOW);
MIDI.sendNoteOn(noteNo, 127, 10); // Send a Note
delay(delay_h);
MIDI.sendNoteOff(noteNo, 0, 10); // Stop the note
digitalWrite(ledGreenPin, HIGH);
if (count>=val2) {
count = 0;
}
delay(delay_h);
}