Skip to content

Instantly share code, notes, and snippets.

@ruizjme
Last active November 3, 2015 03:02
Show Gist options
  • Save ruizjme/7a4097e45f1bbb4411b0 to your computer and use it in GitHub Desktop.
Save ruizjme/7a4097e45f1bbb4411b0 to your computer and use it in GitHub Desktop.
/*
V5 - 2-11-15 - JR
This program plays a note at a given bpm through serial MIDI
-POT changes the speed notes are played (by multiples of the base tempo)
-PROXIMITY SENSOR changes the pitch of the notes played (notes within chord)
-BUTTONS select chord from an arrray of chords
CONNECTIONS:
|-------------5V
[POT]----------|-------------A0
|-------------GND
|-----------5V
D2-------[BUTTON1]
|--(10kR)---GND
|-----------5V
D3-------[BUTTON2]
|--(10kR)---GND
D10------[LED]------(10kR)---GND
__________________
| |-[Vcc]---5V
| PROXIMITY SENSOR |-[Trig]--D13
| |-[Echo]--D12
|__________________|-[Gnd]---GND
[ARDUINO]--->[USB SERIAL PORT]--->[HAIRLESS MIDI-SERIAL]--->[IAC DRIVER]--->[MAINSTAGE]--->[SPEAKERS]
*/
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
//-------------- CHANGE THESE -------------------------------------------------------------------//
#define baseBpm 60 // works best ~60bpm
#define minDist 0 // cm from sensor, if distance is less than minDist, no notes are played
#define maxDist 100 // cm from sensor, if distance is more than maxDist, no notes are played
#define minPitch 3 // min is 0
#define maxPitch 15 // max is amount of different notes in chordNotes array -1
//-----------------------------------------------------------------------------------------------//
#define potPin A0 // define peripherals
#define button1 2
#define button2 3
#define led 10
#define trigPin 13
#define echoPin 12
int potValue = 0; // variable to store the value coming from the sensor
int button1State = 0; // variable for reading the pushbutton state
int button2State = 0;
int chordNotes[4][21]{ // array of chord notes
// p= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
{ 24, 28, 31, 36, 40, 43, 48, 52, 55, 60, 64, 67, 72, 76, 79, 84, 88, 91, 96, 100, 103 }, // C maj (c=0)
{ 23, 31, 35, 38, 43, 47, 50, 55, 59, 62, 67, 71, 74, 79, 83, 86, 91, 95, 98, 103, 107 }, // G maj (c=1)
{ 24, 28, 33, 36, 40, 45, 48, 52, 57, 60, 64, 69, 72, 76, 81, 84, 88, 93, 96, 100, 105 }, // A min (c=2)
{ 24, 29, 33, 36, 41, 45, 48, 53, 57, 60, 65, 69, 72, 77, 81, 84, 89, 93, 96, 101, 105 } // F maj (c=3)
};
void setup() {
MIDI.begin();
Serial.begin(115200);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(led, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
potValue = analogRead(potPin);
button1State = digitalRead(button1);
button2State = digitalRead(button2);
long duration, distance;
int c, p, bpm;
digitalWrite(trigPin, LOW); // reads the distance
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
p = map(distance, minDist, maxDist, minPitch, maxPitch); // map proximity sensor max and min dist values to max and min pitch desired
if (0 <= potValue && potValue < 205){ bpm = baseBpm/2; }
if (205 <= potValue && potValue < 410){ bpm = baseBpm; }
if (410 <= potValue && potValue < 615){ bpm = baseBpm*2; }
if (615 <= potValue && potValue < 820){ bpm = baseBpm*4; }
if (820 <= potValue && potValue <= 1023){ bpm = baseBpm*8; }
if (button1State == LOW && button2State == LOW ){ c = 0; };
if (button1State == HIGH && button2State == LOW ){ c = 1; };
if (button1State == LOW && button2State == HIGH){ c = 2; };
if (button1State == HIGH && button2State == HIGH){ c = 3; };
if (distance >= maxDist || distance <= minDist){ // if there is no prox sensor input, don't play any notes
digitalWrite(led,HIGH);
delay(30000/bpm); // should be 6000/bpm to get the space between each beat in millis, but since there are two delays, it's half of that
digitalWrite(led,LOW);
delay(30000/bpm);
}
else {
MIDI.sendNoteOn(chordNotes[c][p],127,1); // Send a Note (pitch from chord notes array, velocity 127, channel 1)
digitalWrite(led,HIGH);
delay(30000/bpm);
MIDI.sendNoteOff(chordNotes[c][p],0,1); // Stop the note
digitalWrite(led,LOW);
delay(30000/bpm);
}
// DEBUGGING STUFF-------------------------//
// Serial.print(potValue);
// Serial.print(" | ");
// Serial.print(30000/bpm);
// Serial.print(" | ");
// Serial.println(bpm);
// Serial.print(c);
// Serial.print(" | ");
// Serial.print(p);
// Serial.print(" | ");
// Serial.println(chordNotes[c][p]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment