Hand Cranked 3D Printed Electronic Music Box

Here you can find the 3D model with downloadable parts so you can make your own:

Music Box

And here is the arduino code, it’s a remix of the Sparkfun example:
int negpin1 = 52;
int negpin2 = 50;
int negpin3 = 48;
int negpin4 = 46;
int negpin5 = 44;
int negpin6 = 42;
int negpin7 = 40;
int negpin8 = 38;

int rueda1 = 53;
int rueda2 = 51;
int rueda3 = 49;
int rueda4 = 47;
int rueda5 = 45;
int rueda6 = 43;
int rueda7 = 41;
int rueda8 = 39;
int released1=0;
int released2=0;
int released3=0;
int released4=0;
int released5=0;
int released6=0;
int released7=0;
int released8=0;

int volume =127;

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

byte note = 0; //The MIDI note value to be played
byte resetMIDI = 4; //Tied to VS1053 Reset line
byte ledPin = 13; //MIDI traffic inidicator
int instrument = 0;

void setup() {
Serial.begin(57600);

//Setup soft serial for MIDI control
mySerial.begin(31250);

//Reset the VS1053
pinMode(resetMIDI, OUTPUT);
digitalWrite(resetMIDI, LOW);
delay(100);
digitalWrite(resetMIDI, HIGH);
delay(100);
talkMIDI(0xB0, 0x07, 120); //0xB0 is channel message, set channel volume to near max (127)

pinMode(negpin1, OUTPUT);
pinMode(negpin2, OUTPUT);
pinMode(negpin3, OUTPUT);
pinMode(negpin4, OUTPUT);
pinMode(negpin5, OUTPUT);
pinMode(negpin6, OUTPUT);
pinMode(negpin7, OUTPUT);
pinMode(negpin8, OUTPUT);

pinMode(rueda1, INPUT_PULLUP);
pinMode(rueda2, INPUT_PULLUP);
pinMode(rueda3, INPUT_PULLUP);
pinMode(rueda4, INPUT_PULLUP);
pinMode(rueda5, INPUT_PULLUP);
pinMode(rueda6, INPUT_PULLUP);
pinMode(rueda7, INPUT_PULLUP);
pinMode(rueda8, INPUT_PULLUP);

digitalWrite(negpin1, LOW);
digitalWrite(negpin2, LOW);
digitalWrite(negpin3, LOW);
digitalWrite(negpin4, LOW);
digitalWrite(negpin5, LOW);
digitalWrite(negpin6, LOW);
digitalWrite(negpin7, LOW);
digitalWrite(negpin8, LOW);
}
void bateria()
{
//talkMIDI(0xB0, 0, 0x00); //Default bank GM1
talkMIDI(0xB0, 0, 0x78); //Bank select drums

instrument = 10;

talkMIDI(0xC0, instrument, 0);

if (!digitalRead(rueda1) && released1)
{
noteOn(0, 59, volume);
released1=false;
}

if (!digitalRead(rueda2)&& released2)
{
noteOn(0, 28, volume);
released2=false;
}
if (!digitalRead(rueda3)&& released3)
{
noteOn(0, 76, volume);
released3=false;
}
if (!digitalRead(rueda4)&& released4)
{
noteOn(0, 69, volume);
released4=false;
}
if (!digitalRead(rueda5)&& released5)
{
noteOn(0, 44, volume);
released5=false;
}
if (!digitalRead(rueda6)&& released6)
{
noteOn(0, 46, volume);
released6=false;
}
if (!digitalRead(rueda7)&& released7)
{
noteOn(0, 38, volume);
released7=false;
}
if (!digitalRead(rueda8)&& released8)
{
noteOn(0, 35, volume);
released8=false;
}

}

