Created
February 8, 2015 15:52
-
-
Save rhbvkleef/a78f982f8fe9b4287a49 to your computer and use it in GitHub Desktop.
Arduino floppy drive music - Sinterklaas is jarig
This file contains hidden or 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
const int MUSIC_WAIT_TIME = 83; | |
const int MUSIC_BASE_TIME = 250; | |
const int MUSIC_INCREMENT_TIME = 333; | |
const int MUSIC_ARRAY_LENGTH = 61; | |
int dirPin = 2; | |
int stepPin = 3; | |
int dly = 1; | |
int maxSteps = 80; | |
int currStepPos = 0; | |
boolean done = false; | |
int music[] = {392, 392, 392, 392, 523, 0, 392, 0, 440, 440, 523, 494, 440, 392, 0, 294, 330, 350, 440, 392, 0, 330, 0, | |
294, 294, 523, 494, 440, 392, 0, 330, 350, 392, 440, 350, 0, 294, 0, 350, 392, 440, 494, 392, 0, | |
330, 392, 523, 494, 494, 0, 440, 0, 294, 330, 392, 350, 294, 262}; | |
float timing[] = {2, 1, 2, 1, 2, 0.5,2, 1, 2, 1, 1, 1, 1, 4, 2, 2, 1, 2, 1, 2, 0.5,2, 0.5, | |
2, 1, 1, 1, 1, 4, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, | |
2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 4}; | |
boolean state = false; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(dirPin, OUTPUT); | |
pinMode(stepPin, OUTPUT); | |
reset(); | |
} | |
void loop() { | |
if(done) return; | |
done = true; | |
for(int i = 0; i < MUSIC_ARRAY_LENGTH; i++) { | |
int tone_length = MUSIC_BASE_TIME + ((timing[i] - 1) * MUSIC_INCREMENT_TIME); | |
Serial.println("Tone ln: " + String(tone_length)); | |
playTone(music[i] / 2, float(tone_length) / float(1000)); | |
delay(MUSIC_WAIT_TIME); | |
} | |
} | |
void reset() { | |
state = false; | |
for(int i = 0; i < maxSteps; i++) { | |
digitalWrite(stepPin, true); | |
delay(1); | |
digitalWrite(stepPin, false); | |
delay(1); | |
} | |
state = true; | |
digitalWrite(stepPin, true); | |
delay(1); | |
digitalWrite(stepPin, false); | |
delay(1); | |
} | |
void playTone(int freq, float time) { | |
int iterations = time * freq; | |
Serial.println("Interations: " + String(iterations) + ", frequency: " + String(freq) + ", time: " + floatToStr(time, 2) + ", dlyMS: " + String(500000 / freq)); | |
for(int i = 0; i < iterations; i++) { | |
digitalWrite(stepPin, true); | |
delayMicroseconds(500000 / freq); | |
digitalWrite(stepPin, false); | |
delayMicroseconds(500000 / freq); | |
currStepPos++; | |
if(currStepPos >= maxSteps) { | |
currStepPos = 0; | |
state = !state; | |
digitalWrite(dirPin, state); | |
} | |
} | |
} | |
String floatToStr( float val, unsigned int precision){ | |
// prints val with number of decimal places determine by precision | |
// NOTE: precision is 1 followed by the number of zeros for the desired number of decimial places | |
// example: printDouble( 3.1415, 100); // prints 3.14 (two decimal places) | |
String fullStr = String(int(val)); | |
unsigned int frac; | |
if(val >= 0) | |
frac = (val - int(val)) * precision; | |
else | |
frac = (int(val)- val ) * precision; | |
fullStr = fullStr + "." + String(frac); | |
return fullStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment