0

I want to make the PIRs to stay on for about 1 minute. I've found the code that can do so for 1 PIR, but I dont know how to apply the code for 3 PIRs.

Here is the code for 1 PIR with timer using millis:

int ledPin = 8;
int pirPin = 3;

unsigned long piron;
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;       

int off;
int on;
const int LDR = A0;
int ldrStatus;

unsigned long currentMillis =( millis()/1000 ) / 60;

/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);


  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop()

{  
ldrStatus = analogRead (LDR);
if (ldrStatus <= 700)     // variable reading(ldrStatus) according to ldr 
                          // the higher reading(ldrStatus), the dimmer light
                          // the lower the reading(ldrStatus), the brighter light

{
analogRead (pirPin) == off;

Serial.println ("Its SUNNY DAY !!!");
digitalWrite(ledPin, LOW);

}


if (ldrStatus >= 700)
{
Serial.println ("Its DARK already");

    if(digitalRead(pirPin) == HIGH){
       digitalWrite (ledPin, HIGH);
     piron = ( millis()/1000 ) / 60;
  }



  if ((currentMillis - piron) > 1) {
    digitalWrite(ledPin, LOW);

  }

    }
}
`


and here is the code for 3 PIRs without the timer

    int calibrationTime = 10;       


int pirPin = 3;    
int ledPin = 8;

int pirPin1 = 4;   
int ledPin1 = 9;

int pirPin2 = 5;    
int ledPin2 = 10;

int ledPin3 = 11;

int off;
int on;
const int LDR = A0;
int ldrStatus;


/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);

   pinMode(pirPin1, INPUT);
  pinMode(ledPin1, OUTPUT);
  digitalWrite(pirPin1, LOW);

   pinMode(pirPin2, INPUT);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(pirPin2, LOW);

 pinMode (ledPin3, OUTPUT);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop()
{
ldrStatus = analogRead (LDR);
if (ldrStatus <= 700)     // variable reading(ldrStatus) according to ldr 
                          // the higher reading(ldrStatus), the dimmer light
                          // the lower the reading(ldrStatus), the brighter light

{
analogRead (pirPin) == off;
analogRead (pirPin1) == off;
analogRead (pirPin2) == off;
Serial.println ("Its SUNNY DAY !!!");
digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}


if (ldrStatus >= 700)
{
Serial.println ("Its DARK already");
digitalWrite(ledPin3, HIGH);    

    if(digitalRead(pirPin) == HIGH && digitalRead(pirPin1)== LOW && digitalRead(pirPin2) == LOW){
      digitalWrite (ledPin, HIGH);
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2,LOW);
    }



    if(digitalRead(pirPin) == LOW && digitalRead(pirPin1)== HIGH && digitalRead(pirPin2) == LOW){
      digitalWrite (ledPin1, HIGH);
      digitalWrite (ledPin, LOW);
      digitalWrite(ledPin2,LOW);

}
   if(digitalRead(pirPin) == LOW && digitalRead(pirPin1)== LOW && digitalRead(pirPin2) == HIGH){
      digitalWrite (ledPin2, HIGH);
      digitalWrite (ledPin1, LOW);
      digitalWrite (ledPin, LOW);

   }



     if(digitalRead(pirPin) == HIGH && digitalRead(pirPin1)== HIGH && digitalRead(pirPin2) == LOW){
    digitalWrite (ledPin1, HIGH);
    digitalWrite (ledPin, HIGH);
    digitalWrite (ledPin2, LOW);

}

     if(digitalRead(pirPin) == HIGH && digitalRead(pirPin1)== LOW && digitalRead(pirPin2) == HIGH){
      digitalWrite (ledPin, HIGH);
      digitalWrite (ledPin2, HIGH);
      digitalWrite (ledPin1, LOW);
     }


     if(digitalRead(pirPin) == LOW && digitalRead(pirPin1)== HIGH && digitalRead(pirPin2) == HIGH){
     digitalWrite (ledPin2, HIGH);
     digitalWrite (ledPin1, HIGH);
     digitalWrite (ledPin, LOW);
     }



    if(digitalRead(pirPin) == HIGH && digitalRead(pirPin1)== HIGH && digitalRead(pirPin2) == HIGH){
      digitalWrite (ledPin2, HIGH);
      digitalWrite (ledPin1, HIGH);
      digitalWrite (ledPin, HIGH);

    }


     if(digitalRead(pirPin) == LOW && digitalRead(pirPin1)== LOW && digitalRead(pirPin2) == LOW){
      digitalWrite (ledPin2, LOW);
      digitalWrite (ledPin1, LOW);
      digitalWrite (ledPin, LOW);
     }

}

}
6
  • @jsotola ive try rewriting the first code from if(digitalRead(pirPin) == HIGH){ digitalWrite (ledPin, HIGH); piron = ( millis()/1000 ) / 60; } if ((currentMillis - piron) > 1) { digitalWrite(ledPin, LOW); } to digitalWrite (ledPin, digitalRead(pirPin)); piron = ( millis()/1000 ) / 60; if ((currentMillis - piron) > 1) { digitalWrite(ledPin, LOW); } but it seems to ignore the timer or am i writing the code wrong
    – ann
    Mar 20 '20 at 15:10
  • and thanks! now i know i dont need to use if :)
    – ann
    Mar 20 '20 at 15:56
  • okay, ive tried changing to 50seconds in this code ` digitalWrite(ledPin, digitalRead(pirPin)); piron = millis(); currentMillis = millis(); if ((currentMillis - piron) > 50000) { digitalWrite(ledPin, !digitalRead(pirPin)); startMillis = currentMillis;` but the timer still doesnt work. The led turns off in like 3 seconds....
    – ann
    Mar 21 '20 at 1:29
  • can you give me an example? sorry for asking too many question...im still new to this.
    – ann
    Mar 21 '20 at 2:59
  • it means pir on = led on, i want to make the led on by using the pir, and after 1 minute the led is automatically off
    – ann
    Mar 21 '20 at 3:36
1

This is not a complete answer, but an idea of how to accomplish the task.

I just typed it in without testing the code.

Use arrays to hold variables. That way a loop can be used to service the three sensors and the three LEDs..

for (i = 0; i<3; i++) {                                              
   currentTime = millis();                             // save current millis() value

   if (digitalRead(pirPin[i]) == HIGH) {               // check if PIR is detecting motion
      trigTime[i] = millis();                          // save trigger time
      trig[i]     = true;                              // set a state variable that indicates
   }                                                   //   that a trigger happened


   if (trig[i]) {                                      // if trigger happened
      if (currentTime - trigTime[i] < 50000) {         // is it less than 50 sec since last motion?
         digitalWrite (ledPin[i], HIGH);
      }
      else { 
         trig[i] = false;                              // 50 second timer ran out
         digitalWrite (ledPin[i], LOW);
      }
   }
}
1
  • thanks a lot!! though i did some changes in that code and put the pirs and leds in different loops. i finally got it. thanks again, i really appreciate it. :D
    – ann
    Mar 22 '20 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.