void piano()
{
talkMIDI(0xB0, 0, 0x00); //Default bank GM1
//talkMIDI(0xB0, 0, 0x78); //Bank select drums

instrument = 1;

talkMIDI(0xC0, instrument, 0);

if (!digitalRead(rueda1) && released1)
{
noteOn(0, 77, volume);
released1=false;
}

if (!digitalRead(rueda2)&& released2)
{
noteOn(0, 76, volume);
released2=false;
}
if (!digitalRead(rueda3)&& released3)
{
noteOn(0, 75, volume);
released3=false;
}
if (!digitalRead(rueda4)&& released4)
{
noteOn(0, 74, volume);
released4=false;
}
if (!digitalRead(rueda5)&& released5)
{
noteOn(0, 73, volume);
released5=false;
}
if (!digitalRead(rueda6)&& released6)
{
noteOn(0, 72, volume);
released6=false;
}
if (!digitalRead(rueda7)&& released7)
{
noteOn(0, 71, volume);
released7=false;
}
if (!digitalRead(rueda8)&& released8)
{
noteOn(0, 70, volume);
released8=false;
}

}
void bass()
{
talkMIDI(0xB0, 0, 0x00); //Default bank GM1
//talkMIDI(0xB0, 0, 0x78); //Bank select drums

instrument = 108;

talkMIDI(0xC0, instrument, 0);

if (!digitalRead(rueda1) && released1)
{
noteOn(0, 47, volume);
released1=false;
}

if (!digitalRead(rueda2)&& released2)
{
noteOn(0, 46, volume);
released2=false;
}
if (!digitalRead(rueda3)&& released3)
{
noteOn(0, 45, volume);
released3=false;
}
if (!digitalRead(rueda4)&& released4)
{
noteOn(0, 44, volume);
released4=false;
}
if (!digitalRead(rueda5)&& released5)
{
noteOn(0, 43, volume);
released5=false;
}
if (!digitalRead(rueda6)&& released6)
{
noteOn(0, 42, volume);
released6=false;
}
if (!digitalRead(rueda7)&& released7)
{
noteOn(0, 41, volume);
released7=false;
}
if (!digitalRead(rueda8)&& released8)
{
noteOn(0, 40, volume);
released8=false;
}

}
void combi()
{
talkMIDI(0xB0, 0, 0x00); //Default bank GM1
//talkMIDI(0xB0, 0, 0x78); //Bank select drums

instrument = 45;

talkMIDI(0xC0, instrument, 0);

if (!digitalRead(rueda1) && released1)
{
noteOn(0, 47, volume);
released1=false;
}

if (!digitalRead(rueda2)&& released2)
{
noteOn(0, 46, volume);
released2=false;
}
if (!digitalRead(rueda3)&& released3)
{
noteOn(0, 45, volume);
released3=false;
}
if (!digitalRead(rueda4)&& released4)
{
noteOn(0, 44, volume);
released4=false;
}
if (!digitalRead(rueda5)&& released5)
{
noteOn(0, 43, volume);
released5=false;
}
if (!digitalRead(rueda6)&& released6)
{
noteOn(0, 42, volume);
released6=false;
}
if (!digitalRead(rueda7)&& released7)
{
talkMIDI(0xB0, 0, 0x78); //Bank select drums
instrument = 108;
talkMIDI(0xC0, instrument, 0);
noteOn(0, 38, volume);
released7=false;
}
if (!digitalRead(rueda8)&& released8)
{
talkMIDI(0xB0, 0, 0x78); //Bank select drums
instrument = 108;
talkMIDI(0xC0, instrument, 0);
noteOn(0, 35, volume);
released8=false;
}

}
void guitar()
{
talkMIDI(0xB0, 0, 0x00); //Default bank GM1
//talkMIDI(0xB0, 0, 0x78); //Bank select drums

instrument = 28;

talkMIDI(0xC0, instrument, 0);

if (!digitalRead(rueda1) && released1)
{
noteOn(0, 69, volume);
released1=false;
}

if (!digitalRead(rueda2)&& released2)
{
noteOn(0, 68, volume);
released2=false;
}
if (!digitalRead(rueda3)&& released3)
{
noteOn(0, 67, volume);
released3=false;
}
if (!digitalRead(rueda4)&& released4)
{
noteOn(0, 66, volume);
released4=false;
}
if (!digitalRead(rueda5)&& released5)
{
noteOn(0, 65, volume);
released5=false;
}
if (!digitalRead(rueda6)&& released6)
{
noteOn(0, 64, volume);
released6=false;
}
if (!digitalRead(rueda7)&& released7)
{
noteOn(0, 63, volume);
released7=false;
}
if (!digitalRead(rueda8)&& released8)
{
noteOn(0, 62, volume);
released8=false;
}

}
void loop() {

bateria();

if (digitalRead(rueda1))
{
released1=true;
}
if (digitalRead(rueda2))
{
released2=true;
}
if (digitalRead(rueda3))
{
released3=true;
}
if (digitalRead(rueda4))
{
released4=true;
}
if (digitalRead(rueda5))
{
released5=true;
}
if (digitalRead(rueda6))
{
released6=true;
}
if (digitalRead(rueda7))
{
released7=true;
}
if (digitalRead(rueda8))
{
released8=true;
}

}

//Send a MIDI note-on message. Like pressing a piano key
//channel ranges from 0-15
void noteOn(byte channel, byte note, byte attack_velocity) {
talkMIDI( (0x90 | channel), note, attack_velocity);
}

//Send a MIDI note-off message. Like releasing a piano key
void noteOff(byte channel, byte note, byte release_velocity) {
talkMIDI( (0x80 | channel), note, release_velocity);
}

//Plays a MIDI note. Doesn’t check to see that cmd is greater than 127, or that data values are less than 127
void talkMIDI(byte cmd, byte data1, byte data2) {
digitalWrite(ledPin, HIGH);
mySerial.write(cmd);
mySerial.write(data1);

//Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes
//(sort of: http://253.ccarh.org/handout/midiprotocol/)
if( (cmd & 0xF0) <= 0xB0)
mySerial.write(data2);

digitalWrite(ledPin, LOW);
}

 

 

Share Post :

More Posts