JakeS,
I tried to run the posted code but it is incomplete. I was able to get it to run but it does not play anything recogizable. I suspect that there was a bit more left out of your code than I was able to quickly repair/replace.
Code: Select all
const int b= 4056; // 261 Hz
const int c= 3830; // 261 Hz
const int d= 3400; // 294 Hz
const int e= 3038; // 329 Hz
const int f= 2864; // 349 Hz
const int g= 2550; // 392 Hz
const int a= 2272; // 440 Hz
const int B= 2028; // 493 Hz
const int C= 1912; // 523 Hz
const int D= 1700; // 523 Hz
const int E= 1508; // 523 Hz
const int F= 1432; // 523 Hz
const int G= 1278; // 523 Hz
const int A= 1136; // 523 Hz
// Define a special note, 'R', to represent a rest
const int R= 0;
int speakerOut = 9;
int melody[] = { b, C, B, D, a, B, B, g, d, b, d, c, b,R } ;
int beats[] = { 8, 8, 48, 8, 8, 64, 8, 8, 8, 8, 16, 16,16,96 } ;
int MAX_COUNT = 14; // Melody length, for looping.
// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
int tone_=0;
int duration=0;
void setup() {
pinMode(speakerOut, OUTPUT);
drWhooTune();
}
void loop() {
}
void drWhooTune() {
int beat=0;
for(int js=0; js<20; js++) {
for (int i=0; i<MAX_COUNT; i++) {
tone_ = melody[i];
beat = 4*beats[i];
duration = beat * tempo; // Set up timing
playTone();
// A pause between notes...
delayMicroseconds(pause);
}
}
}
void playTone() {
long elapsed_time = 0;
if (tone_ > 0) { // if this isn't a Rest beat, while the tone has
// played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone_ / 2);
// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone_ / 2);
// Keep track of how long we pulsed
elapsed_time += (tone_);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}
This bit of code worked much better but still did not sound like anything from Doctor Who.
Code: Select all
/*
Melody
Plays a melody
circuit:
* 8-ohm speaker on digital pin 8
created 21 Jan 2010
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/Tone
*/
#include "pitches.h"
#define spk_pin 9
// notes in the melody:
int melody[] = {NOTE_B3, NOTE_C4, NOTE_B3, NOTE_D4, NOTE_A4, NOTE_B3, NOTE_B3, NOTE_G4, NOTE_D4, NOTE_B3, NOTE_D4, NOTE_C4, NOTE_B3, 0};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {8, 8, 48, 8, 8, 64, 8, 8, 8, 8, 16, 16, 16, 96};
void setup() {
pinMode(spk_pin, OUTPUT);
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(spk_pin, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(spk_pin);
}
}
void loop() {
// no need to repeat the melody.
}