So let me explain. I want to build something that if it senses a movement, it would turn on the lights. I want to store the value if there is someone present as well. So I have a PIR sensor that is quite accurate. It gives a positive output if it senses movement. But not a plain positive. In a second of movement it outputs something like this: 10011111111011111111. So if their is movement then MOST of the output is positive. So my sketch checks if the movement is really present after 1/2 a second. If it does then it resets the timer so that the light won’t stay on after 30 seconds (just for prototyping).

Well the problem is that the led is just dimmed, like blinking furiously.

So guys could you help me out pleaseee. You are just geniuses for finding common errors pleaseee.

``````unsigned long timex;
unsigned long controlAct = 0;
unsigned long tpir = 0;
byte led = 13;
byte pirpin = 5;
int photo = 0;
long pirMin =  30 * 1000;  // 30 seconds
boolean cPir = 1;
boolean pir = 0;
boolean somebody = 0;
boolean actiPir = 0;

void setup(){
delay(20000);}

void loop(){
timex = millis();
updatePir();
}

void updatePir(){
if(somebody){digitalWrite(led, HIGH); photo = 1;}
else{digitalWrite(led,LOW); photo = 0;}
if(actiPir && timex - controlAct < pirMin){somebody = 1;}
else if(timex - controlAct > pirMin){somebody = 0;}
if(pir && cPir){tpir = timex; cPir = 0;}
if(pir && timex - tpir > 500 && timex - tpir < 800)//to eliminate the fluctuations
{cPir = 1; controlAct = timex; actiPir = 1;}
else if (timex - tpir > 800){cPir = 1;}
``````

Even after I commented this line

``````else{digitalWrite(led,LOW); photo = 0;}
``````

the led keeps dimmed.

why do you have a 20 second delay in setup()?

Also, you probably should set the LED pinmodes pinMode(led, OUTPUT); in the setup() function

20 second delay because the pir needs calibration and I know I forgot the set up output. Either way it is still like the photo. Dimmed kind of blinking furiously.

``````void updatePir(){
if(somebody){digitalWrite(led, HIGH); photo = 1;}
else{digitalWrite(led,LOW); photo = 0;}
if(actiPir && timex - controlAct < pirMin){somebody = 1;}
else if(timex - controlAct > pirMin){somebody = 0;}
if(pir && cPir){tpir = timex; cPir = 0;}
if(pir && timex - tpir > 500 && timex - tpir < 800)//to eliminate the fluctuations
{cPir = 1; controlAct = timex; actiPir = 1;}
else if (timex - tpir > 800){cPir = 1;}
``````

http://www.gammon.com.au/forum/?id=12153#style

You are just geniuses for finding common errors pleaseee

So is the compiler - that doesn't compile.

Have you considered debugging with serial prints?

Thank you for your answers. I re-wrote it and got the following. Now it does compile but still got the same problems. Also I tried the serial output debugging to see if the variable “somebody” change but it never changes to 0 so the led should not blink.

``````  unsigned long timex;
unsigned long controlAct = 0;
unsigned long tpir = 0;
byte led = 13;
byte pirpin = 5;
int photo = 0;
long pirMin =  30 * 1000;  // 30 seconds
boolean cPir = 1;
boolean pir = 0;
boolean somebody = 0;
boolean actiPir = 0;

void setup()
{
pinMode(led,OUTPUT);
delay(20000);
}

void loop()
{
timex = millis();
updatePir();
}

void updatePir()
{
if(somebody)
{
digitalWrite(led, HIGH);
photo = 1;
}
else
{
digitalWrite(led,LOW);
photo = 0;
}
if(actiPir && timex - controlAct < pirMin)
{
somebody = 1;
}
else if(timex - controlAct > pirMin){
somebody = 0;
}
if(pir && cPir){
tpir = timex;
cPir = 0;
}
if(pir && timex - tpir > 500 && timex - tpir < 800)//to eliminate the fluctuations
{
cPir = 1;
controlAct = timex;
actiPir = 1;
}
else if (timex - tpir > 800){
cPir = 1;
}
}
``````

I can’t see anything obviously wrong, although the code is pretty confusing. Some comments about what you are trying to do (in the code) would help.

How about putting in debugging displays (they aren’t there now) and testing, and then copy and paste what you see, here?

In particular display “somebody” and “pir”. There is so much stuff happening here I am just confused:

``````  if(pir && cPir){
tpir = timex;
cPir = 0;
}
if(pir && timex - tpir > 500 && timex - tpir < 800)//to eliminate the fluctuations
{
cPir = 1;
controlAct = timex;
actiPir = 1;
}
else if (timex - tpir > 800){
cPir = 1;
}
``````

Thank you sir, some how it now works. 