Go Down

Topic: PIR - Relay problem (Read 220 times) previous topic - next topic

ifugaopapercraft

This sketch will turn the LED on and off with motion detected, but when the relay is included, the sketch is repetitive; flashing the LED and tripping the relay regardless of PIR input. Any idea what I am doing wrong?
Code: [Select]
int calibrationTime = 30;       

[code]int calibrationTime = 30;       

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 1500; //5000

boolean lockLow = true;
boolean takeLowTime; 

int pirPin = 8;    //the digital pin connected to the PIR sensor's output
int ledPin = 13;
int relayPin = 2;


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

}

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

  if(digitalRead(pirPin) == HIGH){
    digitalWrite(ledPin,LOW);
    digitalWrite(relayPin,LOW);
    //the led visualizes the sensors output pin state
    if(lockLow){ 
      //makes sure we wait for a transition to LOW before any further output is made:
      lockLow = false;           
      Serial.println("---");
      Serial.print("motion detected at ");
      Serial.print(millis()/50);//1000
      Serial.println(" sec");
      delay(50);
    }         
    takeLowTime = true;
  }

  if(digitalRead(pirPin) == LOW){       
    digitalWrite(ledPin, HIGH);
    digitalWrite(relayPin,HIGH);
    delay(5000);
    digitalWrite(ledPin, LOW);
    digitalWrite(relayPin,LOW);
    if(takeLowTime){
      lowIn = millis();          //save the time of the transition from high to LOW
      takeLowTime = false;       //make sure this is only done at the start of a LOW phase
    }
    //if the sensor is low for more than the given pause,
    //we assume that no more motion is going to happen
    if(!lockLow && millis() - lowIn > pause){ 
      //makes sure this block of code is only executed again after
      //a new motion sequence has been detected
      lockLow = true;                       

      delay(50);
    }
  }
}
[/code]

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy