Motion detector script help, need to ADD 20 seconds to original 20 seconds. Easy

I need to basically add 20 seconds to the Alarm (trigger switch) every time motion is detected. Right now it detects motion, alarm goes off for 20 seconds, and then it waits for motion again but I need it to basically add 20 seconds even when the alarm is already triggers.

For example, motion detector detects motion, alarm goes off for 9 seconds and then motion detector detects motion again, alarm continues for another 20 seconds

instead of

motion detector detects motion, alarm goes off for 20 seconds, motion is detected again after 9 seconds but just counts another 11 seconds and then stops and waits for motion again.

I am sure this is something really simple to add to the code below, still new to Ardy :( Hope someone can help

  if (buttonStateMotion == HIGH) {     
    

      digitalWrite(4, HIGH);   // trigger switch
  delay(1000);              // wait for a second
    digitalWrite(4, LOW);    // release switch
  delay(20000);              // wait for 20 seconds 
  
   //need other if statement maybe? to add 20 seconds if motion is found at anytime?

  
        digitalWrite(4, HIGH);   // trigger switch
  delay(1000);              // wait for a second
    digitalWrite(4, LOW);    // release switch
  delay(5000);              // wait 5 seconds 
  
   } 
  else {
}

Your sketch is incomplete, so I can't see what sets/resets "buttonStateMotion".

I'm guessing that all those "delay"s aren't helping matters.

Have a look at the blink without delay example for some ideas.

here is the complete code:

i will look into the blink without delay but my programming skills are very limited right now

    const int buttonPinMotion = 2;     // the number of the pushbuttonMotion pin

// variables will change:
int buttonStateMotion = 0;         // variable for reading the pushbuttonMotion status



void setup() {                
  // initialize the digital pin as an output.
  pinMode(5, OUTPUT);     
    pinMode(4, OUTPUT); 

    
  // initialize the pushbutton pin as an input:
  
  
  pinMode(buttonPinMotion, INPUT);       





  
   
}



void loop() {
  
  //    digitalWrite(4, HIGH); 
 // delay(1000);            
  //  digitalWrite(4, LOW);   
 // delay(8000);              
  
  
   // read the state of the pushbutton value:
  buttonStateMotion = digitalRead(buttonPinMotion);
   buttonStateUSB = digitalRead(buttonPinUSB);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  if (buttonStateMotion == HIGH) {     
    

      digitalWrite(4, HIGH);   // trigger switch
  delay(1000);              // wait for a second
    digitalWrite(4, LOW);    // release switch
  delay(20000);              // wait for 20 seconds 
  
   //need other if statement maybe? to add 20 seconds if motion is found at anytime?

  
        digitalWrite(4, HIGH);   // trigger switch
  delay(1000);              // wait for a second
    digitalWrite(4, LOW);    // release switch
  delay(5000);              // wait 5 seconds 
  
   } 
  else {
}

Warning : only just got my board so haven't even powered it on yet - still a newbee.

However, based on your post, I think you would be looking along these lines :

// constants that will not change: const int MotionSensorPin = 4; // the pin number for the motion sensor (optoisolated ?) const int SirenPin = 5; // the pin number for the relay that powers the siren (transistor ?) const long SirenTime = 20000; // milliseconds for the siren to sound

// variables will change: int SirenON = 0; long cMillisSiren = 0; long pMillisSiren = 0;

void setup() {

// initialize the digital pins pinMode(MotionSensorPin, INPUT); // add a pull-down resistor ? pinMode(SirenPin, OUTPUT); digitalWrite(SirenPin, LOW); // it should also have a pull-down resistor ?

}

void loop() {

if(digitalRead(MotionSensorPin) == HIGH){ digitalWrite(SirenPin, HIGH); // turn the siren ON SirenON = 1; pMillisSiren = millis(); // this is the NEW starting point (time) for the siren countdown every time motion is detected }

if(SirenON == 1) { cMillisSiren = millis(); if(cMillisSiren - pMillisSiren > SirenTime) { SirenON = 0; digitalWrite(SirenPin, LOW); } } }

Thanks Dave, your code seems to work just fine. Problem is that as of today my motion detector has been going crazy, it is extremely sensitive, it wasn't like this yesterday, has nothing to do with the code. I tried putting a 10k resister between it's output and the arduino pin but it doesn't seem to help, should i be using another size resister?

Have you opened it and cleaned it out. You'll be surprised at what a bunch of ants or other insects can do in a sensor.