Coding-styled examples.
This commit is contained in:
parent
d8b0d6f838
commit
e6da273119
|
|
@ -1,24 +1,25 @@
|
||||||
#include <MIDI.h>
|
#include <MIDI.h>
|
||||||
/*
|
|
||||||
Basic I/O MIDI tutorial
|
|
||||||
by Franky
|
|
||||||
28/07/2009
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define LED 13 // LED pin on Arduino board
|
// Simple tutorial on how to receive and send MIDI messages.
|
||||||
|
// Here, when receiving any message on channel 4, the Arduino
|
||||||
|
// will blink a led and play back a note for 1 second.
|
||||||
|
|
||||||
void setup() {
|
#define LED 13 // LED pin on Arduino Uno
|
||||||
pinMode(LED, OUTPUT);
|
|
||||||
MIDI.begin(4); // Launch MIDI with default options
|
void setup()
|
||||||
// input channel is set to 4
|
{
|
||||||
|
pinMode(LED, OUTPUT);
|
||||||
|
MIDI.begin(4); // Launch MIDI and listen to channel 4
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop()
|
||||||
if (MIDI.read()) {
|
{
|
||||||
digitalWrite(LED,HIGH); // Blink the LED
|
if (MIDI.read()) // If we have received a message
|
||||||
MIDI.sendNoteOn(42,127,1); // Send a Note (pitch 42, velo 127 on channel 1)
|
{
|
||||||
delay(1000); // Wait for a second
|
digitalWrite(LED,HIGH);
|
||||||
MIDI.sendNoteOff(42,0,1); // Stop the note
|
MIDI.sendNoteOn(42,127,1); // Send a Note (pitch 42, velo 127 on channel 1)
|
||||||
digitalWrite(LED,LOW);
|
delay(1000); // Wait for a second
|
||||||
}
|
MIDI.sendNoteOff(42,0,1); // Stop the note
|
||||||
|
digitalWrite(LED,LOW);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,17 @@
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
#define MIDI_AUTO_INSTANCIATE 0
|
||||||
#include <MIDI.h>
|
#include <MIDI.h>
|
||||||
|
|
||||||
|
// This program demonstrates how to use two serial ports at a time,
|
||||||
|
// the hardware serial being used for sending messages to the computer,
|
||||||
|
// while MIDI is handled on a software serial port.
|
||||||
|
// This program measures the time spent to receive and parse a message.
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
SoftwareSerial midiSerial(2,3);
|
||||||
|
MIDI_CREATE_INSTANCE(SoftwareSerial, midiSerial, softMidi);
|
||||||
|
|
||||||
unsigned long gTime_start = 0;
|
unsigned long gTime_start = 0;
|
||||||
unsigned long gTime_stop = 0;
|
unsigned long gTime_stop = 0;
|
||||||
unsigned gCounter = 0;
|
unsigned gCounter = 0;
|
||||||
|
|
@ -7,69 +19,63 @@ unsigned long gTime_sum = 0;
|
||||||
unsigned long gTime_min = -1;
|
unsigned long gTime_min = -1;
|
||||||
unsigned long gTime_max = 0;
|
unsigned long gTime_max = 0;
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
void handleNoteOn(byte inChannel,byte inNote,byte inVelocity)
|
void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
|
||||||
{
|
{
|
||||||
|
gTime_stop = micros();
|
||||||
|
|
||||||
gTime_stop = micros();
|
const unsigned long diff = gTime_stop - gTime_start;
|
||||||
|
gTime_sum += diff;
|
||||||
|
|
||||||
const unsigned long diff = gTime_stop - gTime_start;
|
if (diff > gTime_max) gTime_max = diff;
|
||||||
gTime_sum += diff;
|
if (diff < gTime_min) gTime_min = diff;
|
||||||
|
|
||||||
if (diff > gTime_max) gTime_max = diff;
|
gCounter++;
|
||||||
if (diff < gTime_min) gTime_min = diff;
|
|
||||||
|
|
||||||
gCounter++;
|
|
||||||
|
|
||||||
if (gCounter >= 100) {
|
|
||||||
|
|
||||||
const unsigned long average = gTime_sum / (float)gCounter;
|
|
||||||
|
|
||||||
Serial.println("Time to receive NoteOn: ");
|
|
||||||
|
|
||||||
Serial.print("Average: ");
|
|
||||||
Serial.print(average);
|
|
||||||
Serial.println(" microsecs");
|
|
||||||
|
|
||||||
Serial.print("Min: ");
|
|
||||||
Serial.print(gTime_min);
|
|
||||||
Serial.println(" microsecs");
|
|
||||||
|
|
||||||
Serial.print("Max: ");
|
|
||||||
Serial.print(gTime_max);
|
|
||||||
Serial.println(" microsecs");
|
|
||||||
|
|
||||||
gCounter = 0;
|
|
||||||
gTime_sum = 0;
|
|
||||||
gTime_max = 0;
|
|
||||||
gTime_min = -1;
|
|
||||||
|
|
||||||
MIDI.turnThruOff();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (gCounter >= 100)
|
||||||
|
{
|
||||||
|
const unsigned long average = gTime_sum / (float)gCounter;
|
||||||
|
|
||||||
|
Serial.println("Time to receive NoteOn: ");
|
||||||
|
|
||||||
|
Serial.print("Average: ");
|
||||||
|
Serial.print(average);
|
||||||
|
Serial.println(" microsecs");
|
||||||
|
|
||||||
|
Serial.print("Min: ");
|
||||||
|
Serial.print(gTime_min);
|
||||||
|
Serial.println(" microsecs");
|
||||||
|
|
||||||
|
Serial.print("Max: ");
|
||||||
|
Serial.print(gTime_max);
|
||||||
|
Serial.println(" microsecs");
|
||||||
|
|
||||||
|
gCounter = 0;
|
||||||
|
gTime_sum = 0;
|
||||||
|
gTime_max = 0;
|
||||||
|
gTime_min = -1;
|
||||||
|
|
||||||
|
softMidi.turnThruOff();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
softMidi.begin();
|
||||||
|
|
||||||
MIDI.begin();
|
Serial.begin(38400);
|
||||||
|
Serial.print("Arduino Ready");
|
||||||
Serial.begin(38400);
|
|
||||||
Serial.print("MCU Ready");
|
|
||||||
|
|
||||||
MIDI.sendNoteOn(69,127,1);
|
|
||||||
|
|
||||||
|
softMidi.sendNoteOn(69,127,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
gTime_start = micros();
|
||||||
gTime_start = micros();
|
softMidi.read();
|
||||||
|
|
||||||
MIDI.read();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
#include <MIDI.h>
|
#include <MIDI.h>
|
||||||
|
|
||||||
|
|
||||||
// This function will be automatically called when a NoteOn is received.
|
// This function will be automatically called when a NoteOn is received.
|
||||||
// It must be a void-returning function with the correct parameters,
|
// It must be a void-returning function with the correct parameters,
|
||||||
// see documentation here:
|
// see documentation here:
|
||||||
|
|
@ -8,32 +7,38 @@
|
||||||
|
|
||||||
void HandleNoteOn(byte channel, byte pitch, byte velocity)
|
void HandleNoteOn(byte channel, byte pitch, byte velocity)
|
||||||
{
|
{
|
||||||
// Do whatever you want when you receive a Note On.
|
// Do whatever you want when you receive a Note On.
|
||||||
|
|
||||||
if (velocity == 0) {
|
if (velocity == 0)
|
||||||
// This acts like a NoteOff.
|
{
|
||||||
}
|
// This acts like a NoteOff.
|
||||||
|
}
|
||||||
// Try to keep your callbacks short (no delays ect) as the contrary would slow down the loop()
|
|
||||||
// and have a bad impact on real-time performance.
|
// Try to keep your callbacks short (no delays ect)
|
||||||
|
// otherwise it would slow down the loop() and have a bad impact
|
||||||
|
// on real-time performance.
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
// -----------------------------------------------------------------------------
|
||||||
// Initiate MIDI communications, listen to all channels
|
|
||||||
MIDI.begin(MIDI_CHANNEL_OMNI);
|
void setup()
|
||||||
|
{
|
||||||
// Connect the HandleNoteOn function to the library, so it is called upon reception of a NoteOn.
|
// Initiate MIDI communications, listen to all channels
|
||||||
MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function
|
MIDI.begin(MIDI_CHANNEL_OMNI);
|
||||||
|
|
||||||
|
// Connect the HandleNoteOn function to the library,
|
||||||
|
// so it is called upon reception of a NoteOn.
|
||||||
|
MIDI.setHandleNoteOn(HandleNoteOn); // Put only the name of the function
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop()
|
||||||
// Call MIDI.read the fastest you can for real-time performance.
|
{
|
||||||
MIDI.read();
|
// Call MIDI.read the fastest you can for real-time performance.
|
||||||
|
MIDI.read();
|
||||||
|
|
||||||
// There is no need to check if there are messages incoming if they are bound to a Callback function.
|
// There is no need to check if there are messages incoming
|
||||||
// The attached method will be called automatically when the corresponding message has been received.
|
// if they are bound to a Callback function.
|
||||||
|
// The attached method will be called automatically
|
||||||
|
// when the corresponding message has been received.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,9 @@ MIDI_CREATE_INSTANCE(SoftwareSerial, softSerial, midiB);
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
// Initiate MIDI communications, listen to all channels
|
// Initiate MIDI communications, listen to all channels
|
||||||
midiA.begin(MIDI_CHANNEL_OMNI);
|
midiA.begin(MIDI_CHANNEL_OMNI);
|
||||||
midiB.begin(MIDI_CHANNEL_OMNI);
|
midiB.begin(MIDI_CHANNEL_OMNI);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
|
@ -41,5 +41,3 @@ void loop()
|
||||||
midiB.getChannel());
|
midiB.getChannel());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,43 +1,45 @@
|
||||||
#include <MIDI.h>
|
#include <MIDI.h>
|
||||||
/*
|
|
||||||
MIDI Input tutorial
|
|
||||||
by Franky
|
|
||||||
28/07/2009
|
|
||||||
|
|
||||||
NOTE: for easier MIDI input reading,
|
|
||||||
take a look a the Callbacks example.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define LED 13 // LED pin on Arduino board
|
// This example shows the old way of checking for input messages.
|
||||||
|
// It's simpler to use the callbacks now, check out the dedicated example.
|
||||||
|
|
||||||
void BlinkLed(byte num) { // Basic blink function
|
#define LED 13 // LED pin on Arduino Uno
|
||||||
for (byte i=0;i<num;i++) {
|
|
||||||
digitalWrite(LED,HIGH);
|
|
||||||
delay(50);
|
|
||||||
digitalWrite(LED,LOW);
|
|
||||||
delay(50);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
void setup() {
|
void BlinkLed(byte num) // Basic blink function
|
||||||
pinMode(LED, OUTPUT);
|
{
|
||||||
MIDI.begin(); // Launch MIDI with default options
|
for (byte i=0;i<num;i++)
|
||||||
// (input channel is default set to 1)
|
{
|
||||||
}
|
digitalWrite(LED,HIGH);
|
||||||
|
delay(50);
|
||||||
void loop() {
|
digitalWrite(LED,LOW);
|
||||||
if (MIDI.read()) { // Is there a MIDI message incoming ?
|
delay(50);
|
||||||
switch(MIDI.getType()) { // Get the type of the message we caught
|
}
|
||||||
case midi::ProgramChange: // If it is a Program Change
|
}
|
||||||
BlinkLed(MIDI.getData1()); // Blink the LED a number of times
|
|
||||||
// correponding to the program number
|
// -----------------------------------------------------------------------------
|
||||||
// (0 to 127, it can last a while..)
|
|
||||||
break;
|
void setup()
|
||||||
// See the online reference for other message types
|
{
|
||||||
default:
|
pinMode(LED, OUTPUT);
|
||||||
break;
|
MIDI.begin(); // Launch MIDI, by default listening to channel 1.
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
if (MIDI.read()) // Is there a MIDI message incoming ?
|
||||||
|
{
|
||||||
|
switch(MIDI.getType()) // Get the type of the message we caught
|
||||||
|
{
|
||||||
|
case midi::ProgramChange: // If it is a Program Change,
|
||||||
|
BlinkLed(MIDI.getData1()); // blink the LED a number of times
|
||||||
|
// correponding to the program number
|
||||||
|
// (0 to 127, it can last a while..)
|
||||||
|
break;
|
||||||
|
// See the online reference for other message types
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue