Arduino + floppy music based of frequency.

Hi.

I've been playing today with my Arduino and a floppy disk drive doing some noise.
Almost anything i found is based on Moppy, which plays MIDI files from a computer on the fly.
So, i've been working on some code to play notes based on their frequency.

I used a semi-random formula to convert the frequency into floppy header vibrations, so i don't know if it plays in the supposed scale, from C3 to F#6

Any suggestions, or improvements will be welcomed.

Code is quite dirty and fast, i will improve it.

Main program:

#include "notes.h"

#define dirPin 4 //Direction pin
#define stepPin 6 //Step pin
#define track0 8 //Track 0 pin

void setup() {
  
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(track0, INPUT);
  
  digitalWrite(track0, HIGH); //Pull-up for track0
  
  goToCenter(); //Moves the head to the center
  
}

void loop() {
  
  //////////////////////////////////////
  // Imperial March
  /////////////////////////////////////
  
  vibrate(a4, 500);
  vibrate(a4, 500);
  vibrate(a4, 500);
  vibrate(f4, 350);
  vibrate(c5, 150);
  vibrate(a4, 500);
  vibrate(f4, 350);
  vibrate(c5, 150);
  vibrate(a4, 650);
  delay(150);
  
  vibrate(e5, 500);
  vibrate(e5, 500);
  vibrate(e5, 500);
  vibrate(f5, 350);
  vibrate(c5, 150);
  vibrate(gS4, 500);
  vibrate(f4, 350);
  vibrate(c5, 150);
  vibrate(a4, 650);
  delay(150);
  
  vibrate(a5, 500);
  vibrate(a4, 300);
  vibrate(a4, 150);
  vibrate(a5, 400);
  vibrate(gS5, 200);
  vibrate(g5, 200);
  vibrate(fS5, 125);
  vibrate(f5, 125);
  vibrate(fS5, 250);
  delay(250);
  
  vibrate(aS4, 250);
  vibrate(dS5, 400);
  vibrate(d5, 200);
  vibrate(cS5, 200);
  vibrate(c5, 125);
  vibrate(b4, 125);
  vibrate(c5, 250);
  delay(250);
  
  vibrate(f4, 125);
  vibrate(gS4, 500);
  vibrate(f4, 375);
  vibrate(a4, 125);
  vibrate(c5, 500);
  vibrate(a4, 375);
  vibrate(c5, 125);
  vibrate(e5, 650);
  delay(250);
  
  vibrate(a5, 500);
  vibrate(a4, 300);
  vibrate(a4, 150);
  vibrate(a5, 400);
  vibrate(gS5, 200);
  vibrate(g5, 200);
  vibrate(fS5, 125);
  vibrate(f5, 125);
  vibrate(fS5, 250);
  delay(250);
  
  vibrate(aS4, 250);
  vibrate(dS5, 400);
  vibrate(d5, 200);
  vibrate(cS5, 200);
  vibrate(c5, 125);
  vibrate(b4, 125);
  vibrate(c5, 250);
  delay(250);
  
  vibrate(f4, 125);
  vibrate(gS4, 500);
  vibrate(f4, 375);
  vibrate(a4, 125);
  vibrate(c5, 500);
  vibrate(a4, 375);
  vibrate(c5, 125);
  vibrate(e5, 650);
  delay(-1);
  
}

// Moves floppy header to the center, goes to track0 and then
// do 40 steps in the opposite direction
void goToCenter() {
  
  digitalWrite(dirPin, HIGH);
  
  while(digitalRead(track0) != LOW) {
    digitalWrite(stepPin, LOW);
    delay(1);
    digitalWrite(stepPin, HIGH);
    delay(1);
  }
  doSteps(LOW, 40, 1);
}

// Makes the header vibrate for a given time in a given frequency
void vibrate(float freq, float duration) {
  
  int tempo = 5; //Pause between notes
  
  float val = (3000000/freq)/2; //3000000 and 2 are random numbers by now
  float time = millis()+duration; //Calculates how much time the note sounds
  
  //Step forward-step backward loop
  while(millis()<time){
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(val);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(val);
    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(val);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(val);
   }
  delay(tempo);
}

// Does any given number of steps in a given direction
void doSteps(boolean dir, int steps, int stepDelay) {
  digitalWrite(dirPin, dir);
  for(int i=0;i<steps; i++) {
    digitalWrite(stepPin,LOW); 
    delay(stepDelay);
    digitalWrite(stepPin,HIGH); 
    delay(stepDelay);
  } 
}

Header file with musical notes definitions

///////////////////////////
// Notes for floppy
///////////////////////////

#define c3	 130.81
#define cS3	 138.59 //Db3
#define d3	 146.83
#define dS3	 155.56	//Eb3
#define e3	 164.81
#define f3	 174.61
#define fS3	 185.00 //Gb3
#define g3	 196.00
#define gS3	 207.65 //Ab3
#define a3	 220.00
#define aS3	 233.08 //Bb3
#define b3	 246.94
#define c4	 261.63
#define cS4	 277.18 //Db4
#define d4	 293.66
#define dS4/Eb4	 311.13 //Eb4
#define e4	 329.63
#define f4	 349.23
#define fS4	 369.99 //Gb4
#define g4	 392.00
#define gS4	 415.30	//Ab4
#define a4	 440.00
#define aS4	 466.16 //Bb4
#define b4	 493.88
#define c5	 523.25
#define cS5	 554.37 //Db5
#define d5	 587.33
#define dS5	 622.25 //Eb5
#define e5	 659.26
#define f5	 698.46
#define fS5	 739.99 //Gb5
#define g5	 783.99
#define gS5	 830.61 //Ab5
#define a5	 880.00
#define aS5	 932.33 //Bb5
#define b5	 987.77
#define c6	 1046.50
#define cS6	 1108.73 //Db6
#define d6	 1174.66
#define dS6	 1244.51 //Eb6
#define e6	 1318.51
#define f6	 1396.91
#define fS6	 1479.98 ///Gb6

And a video of it playing imperial march with the code (crappy audio, rise volume).

¡Thanks!