Make Music With Stepper Motors!

Introduction: Make Music With Stepper Motors!

About: Unknown engineer

Have you seen those videos where the music was played by rotating motors?

I have, so i wanted to make my own 'motor music player'.

But when I started to search for some tutorials or information of how to make something like this I realized that i can't find anything! =(
That was really sad, so I had to figure out the entire process by myself. And i succeed, so today I'll show you how YOU can make music with stepper motors!

Step 1: Motors

First, you need a stepper motor. You can find it in printers, scanners, floppy drives and some other devices.
You can recognize them by many wires going into the motor, but you need the only one with 4.

Step 2: Materials

Beside the motor you need a bunch of other materials. Here they are:

- A4988 driver

- Arduino board (i used uno)

- 9-12v power supply

- Breadboard

- A bunch of wires to connect everything together

- 47mkF capacitor

Step 3: Assembling the Driver

We are going to control stepper motor with a4988 driver, so check this video for more information:
You need to assemble everything together like in the video, but in case i use single power supply here are my wiring diagrams.

Step 4: Connect Motors

Before connecting motors you need to check the terminals with multimeter and recognize two pairs of shorted wires, each pair for each coil in the motor.

Only then connect them to the circuit.

Step 5: What About Notes?

Yeah, we are almost there.

As you can see from the video we are controlling motors by continuously sending pulses to the driver, and the speed of rotation depends on duration of the pause between them (remember it!).

Now let's move to the musical part of our trip.

As you know, every note has its own frequency which you can find in a special table shown in the picture. Knowing the frequency we can calculate the period, which is 1/f (f - frequency).

And here the main trick comes. Remember the duration of a pause between pulses? This pause is actually a period of the note you hear from rotating. Very exciting, isn't it?

Step 6: Code

Now we can simply control the period to get notes we want from our stepper motor.

And here my 'genius' code comes.

// defines pins numbers
const int stepPin = 3; 
const int dirPin = 4; 

// here comes a bunch of 'useful' vars; dont mind
int coun;
bool dir=0;
int del;
int c=1912;
int cf=1805;
int d= 1703;
int df=1607;
int e=1517;
int f=1431;
int ff=1351;
int g= 1275;
int gf=1203;
int a=1136;
int af=1072;
int b=1012;
int c1=floor(c/2);
int cf1=floor(cf/2);
int d1=floor(d/2);
int df1=floor(df/2);
int e1=floor(e/2);
int f1=floor(1431/2);
int ff1=floor(1351/2);
int g1= floor(1275/2);
int gf1=floor(1203/2);
int a1=floor(1136/2);
int af1=floor(1072/2);
int b1=floor(1012/2);
int e0=e*2;
int g0=g*2;
int b0=b*2;
int af0=af*2;
int a0=a*2;
int f0=f*2;
int use=180;
int tempo=120;
int oct=5;

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
}
void loop() {
  oct=5;
// Here you put your music code

}

void note(int num,long dur) {
  del=(num*oct)/10;
  dir=!dir;
 digitalWrite(dirPin,dir);
  coun=floor((dur*5*tempo)/del);
  for(int x = 0; x < coun; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(del);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(del);
  }

}

void pa(int durp){
  int ker=floor(durp/100)*tempo;
delay(ker);
  
  }
  
  

Now use these functions to create MuSiC:

- note(num,dur)

num - the note you want to play (variable that contains the value of period), the list of them is at the top of this sketch.
dur - duration of the note in milliseconds (1000 = 1 sec)

- pa(durp) (pause between notes)

durp - duration of pause in milliseconds


Also use the 'tempo' var to change the tempo.


That is all!

Step 7: Final Result

This is how the thing sounds.

And here is one of my 'music codes'

