Hello,
***See updated code at bottom of post.
Put together some code that activates relays when a motion is detected using a PIR sensor.
First tried the code with one relay (to turn ON/Off my computer) and works well.
// Turning on electrical systems on my robot via motion PIR sensor and Seeed relay module.
// Give a greeting, hello, etc.. (speech), which I have setup already on my mini ITX PC M/B.
// Then, if there is no motion detected after a certain time (15 minutes),
// shut down PC M/B followed by the robots electrical system.
// The arduino in this setup will be powered by a 9V battery.
// Some of the code used (PIR), I believe was from Kristian Gohlke
// and the Seeed relay demo code.
const int relayComPin = 6; //Digital pin to control the relay for the computer
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
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 = 10000; // Duration of no motion time to shut off system. 15min. pause or wait (900000mS)
boolean lockLow = true;
boolean takeLowTime;
const int pirPin = 9; //the digital pin connected to the PIR sensor's output
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(relayComPin, OUTPUT);
digitalWrite(pirPin, LOW);
}
void loop(){
if(digitalRead(pirPin) == HIGH){
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()/1000);
Serial.println(" sec");
delay(50);
}
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
}
takeLowTime = true;
digitalWrite(relayComPin,HIGH); //Turns on computer
}
if(digitalRead(pirPin) == 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
digitalWrite(relayComPin,HIGH); //Turns off computer
}
//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;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
digitalWrite(relayComPin,LOW); //opens relay for another cycle
}
}
}
Here is where I added the second relay to control the electrical system on my robot. Compiles fine but when I run it, I get a quick double click from the relay(s) and they remain ON instead of turning back OFF or NO(normally open).
// Turning on electrical systems on my robot via motion PIR sensor and Seeed relay module.
// Give a greeting, hello, etc.. (speech), which I have setup already on my mini ITX PC M/B.
// Then, if there is no motion detected after a certain time (15 minutes),
// shut down PC M/B followed by the robots electrical system.
// The arduino in this setup will be powered by a 9V battery.
// Some of the code used (PIR), I believe was from Kristian Gohlke
// and the Seeed relay demo code.
const int relayElePin = 7; //Digital pin to control the relay for the electrical system
const int relayComPin = 6; //Digital pin to control the relay for the computer
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
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 = 10000; // Duration of no motion time to shut off system. 15min. pause or wait (900000mS)
boolean lockLow = true;
boolean takeLowTime;
const int pirPin = 9; //the digital pin connected to the PIR sensor's output
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(relayComPin, OUTPUT);
pinMode(relayElePin, OUTPUT);
digitalWrite(pirPin, LOW);
}
void loop(){
if(digitalRead(pirPin) == HIGH){
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()/1000);
Serial.println(" sec");
delay(50);
}
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
}
takeLowTime = true;
digitalWrite(relayComPin,HIGH); //Turns on electrical system
delay(5000); //Adds a bit of time...
digitalWrite(relayComPin,HIGH); //Turns on computer
}
if(digitalRead(pirPin) == 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
digitalWrite(relayComPin,HIGH); //Turns off computer
delay(10000); //Adds a bit of time to complete shutdown of computer
digitalWrite(relayElePin,HIGH); //Turns off system
}
//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;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
digitalWrite(relayElePin,LOW); //opens relay #1 for another cycle
digitalWrite(relayComPin,LOW); //opens relay #2 for another cycle
}
}
}
Any help, suggestions appreciated.
t