LDR input trigger issue.

Hi, I have a relatively simple Light Dependant Resistor circuit and code for the Uno.
The arrangement simply plays a tune when the light level has decreased to a set threshold level,
this is the code:

#include "pitches.h"

  int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 };

  int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4 };

  int sensorPin = A1;   // select the input pin for ldr
  int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() 
{
  Serial.begin(9600); //sets serial port for communication
}
void loop()
{
  sensorValue = analogRead(sensorPin); // read the value from the sensor   
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  
  if(sensorValue < 600) //setting a threshold value  
   
  for (int thisNote = 0; thisNote < 8; thisNote++)
{   
  int noteDuration = 1000 / noteDurations[thisNote];
  tone(7, melody[thisNote], noteDuration);
  int pauseBetweenNotes = noteDuration * 1.10;
  delay(pauseBetweenNotes);
} 
}

However, with this code, the tune keeps playing over and over (as one would expect) if the light level has stayed at or below the threshold.

But I only want the tune to play once.

Only if the light level has briefly gone back above threshold and then below again, should the tune be re-triggered to play.

I’ve tried a few crazy code ideas but I don’t really know what I am doing in this area.

Hopefully someone can provide a tip or some code ideas to help.

Thanks.

A simple boolean flag to keepup with whether or not you've played the song once since the last time the sensor was lit up.

Pseudocode:

boolean alreadyPlayedIt = false;

void loop(){
   if(light sensor below threshold && !alreadyPlayedIt){
          alreadyPlayedIt = true;
         // play the song
   }
   if(light sensor above threshold) {
          alreadyPlayedIt = false;
   }

@Delta_G

Typo in the last line.

Wawa: @Delta_G

Typo in the last line.

Corrected. Thanks!

Many thanks for your help Delta_G, your code certainly sounds logical, but I have no idea as to where to place it within my code.

I have tried many things but no joy yet (spent an hour or so trying).

Do you mind helping me with where I should be including it ?

Thanks again.

Really? Do you know which part of your code right now plays the song? That part goes where the pseudocode says, "play the song".

If you don't know what part of your program plays the song, then it's time to go do some basic examples and tutorials until you get the basics of coding down.

Yes, much more learning is what I need for sure.

This code works as wished but only plays the first note of the eight:

#include "pitches.h"

  int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 };

  int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4 };

  int sensorPin = A1;   // select the input pin for ldr
  int sensorValue = 0;  // variable to store the value coming from the sensor
  boolean alreadyPlayedIt = false;

void setup() 
{
  Serial.begin(9600); //sets serial port for communication
}

void loop()
{
  sensorValue = analogRead(sensorPin); // read the value from the sensor   
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  
  if(sensorValue < 600) //setting a threshold value  
    
  for (int thisNote = 0; thisNote < 8; thisNote++)
  
  if(sensorValue < 600 && !alreadyPlayedIt){
          alreadyPlayedIt = true;
  
  int noteDuration = 1000 / noteDurations[thisNote];
  tone(7, melody[thisNote], noteDuration);
  int pauseBetweenNotes = noteDuration * 1.10;
  delay(pauseBetweenNotes);
}

  if(sensorValue > 600) {
          alreadyPlayedIt = false;
  
}
}

I’ll keep trying.

Thanks

Finally got it working properly:

#include "pitches.h"
  
  int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 };
  int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4 };
  int sensorPin = A1;   // select the input pin for ldr
  int sensorValue = 0;  // variable to store the value coming from the sensor
  boolean alreadyPlayedIt = false;

void setup() 
{
  Serial.begin(9600); //sets serial port for communication
}
void loop()
{
  sensorValue = analogRead(sensorPin); // read the value from the sensor   
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen

  if(sensorValue < 600 && !alreadyPlayedIt){
        alreadyPlayedIt = true;

  for (int thisNote = 0; thisNote < 8; thisNote++)
{ 
  int noteDuration = 1000 / noteDurations[thisNote];
  tone(7, melody[thisNote], noteDuration);
  int pauseBetweenNotes = noteDuration * 1.10;
  delay(pauseBetweenNotes);
}
}
  if(sensorValue > 600) {
          alreadyPlayedIt = false;
}
}