Skip to content

Commit f895039

Browse files
committed
MIDI updates
1 parent 2976842 commit f895039

File tree

4 files changed

+95
-11
lines changed

4 files changed

+95
-11
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 tigoe
3+
Copyright (c) 2018 Tom Igoe
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

MIDI_examples/MIDIUSBJoystick/MIDIUSBJoystick.ino

+6-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int lastButtonState = LOW;
2424

2525
void setup() {
2626
Serial.begin(9600); // initialize serial
27-
pinMode(buttonPin, INPUT);
27+
pinMode(buttonPin, INPUT_PULLUP);
2828
}
2929

3030
void loop() {
@@ -53,7 +53,7 @@ void loop() {
5353
// button plays the note or stops it:
5454
if (buttonState != lastButtonState) {
5555
delay(5); // debounce delay
56-
if (button == HIGH) {
56+
if (buttonState == LOW) {
5757
midiCommand(0x90, note, 127);
5858
} else {
5959
midiCommand(0x80, note, 0);
@@ -66,10 +66,11 @@ void loop() {
6666
Serial.print("\t");
6767
Serial.print(y);
6868
Serial.print("\t");
69-
Serial.println(button);
69+
70+
Serial.println(buttonState);
7071
// when all else fails, turn everything off:
71-
midiCommand(0xB0, 0x7B, 0x00);
72-
MidiUSB.flush();
72+
// midiCommand(0xB0, 0x7B, 0x00);
73+
// MidiUSB.flush();
7374
// Serial.println("all notes off");
7475
}
7576

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
One-key piano MIDI player for MIDUSB
3+
4+
Generates a MIDI notes when you push the button.
5+
Randomizes the note within a scale each time
6+
7+
Uses MIDIUSB for MIDI, so will work on any
8+
32U4-based board (e.g. Uno, Leonardo, Micro, Yún)
9+
10+
Circuit:
11+
pushbutton attached to +Vcc from pin 5.
12+
10-kilohm resistor to ground from pin 5
13+
14+
created 14 Jan 2019
15+
by Tom Igoe
16+
*/
17+
#include <pitchToNote.h>
18+
19+
// the intervals in a major and natural minor scale:
20+
int major[] = {2, 2, 1, 2, 2, 2, 1};
21+
int naturalMinor[] = {2, 1, 2, 2, 1, 2, 2};
22+
23+
// an array to hold the final notes of the scale:
24+
int scale[8];
25+
26+
// start with middle C:
27+
int tonic = pitchC4;
28+
// note to play:
29+
int noteValue = tonic;
30+
31+
// previous state of the button:
32+
int lastButtonState = LOW;
33+
34+
void setup() {
35+
// initialize MIDI serial:
36+
Serial1.begin(31250);
37+
pinMode(5, INPUT);
38+
// fill the scale array with the scale you want:
39+
// start with the initial note:
40+
scale[0] = tonic;
41+
int note = scale[0];
42+
// iterate over the intervals, adding each to the next note
43+
// in the scale. You can change major to naturalMinor
44+
// if you want that kind of scale instead:
45+
for (int n = 0; n < 7; n++) {
46+
note = note + major[n];
47+
scale[n + 1] = note;
48+
}
49+
}
50+
51+
void loop() {
52+
// read the pushbutton:
53+
int buttonState = digitalRead(5);
54+
// compare its state to the previous state:
55+
if (buttonState != lastButtonState) {
56+
// debounce delay:
57+
delay(5);
58+
// if the button's changed and it's pressed:
59+
if (buttonState == HIGH) {
60+
// pick a random note in the scale:
61+
noteValue = scale[random(8)];
62+
// play it:
63+
midiCommand(0x90, noteValue, 0x7F);
64+
} else {
65+
// turn the note off:
66+
midiCommand(0x80, noteValue, 0);
67+
}
68+
// save the button state for comparison next time through:
69+
lastButtonState = buttonState;
70+
}
71+
}
72+
73+
// send a 3-byte midi message
74+
void midiCommand(byte cmd, byte data1, byte data2) {
75+
Serial1.write(cmd); // command byte (should be > 127)
76+
Serial1.write(data1); // data byte 1 (should be < 128)
77+
Serial1.write(data2); // data byte 2 (should be < 128)
78+
}

MIDI_examples/Serial1_MIDI_simple/Serial1_MIDI_simple.ino

+10-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
1010
Circuit:
1111
connect TX of Serial1 to TX pin of MIDI jack
12-
12+
1313
created 13 Feb 2017
1414
modified 13 Dec 2018
1515
by Tom Igoe
1616
*/
1717

1818
int bpm = 72; // beats per minute
1919
// duration of a beat in ms
20-
float beatDuration = 60.0 / bpm * 1000;
20+
float beatDuration = 60.0 / bpm * 1000;
2121

2222
// the melody sequence:
2323
int melody[] = {64, 66, 71, 73, 74, 66, 64, 73, 71, 66, 74, 73};
@@ -34,12 +34,12 @@ void setup() {
3434
void loop() {
3535
// play a note from the melody:
3636
midiCommand(0x90, melody[thisNote], 127);
37-
// all the notes in this are sixteenth notes,
37+
// all the notes in this are sixteenth notes,
3838
// which is 1/4 of a beat, so:
3939
int noteDuration = beatDuration / 4;
40-
// keep it on for the appropriate duration: delay(noteDuration);
40+
// keep it on for the appropriate duration: delay(noteDuration);
4141
// turn the note off:
42-
midiCommand(0x80, melody[thisNote], 127);
42+
midiCommand(0x90, melody[thisNote], 0);
4343
// increment the note number for next time through the loop:
4444
thisNote++;
4545
// keep the note in the range from 0 - 12 using modulo:
@@ -48,6 +48,11 @@ void loop() {
4848

4949
// send a 3-byte midi message
5050
void midiCommand(byte cmd, byte data1, byte data2) {
51+
Serial.print(cmd, HEX);
52+
Serial.print(" ");
53+
Serial.print(data1, HEX);
54+
Serial.print(" ");
55+
Serial.println(data2, HEX);
5156
Serial1.write(cmd); // command byte (should be > 127)
5257
Serial1.write(data1); // data byte 1 (should be < 128)
5358
Serial1.write(data2); // data byte 2 (should be < 128)

0 commit comments

Comments
 (0)