-
-
Save pc-coholic/6c5ca26d4cc87353d4d2b43cd7b38d2d to your computer and use it in GitHub Desktop.
SWH 2017A - BoomBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <AccelStepper.h> | |
#include <SoftwareSerial.h> | |
// Produce canned output? | |
#define demo false | |
// MIDI related definitions | |
#define MIDI_OUT_PIN 14 // MIDI OUT is connected to Arduino pin ... | |
#define MIDI_IN_PIN 15 // MIDI IN is connected to Arduino pin ..., | |
// How far should each note turn the stepper? | |
#define stepper1step 256 | |
#define stepper2step 1024 | |
#define stepper3step 128 | |
#define stepper4step 2048 | |
#define stepper5step 5096 | |
// Stepper related definitions | |
#define FULLSTEP 8 | |
#define HALFSTEP 8 | |
// motor pins | |
// Stepper 1 | |
#define motorPin1 39 | |
#define motorPin2 43 | |
#define motorPin3 41 | |
#define motorPin4 45 | |
// Stepper 2 | |
#define motorPin5 47 | |
#define motorPin6 51 | |
#define motorPin7 49 | |
#define motorPin8 53 | |
// Stepper 3 | |
#define motorPin9 30 | |
#define motorPin10 34 | |
#define motorPin11 32 | |
#define motorPin12 36 | |
// Stepper 4 | |
#define motorPin13 38 | |
#define motorPin14 42 | |
#define motorPin15 40 | |
#define motorPin16 44 | |
// Stepper 5 | |
#define motorPin17 46 | |
#define motorPin18 50 | |
#define motorPin19 48 | |
#define motorPin20 52 | |
// Stepper Objects | |
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48 | |
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4); | |
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8); | |
AccelStepper stepper3(HALFSTEP, motorPin13, motorPin15, motorPin14, motorPin16); | |
AccelStepper stepper4(HALFSTEP, motorPin9, motorPin11, motorPin10, motorPin12); | |
AccelStepper stepper5(HALFSTEP, motorPin17, motorPin19, motorPin18, motorPin20); | |
// MIDI Objects | |
//SoftwareSerial midiSerial(MIDI_IN_PIN, MIDI_OUT_PIN); | |
const char* NOTE_NAMES[12] = {"C ", "C#", "D ", "D#", "E ", "F ", "F#", "G ", "G#", "A ", "A#", "B "}; | |
#define MIDI_CMD_NOTE_ON 0x90 | |
#define MIDI_CMD_NOTE_OFF 0x80 | |
#define MIDI_CMD_SENSE 0xf0 | |
void midi_init() { | |
Serial3.begin(31250); | |
} | |
int midi_read() { | |
// Read and return | |
return Serial3.read(); | |
} | |
void midi_write(int d) { | |
Serial3.write(d); | |
} | |
// This function sends a "Note ON" out of the MIDI OUT port | |
void midi_noteOn(uint8_t midi_chn, uint8_t midi_note, uint8_t midi_velocity) { | |
midi_write(MIDI_CMD_NOTE_ON + midi_chn); | |
midi_write(midi_note); | |
midi_write(midi_velocity); | |
} | |
// This function sends a "Note OFF" out of the MIDI OUT port | |
void midi_noteOff(uint8_t midi_chn, uint8_t midi_note, uint8_t midi_velocity) { | |
midi_write(MIDI_CMD_NOTE_OFF + midi_chn); | |
midi_write(midi_note); | |
midi_write(midi_velocity); | |
} | |
// This function must be called continuously to check for incoming data on the MIDI IN port | |
void midi_update() { | |
uint8_t midi_c, midi_cmd, midi_chn, midi_note, midi_velocity; | |
if (Serial3.available()) { | |
midi_c = Serial3.read(); | |
Serial.print("RAW "); Serial.println(midi_c, HEX); | |
//if (midi_c == 0) return; | |
midi_cmd = midi_c & 0xf0; | |
midi_chn = midi_c & 0x0f; | |
//Serial.print(" cmd="); Serial.println(midi_cmd, HEX); | |
switch (midi_cmd) { | |
case MIDI_CMD_NOTE_ON: // Note on | |
midi_note = midi_read(); | |
midi_velocity = midi_read(); | |
on_midi_noteOn(midi_chn, midi_note, midi_velocity); // Call the callback | |
break; | |
case MIDI_CMD_NOTE_OFF: // Note off | |
midi_note = midi_read(); | |
midi_velocity = midi_read(); | |
on_midi_noteOff(midi_chn, midi_note, midi_velocity); // Call the callback | |
break; | |
default: | |
// Unknown command. Just ignore! | |
midi_note = 0; | |
midi_velocity = 0; | |
} | |
} | |
} | |
// This function is called whenever a note is pressed on the MIDI IN port | |
void on_midi_noteOn(uint8_t midi_chn, uint8_t midi_note, uint8_t midi_velocity) { | |
// Play note! | |
int note = midi_note % 12; | |
int octave = midi_note / 12; | |
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
// Hier koennt ihr eure Ton-Erzeugung ansteuern! | |
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
Serial.print("Note ON: "); | |
Serial.println(midi_note); | |
//Serial.print(NOTE_NAMES[note]); | |
//Serial.print(octave); | |
//Serial.print(" volume="); | |
//Serial.println(midi_velocity); | |
// Falls jemand wissen will, was fuer eine FREQUENZ die aktuelle Note hat: | |
//float frq = 220.0 * pow(2.0, ((float)midi_note / 12.0)); | |
//Serial.println(frq); | |
Serial.println(note); | |
if (note == 1 || note == 2 || note == 3){ | |
stepper1.setCurrentPosition(0); | |
stepper1.moveTo(stepper1step); | |
} | |
if (note == 11 || note == 12 || note == 10 || note == 6){ | |
stepper2.setCurrentPosition(0); | |
stepper2.moveTo(stepper2step); | |
} | |
if (note == 4 || note == 5){ | |
stepper3.setCurrentPosition(0); | |
stepper3.moveTo(stepper3step); | |
} | |
if (note == 6 || note == 7){ | |
stepper4.setCurrentPosition(0); | |
stepper4.moveTo(stepper4step); | |
} | |
if (note == 8 || note == 9){ | |
stepper5.setCurrentPosition(0); | |
stepper5.moveTo(stepper5step); | |
} | |
// MIDI THRU: Send the note thru to MIDI OUT | |
midi_noteOn(midi_chn, midi_note, midi_velocity); | |
} | |
// This function is called whenever a note is released on the MIDI IN port | |
void on_midi_noteOff(uint8_t midi_chn, uint8_t midi_note, uint8_t midi_velocity) { | |
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
// Eine note wurde losgelassen. Hier koennt ihr die Tonerzeugung stoppen | |
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
Serial.print("Note OFF: "); | |
Serial.println(midi_note); | |
//Serial.print(NOTE_NAMES[midi_note % 12]); | |
//Serial.println(midi_note / 12); | |
// MIDI THRU: Send the noteOff thru to MIDI OUT | |
//midi_noteOff(midi_chn, midi_note, midi_velocity); | |
} | |
void setup() { | |
midi_init(); // Start MIDI | |
Serial.begin(9600); | |
stepper1.setMaxSpeed(1000.0); | |
stepper1.setAcceleration(2000.0); | |
stepper1.setSpeed(250); | |
stepper2.setMaxSpeed(1000.0); | |
stepper2.setAcceleration(2000.0); | |
stepper2.setSpeed(250); | |
stepper3.setMaxSpeed(1000.0); | |
stepper3.setAcceleration(2000.0); | |
stepper3.setSpeed(250); | |
stepper4.setMaxSpeed(1000.0); | |
stepper4.setAcceleration(2000.0); | |
stepper4.setSpeed(250); | |
stepper5.setMaxSpeed(1000.0); | |
stepper5.setAcceleration(2000.0); | |
stepper5.setSpeed(250); | |
}//--(end setup )--- | |
int loopnote = 1; | |
void loop() { | |
if (demo == true) { | |
Serial.print("Playing note #"); | |
Serial.println(loopnote); | |
on_midi_noteOn(0, loopnote, 200); | |
if (loopnote == 12) { | |
loopnote = 1; | |
} else { | |
loopnote++; | |
} | |
} | |
midi_update(); // Update MIDI | |
loopToPosition(); | |
if (demo == true) { | |
delay(1000); | |
} | |
} | |
void loopToPosition() { | |
while (stepper1.distanceToGo() != 0) { | |
stepper1.run(); | |
} | |
while (stepper2.distanceToGo() != 0) { | |
stepper2.run(); | |
} | |
while (stepper3.distanceToGo() != 0) { | |
stepper3.run(); | |
} | |
while (stepper4.distanceToGo() != 0) { | |
stepper4.run(); | |
} | |
while (stepper5.distanceToGo() != 0) { | |
stepper5.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment