URGENT how to use delay

I'm using LDR sensor and what I want to change in my code is that I want my LDR to read for 3s before giving the OUTPUT. So when the LDR reading is less than 600 for 3s then the LED and buzzer trigger. LED starts to "BLINK BLINK" and buzzer "BEEP BEEP"

int led = 12;
int buzzer = 11;
int ldr = A2;

void setup()
{
  Serial.begin(9600);
  
  pinMode(led, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(ldr, INPUT);
}

void loop()
{
  int ldrstate = analogRead(ldr);
  Serial.print("LDR value is :");
  Serial.println(ldrstate);

  
  if (ldrstate < 600)
  {    
    tone(buzzer, 800);
    digitalWrite(led, HIGH);
    delay(100);
    
    noTone(buzzer);
    digitalWrite(led, LOW);
    delay(100);
   
    Serial.println("Alarm is ON");
   
  }
  else
  {
    noTone(buzzer);
    digitalWrite(led, LOW);
    
    Serial.println("Alarm is OFF");
  }
}

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

You can also check out my tutorial How to code Timers and Delays in Arduino
and this post if statement with time delay - #7 by drmpf - Programming Questions - Arduino Forum
and this one Executes a statement only if the condition is True for 5 seconds - #13 by drmpf - Programming Questions - Arduino Forum

For the flash and buzzer see the Led Sequencing example in
How to code Timers and Delays in Arduino

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.