Magician, thanks for the sketch it is quite useful. Using it I noticed that in fact the sensor was going many times from 1 to 0. So I decided to play a bit more with the sensor before trying the interrupts. I uploaded to the arduino this simple sketch:
void setup() {
Serial.begin(9600);
Serial.print("calibrating sensor ");
for(int i = 0; i < 5 ; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
pinMode(3, INPUT);
}
void loop() {
count++;
if(count%80 == 0)
{
Serial.println();
count = 0;
}
Serial.print(digitalRead(3));
delay(200);
}
I powered up the arduino using the USB cable and I got this output when I wave my hand infront of it:
calibrating sensor ..... done
SENSOR ACTIVE
1111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111110000000000001111000100100110000010110110001000101111
00010010111100001000111100110011011000000000111100000000111000000011110010010100
11000000001011000001001110001001101110001101011110000010011100000000101110000001
01110001001001100000010011100101010111100000100101100100000101100100000101000000
10111110000010011110000000101110000000011110000000111110000000111110000010111110
00001011110000001101100000000010110000010111110000000011110000010011100000001001
11000000101110000000000111000001011110000000010110010100011110000010011100000000
01110010010101110000010110110000010011100000000101100010010011100000010011100000
00001010000000010110001010011110011000111100100000011100000000101100001011011000
00001101100101000111100000001111001010100111000000110111000100101111000000001111
10000100111100000001001100000010111000000010011100010010111110000000000000011100
00000110000001101111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
it took about 15 seconds (sometimes even 30sec) to start printing only 1s. I repeated this for few times and sometimes the sensor did not stabilize at all.
My next step was to connect the V+ of the PIR to the arduino Vin instead of the 5V and I was surprised with the results:
calibrating sensor ..... done
SENSOR ACTIVE
1111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111101111111
11111111111110011111111111111011111111111100111111100111111111111111111111111111
It only printed the 0s when I passed my hand infront of the PIR. Now I tried the interrupt code and it worked perfectly getting this output:
calibrating sensor ..... done
SENSOR ACTIVE
11111111111111111111111111111111111111111111111110Motion detected handled.011111111
1111111111111111110Motion detected handled.0111111111111111111111111111111111111111
1111111111110Motion detected handled.0111111111111111111111111111111111111111111111
My question now is, why does it work when I take the power from Vin and not when I take it from 5V?
I also try powering up the arduino using a 9V battery but the results were completely different, the sensor almost never stabilized.
pylon, you are right I should have not used the serial interface in the interrupt. But it seemed so straight forward to use it for debugging purposes.