|
@ -20,14 +20,33 @@ |
|
|
|
|
|
|
|
|
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings); |
|
|
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings); |
|
|
|
|
|
|
|
|
Tone freq1; |
|
|
const int NUM_STEPPER = 5; |
|
|
Tone freq2; |
|
|
|
|
|
Tone freq3; |
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
const long interval = 1000; // interval at which to blink (milliseconds)
|
|
|
unsigned long previousMillis = 0; // will store last time LED was updated
|
|
|
volatile byte playing[NUM_STEPPER] = {0}; |
|
|
unsigned long lastMillis[3] = {0,0,0}; // will store last time LED was updated
|
|
|
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) |
|
|
void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity) |
|
@ -38,20 +57,37 @@ void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity) |
|
|
// Serial.println(inVelocity);
|
|
|
// Serial.println(inVelocity);
|
|
|
// Serial.flush();
|
|
|
// Serial.flush();
|
|
|
//noTone();
|
|
|
//noTone();
|
|
|
long freq = sNotePitches[inNumber]; |
|
|
|
|
|
freq *=4; |
|
|
//if (inChannel == 0 || inChannel >= 32) return;
|
|
|
while (freq > 12000) freq/=2; |
|
|
//if (inChannel == 8) return;
|
|
|
if (inChannel == 9) digitalWrite(LED_PIN, HIGH); |
|
|
|
|
|
if (inChannel == 9) { |
|
|
long f = sNotePitches[inNumber]; |
|
|
freq1.play(freq); |
|
|
f *= 4; |
|
|
lastMillis[0] = millis(); |
|
|
while (f > 12000) f/=2; |
|
|
} else if (inChannel == 7) { |
|
|
//digitalWrite(LED_PIN, HIGH);
|
|
|
freq2.play(freq); |
|
|
bool sent = false; |
|
|
lastMillis[1] = millis(); |
|
|
for (int i=0; i<NUM_STEPPER; i++) { |
|
|
} else if (inChannel == 2) { |
|
|
if (playing[i] == 0) { |
|
|
freq3.play(freq); |
|
|
digitalWrite(enable_pin[i], LOW); |
|
|
lastMillis[2] = millis(); |
|
|
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) |
|
|
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(inNumber);
|
|
|
// Serial.print("\tvelocity: ");
|
|
|
// Serial.print("\tvelocity: ");
|
|
|
// Serial.println(inVelocity);
|
|
|
// Serial.println(inVelocity);
|
|
|
if (inChannel == 9) digitalWrite(LED_PIN, LOW); |
|
|
|
|
|
if (inChannel == 9) freq1.stop(); |
|
|
//if (inChannel == 0 || inChannel >= 32) return;
|
|
|
if (inChannel == 7) freq2.stop(); |
|
|
//if (inChannel == 8) return;
|
|
|
if (inChannel == 2) freq3.stop(); |
|
|
|
|
|
|
|
|
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() |
|
|
void setup() |
|
|
{ |
|
|
{ |
|
|
Serial.begin(115200); |
|
|
Serial.begin(115200); |
|
|
delay(500); |
|
|
/* delay(500);
|
|
|
Serial.println("##########"); |
|
|
Serial.println("##########"); |
|
|
Serial.println("MIDI"); |
|
|
Serial.println("MIDI"); |
|
|
Serial.println("##########"); |
|
|
Serial.println("##########"); |
|
|
Serial.flush(); |
|
|
Serial.flush(); |
|
|
|
|
|
*/ |
|
|
// initialize LED digital pin as an output.
|
|
|
// initialize LED digital pin as an output.
|
|
|
pinMode(LED_PIN, OUTPUT); |
|
|
pinMode(LED_PIN, OUTPUT); |
|
|
|
|
|
|
|
@ -89,61 +134,40 @@ void setup() |
|
|
pinMode(Z_STEP_PIN, OUTPUT); |
|
|
pinMode(Z_STEP_PIN, OUTPUT); |
|
|
pinMode(Z_ENABLE_PIN, OUTPUT); |
|
|
pinMode(Z_ENABLE_PIN, OUTPUT); |
|
|
|
|
|
|
|
|
digitalWrite(X_DIR_PIN, HIGH); |
|
|
pinMode(E0_DIR_PIN, OUTPUT); |
|
|
digitalWrite(X_ENABLE_PIN, HIGH); |
|
|
pinMode(E0_STEP_PIN, OUTPUT); |
|
|
|
|
|
pinMode(E0_ENABLE_PIN, OUTPUT); |
|
|
|
|
|
|
|
|
digitalWrite(Y_DIR_PIN, HIGH); |
|
|
pinMode(E1_DIR_PIN, OUTPUT); |
|
|
digitalWrite(Y_ENABLE_PIN, HIGH); |
|
|
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.setHandleNoteOn(handleNoteOn); |
|
|
MIDI.setHandleNoteOff(handleNoteOff); |
|
|
MIDI.setHandleNoteOff(handleNoteOff); |
|
|
MIDI.begin(MIDI_CHANNEL_OMNI); |
|
|
MIDI.begin(MIDI_CHANNEL_OMNI); |
|
|
|
|
|
|
|
|
freq1.begin(X_STEP_PIN); |
|
|
freq[0].begin(X_STEP_PIN); |
|
|
freq2.begin(Y_STEP_PIN); |
|
|
freq[1].begin(Y_STEP_PIN); |
|
|
freq3.begin(Z_STEP_PIN); |
|
|
freq[2].begin(Z_STEP_PIN); |
|
|
|
|
|
freq[3].begin(E0_STEP_PIN); |
|
|
// digitalWrite(X_ENABLE_PIN, LOW);
|
|
|
freq[4].begin(E1_STEP_PIN); |
|
|
// tone(X_STEP_PIN, 14000);
|
|
|
|
|
|
// tone(X_STEP_PIN, 4*440);
|
|
|
|
|
|
// delay(2000);
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void loop() |
|
|
void loop() |
|
|
{ |
|
|
{ |
|
|
unsigned long currentMillis = millis(); |
|
|
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) { |
|
|
for (int i=0; i<NUM_STEPPER; i++) { |
|
|
digitalWrite(X_ENABLE_PIN, HIGH); |
|
|
if (currentMillis - previousMillis[i] >= interval || previousMillis[i] == 0) { |
|
|
} else { |
|
|
digitalWrite(enable_pin[i], HIGH); |
|
|
digitalWrite(X_ENABLE_PIN, LOW); |
|
|
playing[i] = 0; |
|
|
} |
|
|
|
|
|
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); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Serial.println("test");
|
|
|
//Serial.println("test");
|
|
@ -159,4 +183,7 @@ void loop() |
|
|
//tone(X_STEP_PIN, 120U);
|
|
|
//tone(X_STEP_PIN, 120U);
|
|
|
// wait for a second
|
|
|
// wait for a second
|
|
|
//delay(100);
|
|
|
//delay(100);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|