note(e1,250);
note(e1,500);
note(e1,250);
pa(250);
note(c1,250);
note(e1,500);
note(g1,1000);
note(g,1000);
note(c1,500);
pa(250);
note(g,250);
pa(500);
note(e,500);
pa(250);
note(a,250);
pa(250);
note(b,250);
pa(250);
note(af,250);
note(a,500);
note(g,330);
note(e1,330);
note(g1,330);
note(a1,500);
note(f1,250);
note(g1,250);
pa(250);
note(e1,250);
pa(250);
note(c1,250);
note(d1,250);
note(b,250);
pa(1000);

Rob scallon Rain.

for (int i = 0; i < 3; i++){
note(d,100); pa(use); note(f,100); pa(use); note(c1,100); pa(use); note(f,100); pa(use);} note(c1,100); pa(use); note(c1,100); pa(use); note(af,100); pa(use); note(a,100); pa(use); for (int i = 0; i < 3; i++){ note(c,100); pa(use); note(e,100); pa(use); note(af,100); pa(use); note(e,100); pa(use);} note(af,100); pa(use); note(af,100); pa(use); note(a,100); pa(use); note(f,100); pa(use); for (int i = 0; i < 3; i++){ note(d,100); pa(use); note(f,100); pa(use); note(af,100); pa(use); note(f,100); pa(use);} for (int i = 0; i < 3; i++){ note(af0,100); pa(use); note(d,100); pa(use); note(f,100); pa(use); note(a,100); pa(use);}

Try it!

Step 8: Conclusion

We made it through! Now you know how you can make music with stepper motors!
I just gave you the main information, now is your turn to create!

I hope you enjoyed, and good luck! Thanks for reading!


P.S. A single motor may sound quite, so try to stick it to some surface and it become loud!

=)

Instrument Contest

Participated in the
Instrument Contest

