Hi guys,
I'm trying to get my pir sensor to work.
At the start the led flashes every 7 seconds instead of staying on when it detects motion and then the led just stops turning on.
int ledPin (7);
int pirPin (2);
int voltPin (4);
int pirState = LOW;
int val = 0;
void setup()
{
pinMode (ledPin, OUTPUT);
pinMode (pirPin, INPUT);
digitalWrite (voltPin, HIGH);
Serial.begin(9600);
}
void loop()
{
val = digitalRead(pirPin);
if (val == HIGH) {
digitalWrite (ledPin, HIGH);
//read pir sensor, if it detects movement flash led;
}
else{
val = digitalRead(pirPin);
if (val == LOW);
digitalWrite(ledPin, LOW);
delay (300);
}
}
What have I done wrong?
Thanks heaps,
nathman11
Compare the look and feel of this line:
if (val == LOW);
.... to that of this one:
if (val == HIGH) {
And you should also post details of the actual pir sensor you're using: some are kindof "bare", others have pots for adjusting timings and sensitivity.
Thanks for your help JimboZA,
My programming skills aren't too good so what would you recommend I do with those lines? Should I remove one of them?
I want the pir sensor to sense movement and then keep the led on.
I am using a SR501 pir sensor as per below:
http://www.ebay.com.au/itm/New-HC-SR501-Infrared-PIR-Motion-Sensor-Module-for-Arduino-Raspberry-M6G2-/121422792404?pt=LH_DefaultDomain_0&hash=item1c455cc6d4&_uhb=1
Thanks,
Nathman11
The line I marked with <<<<<<<<<<<< below ends in a ";" as you can see.
else{
val = digitalRead(pirPin);
if (val == LOW); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
digitalWrite(ledPin, LOW);
delay (300);
The ";" means the end of the statement, so the lines that follow it are always carried out, they're not part of the "if". they need to be inside {} like the other part:
if (val == HIGH) {
digitalWrite (ledPin, HIGH);
//read pir sensor, if it detects movement flash led;
}