|
|
@ -20,14 +20,33 @@ |
|
|
|
|
|
|
|
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings); |
|
|
|
|
|
|
|
Tone freq1; |
|
|
|
Tone freq2; |
|
|
|
Tone freq3; |
|
|
|
const int NUM_STEPPER = 5; |
|
|
|
|
|
|
|
int ledState = LOW; // ledState used to set the LED
|
|
|
|
Tone freq[NUM_STEPPER]; |
|
|
|
|
|
|
|
unsigned long previousMillis[NUM_STEPPER] = {0}; // will store last time LED was updated
|
|
|
|
const long interval = 1000; // interval at which to blink (milliseconds)
|
|
|
|
unsigned long previousMillis = 0; // will store last time LED was updated
|
|
|
|
unsigned long lastMillis[3] = {0,0,0}; // will store last time LED was updated
|
|
|
|
volatile byte playing[NUM_STEPPER] = {0}; |
|
|
|
int enable_pin[NUM_STEPPER] = {X_ENABLE_PIN, Y_ENABLE_PIN, Z_ENABLE_PIN, E0_ENABLE_PIN, E1_ENABLE_PIN}; |
|
|
|
|
|
|
|
|
|
|
|
int findSameNotes(byte noteNumber) { |
|
|
|
int candidate = -1; |
|
|
|
bool alreadyPlayingHarmonic = false; |
|
|
|
for (int i=0; i<NUM_STEPPER; i++) { |
|
|
|
if (playing[i] == noteNumber) return -2; // already playing this note
|
|
|
|
if (noteNumber+12 == playing[i] || noteNumber == playing[i]+12) alreadyPlayingHarmonic = true; |
|
|
|
for (int k=i+1; k<NUM_STEPPER; k++) { |
|
|
|
if (playing[k] == playing[i]) { |
|
|
|
return k; |
|
|
|
} else if (playing[k]+12 == playing[i] || playing[k] == playing[i]+12) { |
|
|
|
candidate = k; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (candidate >= 0 && !alreadyPlayingHarmonic) return candidate; |
|
|
|
return -1; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity) |
|
|
@ -38,20 +57,37 @@ void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity) |
|
|
|
// Serial.println(inVelocity);
|
|
|
|
// Serial.flush();
|
|
|
|
//noTone();
|
|
|
|
long freq = sNotePitches[inNumber]; |
|
|
|
freq *=4; |
|
|
|
while (freq > 12000) freq/=2; |
|
|
|
if (inChannel == 9) digitalWrite(LED_PIN, HIGH); |
|
|
|
if (inChannel == 9) { |
|
|
|
freq1.play(freq); |
|
|
|
lastMillis[0] = millis(); |
|
|
|
} else if (inChannel == 7) { |
|
|
|
freq2.play(freq); |
|
|
|
lastMillis[1] = millis(); |
|
|
|
} else if (inChannel == 2) { |
|
|
|
freq3.play(freq); |
|
|
|
lastMillis[2] = millis(); |
|
|
|
|
|
|
|
//if (inChannel == 0 || inChannel >= 32) return;
|
|
|
|
//if (inChannel == 8) return;
|
|
|
|
|
|
|
|
long f = sNotePitches[inNumber]; |
|
|
|
f *= 4; |
|
|
|
while (f > 12000) f/=2; |
|
|
|
//digitalWrite(LED_PIN, HIGH);
|
|
|
|
bool sent = false; |
|
|
|
for (int i=0; i<NUM_STEPPER; i++) { |
|
|
|
if (playing[i] == 0) { |
|
|
|
digitalWrite(enable_pin[i], LOW); |
|
|
|
freq[i].play(f); |
|
|
|
playing[i] = inNumber; |
|
|
|
previousMillis[i] = millis(); |
|
|
|
sent = true; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!sent) { |
|
|
|
digitalWrite(LED_PIN, HIGH); |
|
|
|
int i = findSameNotes(inNumber); |
|
|
|
if (i >= 0) { |
|
|
|
digitalWrite(enable_pin[i], LOW); |
|
|
|
freq[i].play(f); |
|
|
|
playing[i] = inNumber; |
|
|
|
previousMillis[i] = millis(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
void handleNoteOff(byte inChannel, byte inNumber, byte inVelocity) |
|
|
|
{ |
|
|
@ -59,21 +95,30 @@ void handleNoteOff(byte inChannel, byte inNumber, byte inVelocity) |
|
|
|
// Serial.print(inNumber);
|
|
|
|
// Serial.print("\tvelocity: ");
|
|
|
|
// Serial.println(inVelocity);
|
|
|
|
if (inChannel == 9) digitalWrite(LED_PIN, LOW); |
|
|
|
if (inChannel == 9) freq1.stop(); |
|
|
|
if (inChannel == 7) freq2.stop(); |
|
|
|
if (inChannel == 2) freq3.stop(); |
|
|
|
|
|
|
|
//if (inChannel == 0 || inChannel >= 32) return;
|
|
|
|
//if (inChannel == 8) return;
|
|
|
|
|
|
|
|
digitalWrite(LED_PIN, LOW); |
|
|
|
for (int i=0; i<NUM_STEPPER; i++) { |
|
|
|
if (playing[i] == inNumber) { |
|
|
|
freq[i].stop(); |
|
|
|
playing[i] = 0; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void setup() |
|
|
|
{ |
|
|
|
Serial.begin(115200); |
|
|
|
delay(500); |
|
|
|
/* delay(500);
|
|
|
|
Serial.println("##########"); |
|
|
|
Serial.println("MIDI"); |
|
|
|
Serial.println("##########"); |
|
|
|
Serial.flush(); |
|
|
|
|
|
|
|
*/ |
|
|
|
// initialize LED digital pin as an output.
|
|
|
|
pinMode(LED_PIN, OUTPUT); |
|
|
|
|
|
|
@ -89,61 +134,40 @@ void setup() |
|
|
|
pinMode(Z_STEP_PIN, OUTPUT); |
|
|
|
pinMode(Z_ENABLE_PIN, OUTPUT); |
|
|
|
|
|
|
|
digitalWrite(X_DIR_PIN, HIGH); |
|
|
|
digitalWrite(X_ENABLE_PIN, HIGH); |
|
|
|
pinMode(E0_DIR_PIN, OUTPUT); |
|
|
|
pinMode(E0_STEP_PIN, OUTPUT); |
|
|
|
pinMode(E0_ENABLE_PIN, OUTPUT); |
|
|
|
|
|
|
|
digitalWrite(Y_DIR_PIN, HIGH); |
|
|
|
digitalWrite(Y_ENABLE_PIN, HIGH); |
|
|
|
pinMode(E1_DIR_PIN, OUTPUT); |
|
|
|
pinMode(E1_STEP_PIN, OUTPUT); |
|
|
|
pinMode(E1_ENABLE_PIN, OUTPUT); |
|
|
|
|
|
|
|
for (int i=0; i<NUM_STEPPER; i++) { |
|
|
|
digitalWrite(enable_pin[i], HIGH); |
|
|
|
} |
|
|
|
|
|
|
|
digitalWrite(Z_DIR_PIN, HIGH); |
|
|
|
digitalWrite(Z_ENABLE_PIN, HIGH); |
|
|
|
|
|
|
|
MIDI.setHandleNoteOn(handleNoteOn); |
|
|
|
MIDI.setHandleNoteOff(handleNoteOff); |
|
|
|
MIDI.begin(MIDI_CHANNEL_OMNI); |
|
|
|
|
|
|
|
freq1.begin(X_STEP_PIN); |
|
|
|
freq2.begin(Y_STEP_PIN); |
|
|
|
freq3.begin(Z_STEP_PIN); |
|
|
|
|
|
|
|
// digitalWrite(X_ENABLE_PIN, LOW);
|
|
|
|
// tone(X_STEP_PIN, 14000);
|
|
|
|
// tone(X_STEP_PIN, 4*440);
|
|
|
|
// delay(2000);
|
|
|
|
freq[0].begin(X_STEP_PIN); |
|
|
|
freq[1].begin(Y_STEP_PIN); |
|
|
|
freq[2].begin(Z_STEP_PIN); |
|
|
|
freq[3].begin(E0_STEP_PIN); |
|
|
|
freq[4].begin(E1_STEP_PIN); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
void loop() |
|
|
|
{ |
|
|
|
unsigned long currentMillis = millis(); |
|
|
|
if (currentMillis - previousMillis >= interval) { |
|
|
|
previousMillis = currentMillis; |
|
|
|
Serial.println("arduino"); |
|
|
|
// if the LED is off turn it on and vice-versa:
|
|
|
|
if (ledState == LOW) { |
|
|
|
ledState = HIGH; |
|
|
|
} else { |
|
|
|
ledState = LOW; |
|
|
|
} |
|
|
|
|
|
|
|
// set the LED with the ledState of the variable:
|
|
|
|
//digitalWrite(LED_PIN, ledState);
|
|
|
|
} |
|
|
|
|
|
|
|
if (currentMillis - lastMillis[0] >= 2000 || lastMillis[0] == 0) { |
|
|
|
digitalWrite(X_ENABLE_PIN, HIGH); |
|
|
|
} else { |
|
|
|
digitalWrite(X_ENABLE_PIN, LOW); |
|
|
|
} |
|
|
|
if (currentMillis - lastMillis[1] >= 2000 || lastMillis[1] == 0) { |
|
|
|
digitalWrite(Y_ENABLE_PIN, HIGH); |
|
|
|
} else { |
|
|
|
digitalWrite(Y_ENABLE_PIN, LOW); |
|
|
|
} |
|
|
|
if (currentMillis - lastMillis[2] >= 2000 || lastMillis[2] == 0) { |
|
|
|
digitalWrite(Z_ENABLE_PIN, HIGH); |
|
|
|
} else { |
|
|
|
digitalWrite(Z_ENABLE_PIN, LOW); |
|
|
|
for (int i=0; i<NUM_STEPPER; i++) { |
|
|
|
if (currentMillis - previousMillis[i] >= interval || previousMillis[i] == 0) { |
|
|
|
digitalWrite(enable_pin[i], HIGH); |
|
|
|
playing[i] = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//Serial.println("test");
|
|
|
@ -159,4 +183,7 @@ void loop() |
|
|
|
//tone(X_STEP_PIN, 120U);
|
|
|
|
// wait for a second
|
|
|
|
//delay(100);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|