I think Groove's solution looks like a good starting point - you've got all the bits.
if ((delay() > 10000) && (LED == HIGH))
"delay" is a void function; it doesn't return anything, and needs an argument to tell it how long to delay.
Looks like your "loop()" has a "}" too soon (just before the "if(delay" line.
You'd be better moving the declarations of "playNote" and "playTone" out, above "loop()"
I took Groove's outline and padded it out - it compiles, but I don't have any h/w to test it.
I'm not sure the tune playing logic is correct, but it's a start.
int val = 0;
const int speakerPin = 9;
const int LED = 13;
const int LDR = 0;
const char notes[] = "ccggaagffeeddc ";
const int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
const int length = sizeof (beats) / sizeof (beats [0]);
const int tempo = 50;
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) {
const char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
const int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void playTune (const char* notes, const int* beats, int length, int tempo)
{
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
}
delay(tempo / 2);
}
void setup()
{
pinMode(LED, OUTPUT);
pinMode(speakerPin, OUTPUT);
}
const int triggerLevel = 250; // if LDR falls below this, trigger.
const int DETECT_TIME = 10000;
const int PAUSE_TIME = 3000;
void loop()
{
long start = millis ();
while (analogRead(LDR) <= triggerLevel) {
if ((millis () - start) > DETECT_TIME) {
digitalWrite (LED, HIGH);
do {
playTune (notes, beats, length, tempo);
delay (PAUSE_TIME);
} while (analogRead (LDR) <= triggerLevel);
digitalWrite (LED, LOW);
}
}