help with photosensor and serial print

Hi all
I been having problems with a photosensor and println ( serial.print)
I have a photosensor on analog 0 and I want to print line once when the photosensor is cover, the code below keeps printing lines for ever.

any help guys.

int photoPin = 0; // photo resitor
int val = 0;

int ledPin = 13; // led for debugging

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT); // set the pin

}

void loop()                    
{

  val = analogRead(photoPin); // set val equal to the resitor input
Serial.println("dddddval");
  if(val == LOW) { // if nothing from photo resistor (low light), turn on led

 digitalWrite(ledPin, HIGH);
 delay(500);

  } else {
    digitalWrite(ledPin, LOW);
  }
  Serial.println(val);
}

When you've done something you don't want to repeat, remember that fact in a variable. When you want it to be allowed again, clear that condition in the variable.

int photoPin = 0; // photo resitor
int val = 0;
[glow]boolean reported = false; // have we reported the LOW?[/glow]

int ledPin = 13; // led for debugging

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT); // set the pin
}

void loop()
{
  val = analogRead(photoPin); // set val equal to the resitor input
  // if nothing from photo resistor (low light), turn on led
  if(val == LOW) {
    [glow]if (!reported) {
      Serial.print("val="); Serial.println(val); // report the LOW once
      reported = true; // remember so we don't do it again
    }[/glow]
    digitalWrite(ledPin, HIGH);
    delay(500);
  } else {
    digitalWrite(ledPin, LOW);
    [glow]reported = false; // allow new reports[/glow]
  }
  Serial.println(val);
}

than you so much
I will try it soon

Hi I just try you suggesstion and still getting tons of serial prints

Hi,
I found this openosurce script . How can I make the printlin once

int photoPin = 0; // photo resitor
int val = 0;





void setup(){ 
Serial.begin(9600); 
pinMode(13,OUTPUT); // LED on pin 13 to light up whenever the shadow conidtion is met. 

}




void loop() 
{ 
int lightlevel=analogRead(0); //reads the light level from the photoresistor and assigns it to the variable called lightlevel 
delay(1000); //wait one second. 
if (analogRead(0)<lightlevel-20) //if the light level changes by more than 20, it activates the shadow condition. 
{ 
digitalWrite(13,HIGH); //turn LED on 
Serial.println("A");//send signal to Mac, which interprets the A as a applescript command. 
} 
else{ 
digitalWrite(13,LOW); //turns LED off. 
} 
}

I see no functional difference between your first script and this script. I also see no attempt at adding and using the features I highlighted in my previous answer, which still applies.

thank you for your hones reply