I have some code (attached)...i use an arduino uno
I am attempting to send ledPin4 HIGH after the pir has detected motion for 5 consecutive seconds.
/*
This example shows the output of an analogRead() of a Photocell.
By M.Gonzalez
www.codingcolor.com
The example code is in the public domain
*/
int photocellPin = 0;// Photocell connected to analog pin 0
int photocellVal = A0; // define photocell variable
int pirVal = LOW;// motion sensor variable
int ledState = 0;//state of the led
int minLight = 20;//min light threshold
int maxLight = 20;//max light threshold
//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
unsigned long lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
unsigned long pause = 5000UL;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 2; //the digital pin connected to the PIR sensor's output
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;
int ledPin4 = 6;
void setup() {
Serial.begin(9600);
pinMode(photocellPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMmode(ledPIn4, 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);
}
void loop() {
photocellVal = analogRead(photocellPin);
pirVal = digitalRead(pirPin);
if (photocellVal < minLight && ledState == 0){
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
if (pirVal == HIGH) {
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, 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);
}
takeLowTime = true;
}
//Serial.println("fade up");
}
if(pirVal == LOW)
{
digitalWrite(ledPin1, LOW); //the led visualizes the sensors output pin state
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, 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;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
No luck as of yet. I did try adding a delay(200); but that delayed everything...yikes
The Good.
What works so far is:
ldr senses light and only when at or below minlight does ledPin1 fade on and off continuous until daylight.
pir senses motion and only if ldr is HIGH, it turns ledPins2,3,4,5 HIGH
when pir does not sense motion ledPins2,3,4,5 & go LOW & ledPin1 continues High (fade) unless its light out
The Bad.
Having trouble with:
at the point when the pir senses motion, i want only ledPins2&3 go HIGH
and IF the pir continues to sense motion after 5 seconds, turn ledPins4&5 HIGH
I attached the code that has a delay in it, but it doesn't work.
/*
This example shows the output of an analogRead() of a Photocell.
By M.Gonzalez
www.codingcolor.com
The example code is in the public domain
*/
int photocellPin = 0;// Photocell connected to analog pin 0
int photocellVal = A0; // define photocell variable
int pirVal = LOW;// motion sensor variable
int ledState = 0;//state of the led
int minLight = 20;//min light threshold
int maxLight = 20;//max light threshold
//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
unsigned long lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
unsigned long pause = 5000UL;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 2; //the digital pin connected to the PIR sensor's output
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;
int ledPin4 = 6;
int ledPin5 = 7;
void setup() {
Serial.begin(9600);
pinMode(photocellPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, 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);
}
void loop() {
photocellVal = analogRead(photocellPin);
pirVal = digitalRead(pirPin);
if (photocellVal < minLight && ledState == 0){
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
if (pirVal == HIGH) {
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
delay(200);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, 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);
}
takeLowTime = true;
}
//Serial.println("fade up");
}
if(pirVal == LOW)
{
digitalWrite(ledPin1, LOW); //the led visualizes the sensors output pin state
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, 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;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
I don't know which is the active state of a PIR sensor .
My assumption, for this sketch, was that it would go Low.
You can change that easily.
Anyway, once the 5 consecutive second threshold is crossed, LED 13 goes On.
And it stays on till the Reset button is pushed or you include a separate reset option
// trippoint5sec
//
// wait for 5 seconds
// of constant detection
//
unsigned long refTime;
unsigned long trippoint;
const long threshold = 5000;
byte DET;
const byte pirPin = 8;
const byte ledPin = 13;
void setup ()
{
//pirPin is INPUT by default, use ExtPullup
pinMode (ledPin,OUTPUT);
digitalWrite (ledPin,LOW);
}
void loop ()
{
DET = digitalRead(pirPin);
if (DET == 1) // inactive
{
refTime = millis();
trippoint = refTime;
}
else // Active Low
{
pir_active();
}
}
void pir_active ()
{
trippoint = millis();
if ((trippoint-refTime) > threshold)
{
digitalWrite(ledPin,HIGH);
}
}
I added the "wait for 5 seconds" code...thank you Runaway Pancake.
Haven't been able to test it yet because I cannot verify the code with errors.
I don't know how to fix it...I'm still at it, won't give up...and yes, I'm very new at this.
Automobile_Deterrent:131: error: a function-definition is not allowed here before '{' token
Automobile_Deterrent:138: error: expected }' at end of input Automobile_Deterrent:138: error: expected }' at end of input
/*
This example shows the output of an analogRead() of a Photocell.
By M.Gonzalez
www.codingcolor.com
The example code is in the public domain
*/
int photocellPin = 0;// Photocell connected to analog pin 0
int photocellVal = A0; // define photocell variable
int pirVal = LOW;// motion sensor variable
int ledState = 0;//state of the led
int minLight = 20;//min light threshold
int maxLight = 20;//max light threshold
//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
unsigned long lowIn;
unsigned long refTime;
unsigned long trippoint;
const long threshold = 5000;
byte DET;
const byte pirPin = 2;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
unsigned long pause = 5000UL;
boolean lockLow = true;
boolean takeLowTime;
//int pirPin = 2; //the digital pin connected to the PIR sensor's output
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;
int ledPin4 = 6;
int ledPin5 = 7;
void setup() {
Serial.begin(9600);
pinMode(photocellPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, 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);
}
void loop() {
photocellVal = analogRead(photocellPin);
pirVal = digitalRead(pirPin);
if (photocellVal < minLight && ledState == 0){
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
{
DET = digitalRead(pirPin);
if (DET == 1) // inactive
{
refTime = millis();
trippoint = refTime;
}
else // Active Low
{
pir_active();
}
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, 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);
}
takeLowTime = true;
//Serial.println("fade up");
{
if(pirVal == LOW)
digitalWrite(ledPin1, LOW); //the led visualizes the sensors output pin state
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, 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;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
void pir_active ()
{
trippoint = millis();
if ((trippoint-refTime) > threshold)
{
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3, HIGH);}
}
}
jdickinsonarduino:
Automobile_Deterrent:131: error: a function-definition is not allowed here before '{' token
Automobile_Deterrent:138: error: expected }' at end of input Automobile_Deterrent:138: error: expected }' at end of input
You have some bracket errors.
I think that it's easier to see where your brackets are amiss if you adopt "our" convention, each bracket on a line by itself:
void loop ()
{
something
or other
if (whatever)
{
this and that
}
}
That way they're stacked up like, a little more obvious.
[ I don't see where the problem is right now, because... I don't have access to the IDE right now. ]
okay, i've been looking at it and jockeying things yet to no avail..
Perhaps I really dont have an understanding of the }
i thought it's to execute whats after it as a whole?
before moving on to the next?
I'm really new at this...
jdickinsonarduino:
Perhaps I really dont have an understanding of the }
i thought it's to execute whats after it as a whole?
before moving on to the next?
The brackets are for bunching relevant stuff together.
jdickinsonarduino:
I'm really new at this...
This is about a lot more than "Send LED HIGH after 'x' seconds" ? isn't it?
You have to approach this thing in steps.
This "one felled swoop" approach is not a winner.
I inserted some brackets where I thought they should go, but that's kind of hard to tell
So, it compiles, but I'm pretty sure that it's not what you figure.
int photocellPin = 0;// Photocell connected to analog pin 0
int photocellVal = A0; // define photocell variable
int pirVal = LOW;// motion sensor variable
int ledState = 0;//state of the led
int minLight = 20;//min light threshold
int maxLight = 20;//max light threshold
//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
unsigned long lowIn;
unsigned long refTime;
unsigned long trippoint;
const long threshold = 5000;
byte DET;
const byte pirPin = 2;
unsigned long pause = 5000UL;
boolean lockLow = true;
boolean takeLowTime;
//int pirPin = 2; //the digital pin connected to the PIR sensor's output
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;
int ledPin4 = 6;
int ledPin5 = 7;
void setup()
{
Serial.begin(9600);
pinMode(photocellPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, 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);
}
void loop()
{
photocellVal = analogRead(photocellPin);
pirVal = digitalRead(pirPin);
if (photocellVal < minLight && ledState == 0)
{
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5)
{
analogWrite(ledPin1, fadeValue);
delay(30);
}
}
DET = digitalRead(pirPin);
if (DET == 1) // inactive
{
refTime = millis();
trippoint = refTime;
}
else // Active Low
{
pirActive();
}
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
if(lockLow) // IF without a Condition to Meet -- WRONG
{
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
if(pirVal == LOW)
{
digitalWrite(ledPin1, LOW); //the led visualizes the sensors output pin state
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
}
if(takeLowTime) // another IF without a CONDITION!!!!!
{
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(!lockLow && millis() - lowIn > pause) // BAD code, and NO CONDITION !!!!
{
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
void pirActive ()
{
trippoint = millis();
if ((trippoint-refTime) > threshold)
{
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3, HIGH);
}
}
I removed a lot of the comments, that's the only way I could keep from flying into a rage.
You have a few IF statements without a Condition, the equivalent of: IF (B). Well, What about "B", anyway? If (B == 100) or If (B == false) gives "B" a condition to meet. See?
Work out simpler stuff first that you can incorporate later.
There's much for you to develop.