need help with photoresister

Hello, I"m a compete newbie, and I need some help with my code.

I have to write a program for class that uses a photoreister to sense light, then pause for a set amount of time, play a melody with a piezo, then when the light stops, it stops playing.

My code senses the light, but does not stop playing when the light goes away. I think I"m stuck in some sort of loop,
I’ve tried do statement, do/while statements, ect but this is my latest code,can someone help?

#define LED 13    //  the pin for the LED

#define photoresister 7         // the input pin for the LDR

int speakerPin = 9;   // the pin for the piezo

int val = 0;  // val will be used to store the state of the input pin

int length = 15;   // the number of notes

char notes[] = "ccggaagffeeddc "; // a space represents a rest

int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };

int tempo = 300;


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', 'g', 'a', 'b', 'C' };
        int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

        // play the tone corresponding to the note name
        for (int i = 0; i < 8; i++) 
         {
          if (names[i] == note) 
               {
                  playTone(tones[i], duration);
                }
        }
}
void playTune (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); // tell program LED is an output
        pinMode (photoresister, INPUT);  // tell program the light sensor is an input
        pinMode (speakerPin, OUTPUT); //tell program the piezo is an output
}

void loop()
{
val = digitalRead (photoresister);     // reads input value and stores it

if(val == HIGH)      //check to see if button has light on it
{
      
        digitalWrite(LED, HIGH);      //turn on LDR
      delay(180000);            //wait 3 minutes
     
        playTune (notes, beats, length, tempo);
        digitalRead (photoresister);
val=digitalRead(photoresister);
      
      
  }


else                              // if the light is not on the LDR
{
      digitalWrite (LED, LOW);      //turn off the Led
}

}

thanks a bunch!

Well, once you call playTune() you don't return until the tune is done. If you want to stop playing in the middle of the tune, you have to keep checking the photoresistor while playing notes.

Sounds familiar:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1246308375

yeah, the guy who wrote that other post is in my class - he saw me looking at it lol

He's right, our teacher is an idiot and no help at all.

I looked thru that program, but dont' want to copy it exactly since the teacher might guess something's up if two people have the same code.

Well, once you call playTune() you don't return until the tune is done. If you want to stop playing in the middle of the tune, you have to keep checking the photoresistor while playing notes

I'm fine with it playing the whole tune (which is rather short) then stopping, but even if I put it in compete darkeness, it just keeps going.

it's as if somehow it's not recongizing that the photoresister is now LOW instead of HIGH. I"ve wrote it a dozen differnt ways, but it still keeps going. I even added a part where after the tune plays, it checks the photoresister, then blinks the LED. That works, so it's going thru the whole loop, but it's not returning the value as LOW to get out of the loop.

our teacher is an idiot and no help at all.

I wonder what his forum nick is ::)

RUGGED CIRCUITS nailed it. The hint here is that you need to keep checking...

When you are IN THE LOOP to play music... you will only play music unless you check the light level "while" playing and set or clear a STATE variable.

If you do something like telling the ARDUINO to pause with a delay command... that's ALL it's doing... not checking pin changes..... just delaying.

If you use a polling method that relies on setting or clearing a boolean variable while in a loop, you have a better chance of catching changes closer to when they occur.

You can use millis() and a DELTA TIME between values received to create a delay that you can "interrupt", for example.

So, if I change this:

if(val == HIGH)      //check to see if button has light on it
{

        digitalWrite(LED, HIGH);      //turn on LDR
      delay(180000);            //wait 3 minutes
    
        playTune (notes, beats, length, tempo);
        digitalRead (photoresister);
val=digitalRead(photoresister);

to be

if(val == HIGH)      //check to see if button has light on it
{

        digitalWrite(LED, HIGH);      //turn on LDR
      delay(180000);            //wait 3 minutes
    
        playTune (notes, beats, length, tempo);
                  [color=#ff0000]val = LOW;[/color]        
digitalRead (photoresister);
val=digitalRead(photoresister);

would that work? I’m setting the value to low to exit the loop, then I"m checking to see if it’s low to get back into the loop. . . or I could even move the

digitalRead (photoresister);
val=digitalRead(photoresister)

outside the loop again.

Don't know why you're reading the LDR inside the "if" clause - it won't do anything, because you're not examining the value.

could you explain that a little further? I'm not sure if I know what that means, "examining the value", but that seems to be the crux of my problem. It sees light, it starts playing, then it never reads the photoresister again, but I dont' know how to get it to do so. I'm at the limit of my programming skills, and all the googleing in the world isn't helping me find what I need to know.

that's why that last bit of setting the val to low, then getting out of the loop hopes to solve. I"m at work right now, so I can't check it, but I will tonight.

I think the problem might be that you're using digitalRead() to read an analog value. You haven't explained how you hooked the LDR up to the circuit. You might be better off with analogRead(). My guess is that the circuit is just barely high to begin with, but then when it goes low it stays just barely low.

Thanks! I was thinking about changing it to an analogread pin, but didnt' know if that would make a difference. I will try that tonight for sure.

I'd write a simple sketch without all the delay and tune playing, just to print values from an analogueRead, so you'v got some idea of what sort of values you can expect for "light" and "dark"

ok, I wrote a simple code to read analog inputs, it was always in the 705 range, with light,or with my finger over the sensor.

So is my sensor bad? I'm goign to head to radio shack to see if they have another photoresister, hopefully they do.

I'm so confused, I think my code should work, but it don't for some reason, but it don't I know know if they board is wired wrong, or even if allthe componts are working. I'm getting no help from my teacher, this is frustrating!

I started from scratch, rewired the entire board,rewrote the program, and IT WORKS!!!

Thanks for all the help.