Single printing of serial.println when receiving data instead of multiple

We are using 4 LDRs to detect the absence of light and we want to print it in serial monitor as "detected". The problem is the continuous printing of "detected" when there is no light present. We want "detected" to be printed once and not multiple. We try the concept of debounce button but after a few tries it is printing continuously. We can't understand the problem.

int val;
int detected;
int pin;
int limit=600;
int con=0;   //condition
int cnt;       // count
int toggle1;
int counter;
int lastDebounceTime1;

void setup(){
  Serial.begin(9600);
  for (pin=A0; pin<A4; pin++){
    pinMode(pin,INPUT);
  }
}

void loop(){
  for (pin=A0; pin<A4; pin++){
    val = analogRead(pin);
    detected=map(val,0,1023,0,1023);
    cnt =  (detected<limit) ? 1 : 0;
    con+=cnt;
   }
  
 if (con>0 && con<5){
    lastDebounceTime1 = millis();
    toggle1 = HIGH;
  }
 
  if(toggle1 == HIGH){
    if ((millis() - lastDebounceTime1) > 50){
      counter++;
      Serial.println(counter);
      toggle1 = LOW;
    }
  }
 
  
  if(counter == 1){
    digitalWrite(9,HIGH);
    Serial.println("detected");
    counter = 0;
  }
  else if (con==0){
    digitalWrite(9,LOW);
  }
 con=0;
}

Remember two things, to post your code in this forum using the code post feature, and that codes loop, so as long as counter equals 1, which it always does when it doesn't detect light, it will keep doing everything in those brackets over and over again. including print "detected" in the serial.

i fixed it for you with a variable, just remember to put

counter = LOW;

in the bracket that activates only when light IS detected. I just plopped it in your

else if (con == 0)

so you may have to move it

good luck! :smiley:

New Code

int val;
int detected;
int pin;
int limit = 600;
int con = 0; //condition
int cnt;       // count
int toggle1;
int counter = 1;
int lastDebounceTime1;
boolean counterOnce = LOW;

void setup() {
  Serial.begin(9600);
  for (pin = A0; pin < A4; pin++) {
    pinMode(pin, INPUT);
  }
}

void loop() {
  for (pin = A0; pin < A4; pin++) {
    val = analogRead(pin);
    detected = map(val, 0, 1023, 0, 1023);
    cnt =  (detected < limit) ? 1 : 0;
    con += cnt;
  }

  if (con > 0 && con < 5) {
    lastDebounceTime1 = millis();
    toggle1 = HIGH;
    
  }

  if ((toggle1 == HIGH) && ((millis() - lastDebounceTime1) > 50))
  {
    counter++;
    Serial.println(counter);
    toggle1 = LOW;
  }


  if (counter == 1) {
  digitalWrite(9, HIGH);
    if (counterOnce == LOW)
    {
      Serial.println("detected");
      counterOnce = HIGH;
    }
    counter = 0;
  }
  else if (con == 0) {
  digitalWrite(9, LOW);
  counterOnce = LOW;
  }
  con = 0;
}

Please edit your post and use code tags for the code.
Type
** **[code]** **
before the code
Type
** **[/code]** **
after the code

You need a flag to remember that you the condition was true. Set the flag the first time that the condition was true and reset it when the condition is no longer true. Make that flag part of the condition when you print.

I'm not quite sure how your code is intended to work so can't completely advise; maybe add a previousToggle variable and when that differs from toggle1 print "detected".

Wounding what you think this does?

detected = map(val, 0, 1023, 0, 1023);

to me you are mapping a value from 0 min 1023 max to 0 min 1023 max. detected = val.

Sorry, I forgot to mention that the LDRs aren't supposed to be detecting light. We are using the LDR like a button. If we cover the LDR, "detected" will come up on the serial monitor. We wanted to print "detected" only once, it works after a few tries but after some time "detected" will be printed continuously unless we reset the arduino. I forgot to remove the map. Sorry about that. Thanks for your help.