My original code ripped from the arduino site example:
int speakerPin = 10;
int length = 69; // the number of notes
char notes[] = "ddaagfedcdefga ddaagfedcdefga avgavCDagfdefgfgavaagfedfedgfgavCDagfed";
char names[] = { 'c', 'd', 'e', 'f', 's', 'g', 'a', 'v', 'b', 'C', 'D', 'E' };
int tones[] = { 1915, 1700, 1519, 1432, 1352, 1275, 1136, 1073, 1014, 956, 852, 758 };
int beats[] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,4,2,2,2,2,2,2,4,1,1,2,4,2,2,2,2,2,2,2,2,2,2,8 };
int tempo = 150;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 's', 'g', 'a', 'v', 'b', 'C', 'D', 'E' };
int tones[] = { 1915, 1700, 1519, 1432, 1352, 1275, 1136, 1073, 1014, 956, 852, 758 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
Also a secondary one which I cant get working because it requires a library for the notes (which I have downloaded) and it told me to put the file in the same folder as my sketch. I did and it says it cannot find it. Any help?
#include "pitches.h"
// Piezzo element connected to Arduino pin 12 and ground
const int buzzerPin = 10;
// Array with the notes in the melody (see pitches.h for reference)
int melody[] = {NOTE_A4, NOTE_A4, NOTE_A4, NOTE_F4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_C5, NOTE_A4, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_F5, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_C5, NOTE_A4};
// Array with the note durations: a quarter note has a duration of 4, half note 2 etc.
int durations[] = {4, 4, 4, 5, 16, 4, 5, 16, 2, 4, 4, 4, 5, 16, 4, 5, 16, 2};
int tempo = 120; // tempo for the melody expressed in beats per minute (BPM)
void setup() {
playTune(melody, durations, tempo);
}
void loop() {
// no need to repeat the melody.
}
void playTune(int notes[], int durations[], int BPM)
{
int tuneSize = sizeof(melody) / sizeof(int);
// iterate over the notes of the tune:
for (int thisNote = 0; thisNote < tuneSize; thisNote++) {
// For details on calculating the note duration using the tempo and the note type,
// see http://bradthemad.org/guitar/tempo_explanation.php.
// A quarter note at 60 BPM lasts exactly one second and at 120 BPM - half a second.
int noteDuration = (int)((1000 * (60 * 4 / BPM)) / durations[thisNote] + 0.);
tone(buzzerPin, notes[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 20% seems to work well:
int pauseBetweenNotes = noteDuration * 1.20;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(buzzerPin);
}
}