Be the First to Share

    Recommendations

    • Plastic Challenge

      Plastic Challenge
    • Tinkercad Student Design Contest

      Tinkercad Student Design Contest
    • Rocks, Gems, and Stones Speed Challenge

      Rocks, Gems, and Stones Speed Challenge

    19 Comments

    0
    EpicCrowd101
    EpicCrowd101

    2 months ago

    Hi, I want to adapt you code a bit but I am stuck on a technical aspect of how the noise is made, if I have a very specific frequency I want the stepper to play i.e. 294hz what calculation would I need to do to convert that to the number the stepper would recreate the sound? as many obvious detail as well plz incase I missed it, thanks in advance

    0
    Whiteshadow11
    Whiteshadow11

    Reply 2 months ago

    Hi! In this code i used A4988 drivers, and they are just turning on and off the motor at the certain frequency and direction. So that's what you need: you got the frequency (294) , now just convert it in time periods like 1000/294. And you got the pulse duration in milliseconds, which is 3.4 for this case. Now just turn on the motor for this period, then turn off for the same period, turn on, turn off... and so on and you'll get the sound.
    I wonder if there is more advanced way driving stepper motors with a4988, but all my knowledge ends with that shitcode you saw above =)

    0
    motor24v
    motor24v

    Question 5 months ago on Step 6

    Hi Whiteshadow11,

    First of all ,this is a wonderful project, let me congratulate on you putting it together.
    Now, I understand motors, but I am a beginner on programming. I lost my grasp on the subject after Step7.
    My question is: how do you exactly go from a MIDI file to this gcode (I guess?) music? I see you created the commands for the notes, but how do you convert MIDI channel information into these commands? Is it manual work or there is an automated script or something?
    Thanks a lot in advance!

    0
    Whiteshadow11
    Whiteshadow11

    Answer 4 months ago

    Hi! Sorry for not replying for so long.
    I didn't use any algorithms, just watched the notes in synthesia (visual midi synth) and then write them in program. For automatization i think there is much better and optimized code needed. I made this project for understanding the basics of connecting motors and music =) and for fun of course

    0
    handyharris17
    handyharris17

    Question 6 months ago

    Hi, this is Austin again, I just want to know how the code works, I don't really understand half of it. Like, what I am specifically confused on is the calculations and the note frequencies. When I am looking at the chart, I get confused because the note frequencies in the code do not match. So, if you may provide some more detail on the code, then I may understand.

    -Thanks,

    Austin

    0
    Whiteshadow11
    Whiteshadow11

    Answer 6 months ago

    I started to understand. The note frequencies dont match to the code, because in the chart there are actually frequencies, while in code we need to find periods to control the motors. Period is the opposite to the freq., T=1/f. I think i took 5th octave as a base and just did 1/523 for the C note, which gives us 1912 because arduino processor counts in microseconds.

    0
    handyharris17
    handyharris17

    Reply 6 months ago

    But, I guess I can see where you put the decimal three places to the right. Thanks, I am trying to make this for guitar notes, so I got confused on how you reached that conclusion.

    0
    handyharris17
    handyharris17

    Reply 6 months ago

    Hey, what about the

    void note(int num, long dur){
    del = (num * oct) / 10;
    dir = !dir;
    digitalWrite(dirPin, dir);
    coun = floor((dur * 5 * tempo) / del);
    for (int x = 0; x < coun; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(del);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(del);
    }
    }

    How does that work? I think I understand part of it, but I don't know how to implement it with guitar notes, such as this:

    /*
    * *GUITAR NOTES ONLY* This is basically a stepper motor
    motor music program to play Megalovania. Resources:
    https://www.youtube.com/watch?v=0FCvzsVlXpQ
    https://www.youtube.com/watch?v=65HidYKtwxU&t=18s
    http://www.valvewizard.co.uk/FretFreq.jpg
    https://www.instructables.com/Make-Music-With-Stepper-Motors/#discuss
    https://www.checkyourmath.com/convert/time/milliseconds_microseconds.php
    by UptownKitten453 or HandyHarris17
    email: uptownkitten453@gmail
    */
    // defines pins numbers
    const int stepPin = 2;
    const int dirPin = 3;
    // here comes a bunch of 'useful' vars; dont mind
    int coun;
    bool dir = 0;
    int del;
    int tempo = 240;
    // ---------------- High E String Section -------------------------
    int highE0 = 3030;
    int highE1 = 2865;
    int highE2 = 2703;
    int highE3 = 2551;
    int highE4 = 2410;
    int highE5 = 2273;
    int highE6 = 2146;
    int highE7 = 2024;
    int highE8 = 1912;
    int highE9 = 1805;
    int highE10 = 1704;
    // --------- B String -----------------------------------------------------
    int B0 = 4049;
    int B1 = 3817;
    int B2 = 3610;
    int B3 = 3401;
    int B4 = 3215;
    /*
    int B5 = 3030; == int E0 = 3030;
    int B6 = 2865; == int E1 = 2865;
    etc...
    */
    // ------------- G String ---------------------------------
    int G0 = 5102;
    int G1 = 4808;
    int G2 = 4546;
    int G3 = 4292;
    /*
    int G4 = 4049; == int B0 = 4049;
    int G5 = 3817; == int B1 = 3817;
    etc...
    */
    // ---------------- D String -------------------------------
    int D0 = 6803;
    int D1 = 6410;
    int D2 = 6061;
    int D3 = 5714;
    int D4 = 5405;
    /*
    * int D5 = 5102; == int G0 = 5102;
    * int D6 = 4808; == int G1 = 4808;
    * etc...
    */
    // ---------------- A String -------------------------------
    int A0 = 9091;
    int A1 = 8547;
    int A2 = 8130;
    int A3 = 7634;
    int A4 = 7194;
    /*
    * int A5 = 6803; == int D0 = 6803;
    * int A6 = 6410; == int D1 = 6410;
    * etc...
    */
    // ---------------- Low E String ---------------------------
    int lowE0 = 12200;
    int lowE1 = 11490;
    int lowE2 = 10870;
    int lowE3 = 10200;
    int lowE4 = 9615;
    /*
    * int lowE5 = 9091; == int A0 = 9091;
    * int lowE6 = 8547; == int A1 = 8547;
    * etc...
    */
    void setup() {
    // put your setup code here, to run once:
    // Sets the two pins as Outputs
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    }

    0
    handyharris17
    handyharris17

    Reply 6 months ago

    I guess the frets are the same as the octaves? Would I have to replicate the part where you have the octave coun and del part for every fret? How do I make the note the same duration of the music?

    0
    handyharris17
    handyharris17

    Reply 6 months ago

    This is the best I could come up with for the guitar, trying the piano this time because it worked like half way through:

    /*
    * *GUITAR NOTES ONLY* This is basically a stepper motor
    motor music program to play Megalovania. Resources:
    https://www.youtube.com/watch?v=0FCvzsVlXpQ
    https://www.youtube.com/watch?v=65HidYKtwxU&t=18s
    http://www.valvewizard.co.uk/FretFreq.jpg
    https://www.instructables.com/Make-Music-With-Stepper-Motors/#discuss
    https://www.checkyourmath.com/convert/time/milliseconds_microseconds.php
    by UptownKitten453 or HandyHarris17
    email: uptownkitten453@gmail
    */
    // defines pins numbers
    const int stepPin = 2;
    const int dirPin = 3;
    // here comes a bunch of 'useful' vars; dont mind
    int coun;
    bool dir = 1;
    int del;
    int tempo = 240;
    int use = 180;
    // ---------------- High E String Section -------------------------
    int highE0 = 3030;
    int highE1 = 2865;
    int highE2 = 2703;
    int highE3 = 2551;
    int highE4 = 2410;
    int highE5 = 2273;
    int highE6 = 2146;
    int highE7 = 2024;
    int highE8 = 1912;
    int highE9 = 1805;
    int highE10 = 1704;
    // --------- B String -----------------------------------------------------
    int stringB0 = 4049; //Had to fix the names because it gave me some problems when varifying.
    int stringB1 = 3817;
    int stringB2 = 3610;
    int stringB3 = 3401;
    int stringB4 = 3215;
    /*
    int stringB5 = 3030; == int E0 = 3030;
    int stringB6 = 2865; == int E1 = 2865;
    etc...
    */
    // ------------- G String ---------------------------------
    int G0 = 5102;
    int G1 = 4808;
    int G2 = 4546;
    int G3 = 4292;
    /*
    int G4 = 4049; == int B0 = 4049;
    int G5 = 3817; == int B1 = 3817;
    etc...
    */
    // ---------------- D String -------------------------------
    int D0 = 6803;
    int D1 = 6410;
    int D2 = 6061;
    int D3 = 5714;
    int D4 = 5405;
    /*
    int D5 = 5102; == int G0 = 5102;
    int D6 = 4808; == int G1 = 4808;
    etc...
    */
    // ---------------- A String -------------------------------
    int stringA0 = 9091; //Had to fix the names because it gave me some problems when varifying.
    int stringA1 = 8547;
    int stringA2 = 8130;
    int stringA3 = 7634;
    int stringA4 = 7194;
    /*
    int A5 = 6803; == int D0 = 6803;
    int A6 = 6410; == int D1 = 6410;
    etc...
    */
    // ---------------- Low E String ---------------------------
    int lowE0 = 12200;
    int lowE1 = 11490;
    int lowE2 = 10870;
    int lowE3 = 10200;
    int lowE4 = 9615;
    /*
    int lowE5 = 9091; == int A0 = 9091;
    int lowE6 = 8547; == int A1 = 8547;
    etc...
    */
    // zeros out
    // int oct1 = 0;
    int oct2 = 1;
    int oct3 = 2;
    int oct4 = 3;
    int oct5 = 4;
    int oct6 = 5;
    int oct7 = 6;
    int oct8 = 7;
    int oct9 = 8;
    int oct10 = 9;
    int oct11 = 10;
    int wholeNote = 1000;
    int halfNote = 500;
    int quarterNote = 250;
    int eigthNote = 125;
    int eigthTriplet = 83;
    int sixteenthNote = 63;
    int sixteenthNoteTriplet = 42;
    int thirtysecondthNote = 32;
    int thirtysecondthNoteTriplet = 21;
    void setup() {
    // put your setup code here, to run once:
    // Sets the two pins as Outputs
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    for(int i = 0; i <1; i++){
    note(D0, sixteenthNoteTriplet);
    pa(use);
    note(D0, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(stringB3, sixteenthNoteTriplet);
    pa(use);
    oct3 = 2;
    note(G2, sixteenthNoteTriplet);
    pa(use);
    oct2 = 1;
    note(G1, sixteenthNoteTriplet);
    pa(use);
    note(G0, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(D3, sixteenthNoteTriplet);
    pa(use);
    note(D0, sixteenthNoteTriplet);
    pa(use);
    note(D3, sixteenthNoteTriplet);
    pa(use);
    note(G0, 83);
    pa(use);
    note(stringA3, sixteenthNoteTriplet);
    pa(use);
    note(stringA3, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(stringB3, sixteenthNoteTriplet);
    pa(use);
    oct3 = 2;
    note(G2, sixteenthNoteTriplet);
    pa(use);
    oct2 = 1;
    note(G1, sixteenthNoteTriplet);
    pa(use);
    note(G0, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(D3, sixteenthNoteTriplet);
    pa(use);
    note(D0, sixteenthNoteTriplet);
    pa(use);
    note(D3, sixteenthNoteTriplet);
    pa(use);
    note(G0, 83);
    pa(use);
    oct3 = 2;
    note(stringA2, sixteenthNoteTriplet);
    note(stringA2, sixteenthNoteTriplet);
    oct4 = 3;
    note(stringB3, sixteenthNoteTriplet);
    pa(use);
    oct3 = 2;
    note(G2, sixteenthNoteTriplet);
    pa(use);
    oct2 = 1;
    note(G1, sixteenthNoteTriplet);
    pa(use);
    note(G0, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(D3, sixteenthNoteTriplet);
    pa(use);
    note(D0, sixteenthNoteTriplet);
    pa(use);
    note(D3, sixteenthNoteTriplet);
    pa(use);
    note(G0, 83);
    pa(use);
    oct2 = 1;
    note(stringA1, sixteenthNoteTriplet);
    note(stringA1, sixteenthNoteTriplet);
    oct4 = 3;
    note(stringB3, sixteenthNoteTriplet);
    pa(use);
    oct3 = 2;
    note(G2, sixteenthNoteTriplet);
    pa(use);
    oct2 = 1;
    note(G1, sixteenthNoteTriplet);
    pa(use);
    note(G0, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(D3, sixteenthNoteTriplet);
    pa(use);
    note(D0, sixteenthNoteTriplet);
    pa(use);
    note(D3, sixteenthNoteTriplet);
    pa(use);
    note(G0, 83);
    pa(use);
    }
    oct2 = 1;
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(stringB3, sixteenthNoteTriplet);
    pa(use);
    note(stringB3, sixteenthNoteTriplet);
    pa(use);
    oct2 = 1;
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(highE3, sixteenthNoteTriplet);
    pa(use);
    oct5 = 4;
    note(highE4, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(highE3, sixteenthNoteTriplet);
    pa(use);
    oct5 = 4;
    note(highE4, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(highE3, sixteenthNoteTriplet);
    pa(use);
    oct2 = 1;
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(stringB3, sixteenthNoteTriplet);
    pa(use);
    oct2 = 1;
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(highE3, sixteenthNoteTriplet);
    pa(use);
    oct2 = 1;
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    note(highE1, sixteenthNoteTriplet);
    pa(use);
    oct4 = 3;
    note(highE3, sixteenthNoteTriplet);
    pa(use);
    oct5 = 4;
    note(highE4, sixteenthNoteTriplet);
    pa(use);
    oct6 = 5;
    note(highE5, sixteenthNoteTriplet);
    pa(use);
    oct9 = 8;
    note(highE8, sixteenthNoteTriplet);
    pa(use);
    oct6 = 5;
    note(highE5, sixteenthNoteTriplet);
    pa(use);
    oct11 = 10;
    note(highE10, sixteenthNoteTriplet);
    pa(use);
    note(highE10, sixteenthNoteTriplet);
    pa(use);
    note(highE10, sixteenthNoteTriplet);
    pa(use);
    oct6 = 5;
    note(highE5, sixteenthNoteTriplet);
    pa(use);
    oct11 = 10;
    note(highE10, sixteenthNoteTriplet);
    oct9 = 8;
    note(highE8, sixteenthNoteTriplet);
    }
    void note(int num, long dur) {
    // this zeros out
    // del = (num * oct1) / 10;
    del = (num * oct2) / 10;
    del = (num * oct3) / 10;
    del = (num * oct4) / 10;
    del = (num * oct5) / 10;
    del = (num * oct6) / 10;
    del = (num * oct7) / 10;
    del = (num * oct8) / 10;
    del = (num * oct9) / 10;
    del = (num * oct10) / 10;
    del = (num * oct11) / 10;
    digitalWrite(dirPin, dir);
    // zeros out
    // coun = floor((dur * oct1 * tempo) / del);
    coun = floor((dur * oct2 * tempo) / del);
    coun = floor((dur * oct3 * tempo) / del);
    coun = floor((dur * oct4 * tempo) / del);
    coun = floor((dur * oct5 * tempo) / del);
    coun = floor((dur * oct6 * tempo) / del);
    coun = floor((dur * oct7 * tempo) / del);
    coun = floor((dur * oct8 * tempo) / del);
    coun = floor((dur * oct9 * tempo) / del);
    coun = floor((dur * oct10 * tempo) / del);
    coun = floor((dur * oct11 * tempo) / del);
    for (int x = 0; x < coun; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(del);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(del);
    }
    }
    void pa(int durp) {
    int ker = floor(durp / 100) * tempo;
    delay(ker);
    }

    0
    Whiteshadow11
    Whiteshadow11

    Answer 6 months ago

    Ah, that code is a disaster. It was so long ago i dont remember what was in my mind while making it. But i'll try to help you. So what actually is not working, or you just have troubles in understanding?

    0
    handyharris17
    handyharris17

    6 months ago

    Hi, I had some issues with the code above, may you, please, help?

    Thanks,

    Austin

    0
    Whiteshadow11
    Whiteshadow11

    Reply 6 months ago

    Hi! What kind of error do you get? Can you send a screenshot?

    0
    handyharris17
    handyharris17

    Reply 6 months ago

    Figured out the solution (below). Thanks, though!

    0
    handyharris17
    handyharris17

    Reply 6 months ago

    Okay, nevermind. This code seemed to work better (my loop is doing something funny):

    // defines pins numbers
    const int stepPin = 2;
    const int dirPin = 3;
    // here comes a bunch of 'useful' vars; dont mind
    int coun;
    bool dir=0;
    int del;
    int c=1912;
    int cf=1805;
    int d= 1703;
    int df=1607;
    int e=1517;
    int f=1431;
    int ff=1351;
    int g= 1275;
    int gf=1203;
    int a=1136;
    int af=1072;
    int b=1012;
    int c1=floor(c/2);
    int cf1=floor(cf/2);
    int d1=floor(d/2);
    int df1=floor(df/2);
    int e1=floor(e/2);
    int f1=floor(1431/2);
    int ff1=floor(1351/2);
    int g1= floor(1275/2);
    int gf1=floor(1203/2);
    int a1=floor(1136/2);
    int af1=floor(1072/2);
    int b1=floor(1012/2);
    int e0=e*2;
    int g0=g*2;
    int b0=b*2;
    int af0=af*2;
    int a0=a*2;
    int f0=f*2;
    int use=180;
    int tempo=120;
    int oct=5;
    void setup() {
    // Sets the two pins as Outputs
    pinMode(stepPin,OUTPUT);
    pinMode(dirPin,OUTPUT);
    }
    void loop() {
    oct=5;
    // Here you put your music code
    note(e1,250);
    note(e1,500);
    note(e1,250);
    pa(250);
    note(c1,250);
    note(e1,500);
    note(g1,1000);
    note(g,1000);
    note(c1,500);
    pa(250);
    note(g,250);
    pa(500);
    note(e,500);
    pa(250);
    note(a,250);
    pa(250);
    note(b,250);
    pa(250);
    note(af,250);
    note(a,500);
    note(g,330);
    note(e1,330);
    note(g1,330);
    note(a1,500);
    note(f1,250);
    note(g1,250);
    pa(250);
    note(e1,250);
    pa(250);
    note(c1,250);
    note(d1,250);
    note(b,250);
    pa(1000);
    //Rob scallon Rain.
    for (int i = 0; i < 3; i++){
    note(d,100);
    pa(use);
    note(f,100);
    pa(use);
    note(c1,100);
    pa(use);
    note(f,100);
    pa(use);}
    note(c1,100);
    pa(use);
    note(c1,100);
    pa(use);
    note(af,100);
    pa(use);
    note(a,100);
    pa(use);
    runSM();
    }
    void runSM(){
    for (int i = 0; i < 3; i++){
    note(c,100);
    pa(use);
    note(e,100);
    pa(use);
    note(af,100);
    pa(use);
    note(e,100);
    pa(use);}
    note(af,100);
    pa(use);
    note(af,100);
    pa(use);
    note(a,100);
    pa(use);
    note(f,100);
    pa(use);
    for (int i = 0; i < 3; i++){
    note(d,100);
    pa(use);
    note(f,100);
    pa(use);
    note(af,100);
    pa(use);
    note(f,100);
    pa(use);}
    for (int i = 0; i < 3; i++){
    note(af0,100);
    pa(use);
    note(d,100);
    pa(use);
    note(f,100);
    pa(use);
    note(a,100);
    pa(use);}
    }
    void note(int num,long dur) {
    del=(num*oct)/10;
    dir=!dir;
    digitalWrite(dirPin,dir);
    coun=floor((dur*5*tempo)/del);
    for(int x = 0; x < coun; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(del);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(del);
    }
    }
    void pa(int durp){
    int ker=floor(durp/100)*tempo;
    delay(ker);
    }


    0
    javimartinezmartinezpa
    javimartinezmartinezpa

    Question 1 year ago

    Hi! I loved ur project. I tried replicating it however I cant get the stepper motor to spin (they make a quiet buzzing sound) The only thing diferent is the stepper, Im using a Nema17 (usongshine us-17hs4401s). Is there something I can change on the code to make it work? Thanks!!

    0
    Whiteshadow11
    Whiteshadow11

    Answer 1 year ago

    Oh hi, im very vlad to see you interested in my project! So, firstly check your connections, these motors can buzz but not spin when there is wrong connection. Also it can be lack of power to start your motor, try to adjust the current on your a4988. Did you try to make it spin without this code? If you can find another motor, try with it. Good luck! hope to hear from you soon!

    0
    javimartinezmartinezpa
    javimartinezmartinezpa

    Reply 1 year ago

    Hi! Thanks for replying!, it ended up being that the driver I was using was broken... :(
    I just used another one and it worked up just fine. Now I'm trying to play 3 steppers at the same time but I'm struggling tho, I'm also trying to replicate Jonathan Kayne 's take on this kind of project but w/ no luck.
    Hopefully I solve the problems and get it working...
    Best wishes from Spain